-
Notifications
You must be signed in to change notification settings - Fork 41
/
rtb.go
57 lines (49 loc) · 996 Bytes
/
rtb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"context"
"fmt"
"strings"
"time"
)
func main() {
// We have 50 msec to return an answer
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
url := "https://go.dev"
bid := bidOn(ctx, url)
fmt.Println(bid)
}
// If algo didn't finish in time, return a default bid
func bidOn(ctx context.Context, url string) Bid {
ch := make(chan Bid, 1) // buffered channel to avoid goroutine leak
go func() {
ch <- bestBid(url)
}()
select {
case bid := <-ch:
return bid
case <-ctx.Done():
return defaultBid
}
}
var defaultBid = Bid{
AdURL: "http://adsЯus.com/default",
Price: 3,
}
// Written by Algo team, time to completion varies
func bestBid(url string) Bid {
// Simulate work
d := 100 * time.Millisecond
if strings.HasPrefix(url, "https://") {
d = 20 * time.Millisecond
}
time.Sleep(d)
return Bid{
AdURL: "http://adsЯus.com/ad17",
Price: 7,
}
}
type Bid struct {
AdURL string
Price int // In ¢
}