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