// Context is a wrapper around context.Context and contains the current pid for this context
typeContextstruct{
context.Context
pidIDType
}
// GetPID returns the PID for this context
func(c*Context)GetPID()IDType{
returnc.pid
}
// GetParent returns the parent process context (if any)
func(c*Context)GetParent()*Context{
returnGetContext(c.Context)
}
// Value is part of the interface for context.Context. We mostly defer to the internal context - but we return this in response to the ProcessContextKey
func(c*Context)Value(keyinterface{})interface{}{
ifkey==ProcessContextKey{
returnc
}
returnc.Context.Value(key)
}
// ProcessContextKey is the key under which process contexts are stored
varProcessContextKeyinterface{}="process-context"
// GetContext will return a process context if one exists
funcGetContext(ctxcontext.Context)*Context{
ifpCtx,ok:=ctx.(*Context);ok{
returnpCtx
}
pCtxInterface:=ctx.Value(ProcessContextKey)
ifpCtxInterface==nil{
returnnil
}
ifpCtx,ok:=pCtxInterface.(*Context);ok{
returnpCtx
}
returnnil
}
// GetPID returns the PID for this context
funcGetPID(ctxcontext.Context)IDType{
pCtx:=GetContext(ctx)
ifpCtx==nil{
return""
}
returnpCtx.GetPID()
}
// GetParentPID returns the ParentPID for this context