-
Notifications
You must be signed in to change notification settings - Fork 726
/
tmax_adjustments.go
63 lines (51 loc) · 1.86 KB
/
tmax_adjustments.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
58
59
60
61
62
63
package exchange
import (
"context"
"time"
"github.com/prebid/prebid-server/v2/config"
)
type TmaxAdjustmentsPreprocessed struct {
BidderNetworkLatencyBuffer uint
PBSResponsePreparationDuration uint
BidderResponseDurationMin uint
IsEnforced bool
}
func ProcessTMaxAdjustments(adjustmentsConfig config.TmaxAdjustments) *TmaxAdjustmentsPreprocessed {
if !adjustmentsConfig.Enabled {
return nil
}
isEnforced := adjustmentsConfig.BidderResponseDurationMin != 0 &&
(adjustmentsConfig.BidderNetworkLatencyBuffer != 0 || adjustmentsConfig.PBSResponsePreparationDuration != 0)
tmax := &TmaxAdjustmentsPreprocessed{
BidderNetworkLatencyBuffer: adjustmentsConfig.BidderNetworkLatencyBuffer,
PBSResponsePreparationDuration: adjustmentsConfig.PBSResponsePreparationDuration,
BidderResponseDurationMin: adjustmentsConfig.BidderResponseDurationMin,
IsEnforced: isEnforced,
}
return tmax
}
type bidderTmaxContext interface {
Deadline() (deadline time.Time, ok bool)
RemainingDurationMS(deadline time.Time) int64
Until(t time.Time) time.Duration
}
type bidderTmaxCtx struct{ ctx context.Context }
func (b *bidderTmaxCtx) RemainingDurationMS(deadline time.Time) int64 {
return time.Until(deadline).Milliseconds()
}
func (b *bidderTmaxCtx) Deadline() (deadline time.Time, ok bool) {
deadline, ok = b.ctx.Deadline()
return
}
// Until returns the remaining duration until the specified time
func (b *bidderTmaxCtx) Until(t time.Time) time.Duration {
return time.Until(t)
}
func getBidderTmax(ctx bidderTmaxContext, requestTmaxMS int64, tmaxAdjustments TmaxAdjustmentsPreprocessed) int64 {
if tmaxAdjustments.IsEnforced {
if deadline, ok := ctx.Deadline(); ok {
return ctx.RemainingDurationMS(deadline) - int64(tmaxAdjustments.BidderNetworkLatencyBuffer) - int64(tmaxAdjustments.PBSResponsePreparationDuration)
}
}
return requestTmaxMS
}