-
Notifications
You must be signed in to change notification settings - Fork 701
/
context.go
74 lines (60 loc) · 1.91 KB
/
context.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
64
65
66
67
68
69
70
71
72
73
74
package hookexecution
import (
"sync"
"github.com/golang/glog"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/hooks/hookstage"
"github.com/prebid/prebid-server/v2/privacy"
)
// executionContext holds information passed to module's hook during hook execution.
type executionContext struct {
endpoint string
stage string
accountID string
account *config.Account
moduleContexts *moduleContexts
activityControl privacy.ActivityControl
}
func (ctx executionContext) getModuleContext(moduleName string) hookstage.ModuleInvocationContext {
moduleInvocationCtx := hookstage.ModuleInvocationContext{Endpoint: ctx.endpoint}
if ctx.moduleContexts != nil {
if mc, ok := ctx.moduleContexts.get(moduleName); ok {
moduleInvocationCtx.ModuleContext = mc
}
}
if ctx.account != nil {
cfg, err := ctx.account.Hooks.Modules.ModuleConfig(moduleName)
if err != nil {
glog.Warningf("Failed to get account config for %s module: %s", moduleName, err)
}
moduleInvocationCtx.AccountConfig = cfg
}
return moduleInvocationCtx
}
// moduleContexts preserves data the module wants to pass to itself from earlier stages to later stages.
type moduleContexts struct {
sync.RWMutex
ctxs map[string]hookstage.ModuleContext // format: {"module_name": hookstage.ModuleContext}
}
func (mc *moduleContexts) put(moduleName string, mCtx hookstage.ModuleContext) {
mc.Lock()
defer mc.Unlock()
newCtx := mCtx
if existingCtx, ok := mc.ctxs[moduleName]; ok && existingCtx != nil {
for k, v := range mCtx {
existingCtx[k] = v
}
newCtx = existingCtx
}
mc.ctxs[moduleName] = newCtx
}
func (mc *moduleContexts) get(moduleName string) (hookstage.ModuleContext, bool) {
mc.RLock()
defer mc.RUnlock()
mCtx, ok := mc.ctxs[moduleName]
return mCtx, ok
}
type stageModuleContext struct {
groupCtx []groupModuleContext
}
type groupModuleContext map[string]hookstage.ModuleContext