github.com/prebid/prebid-server/v2@v2.18.0/hooks/hookexecution/context.go (about)

     1  package hookexecution
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/golang/glog"
     7  	"github.com/prebid/prebid-server/v2/config"
     8  	"github.com/prebid/prebid-server/v2/hooks/hookstage"
     9  	"github.com/prebid/prebid-server/v2/privacy"
    10  )
    11  
    12  // executionContext holds information passed to module's hook during hook execution.
    13  type executionContext struct {
    14  	endpoint        string
    15  	stage           string
    16  	accountID       string
    17  	account         *config.Account
    18  	moduleContexts  *moduleContexts
    19  	activityControl privacy.ActivityControl
    20  }
    21  
    22  func (ctx executionContext) getModuleContext(moduleName string) hookstage.ModuleInvocationContext {
    23  	moduleInvocationCtx := hookstage.ModuleInvocationContext{Endpoint: ctx.endpoint}
    24  	if ctx.moduleContexts != nil {
    25  		if mc, ok := ctx.moduleContexts.get(moduleName); ok {
    26  			moduleInvocationCtx.ModuleContext = mc
    27  		}
    28  	}
    29  
    30  	if ctx.account != nil {
    31  		cfg, err := ctx.account.Hooks.Modules.ModuleConfig(moduleName)
    32  		if err != nil {
    33  			glog.Warningf("Failed to get account config for %s module: %s", moduleName, err)
    34  		}
    35  
    36  		moduleInvocationCtx.AccountConfig = cfg
    37  	}
    38  
    39  	return moduleInvocationCtx
    40  }
    41  
    42  // moduleContexts preserves data the module wants to pass to itself from earlier stages to later stages.
    43  type moduleContexts struct {
    44  	sync.RWMutex
    45  	ctxs map[string]hookstage.ModuleContext // format: {"module_name": hookstage.ModuleContext}
    46  }
    47  
    48  func (mc *moduleContexts) put(moduleName string, mCtx hookstage.ModuleContext) {
    49  	mc.Lock()
    50  	defer mc.Unlock()
    51  
    52  	newCtx := mCtx
    53  	if existingCtx, ok := mc.ctxs[moduleName]; ok && existingCtx != nil {
    54  		for k, v := range mCtx {
    55  			existingCtx[k] = v
    56  		}
    57  		newCtx = existingCtx
    58  	}
    59  	mc.ctxs[moduleName] = newCtx
    60  }
    61  
    62  func (mc *moduleContexts) get(moduleName string) (hookstage.ModuleContext, bool) {
    63  	mc.RLock()
    64  	defer mc.RUnlock()
    65  	mCtx, ok := mc.ctxs[moduleName]
    66  
    67  	return mCtx, ok
    68  }
    69  
    70  type stageModuleContext struct {
    71  	groupCtx []groupModuleContext
    72  }
    73  
    74  type groupModuleContext map[string]hookstage.ModuleContext