fn: agent call overrider (#1080)

Similar to LB Agent call overrider, this PR adds Agent overrider
for Agents to modify/analyze a Call/Extensions during GetCall().
This commit is contained in:
Tolga Ceylan
2018-06-20 16:21:09 -07:00
committed by GitHub
parent 77f243bfaf
commit 881a0ba1db
5 changed files with 63 additions and 11 deletions

View File

@@ -120,6 +120,8 @@ type agent struct {
shutonce sync.Once
callEndCount int64
disableAsyncDequeue bool
callOverrider CallOverrider
}
type AgentOption func(*agent) error
@@ -194,6 +196,17 @@ func WithoutAsyncDequeue() AgentOption {
}
}
// Agents can use this to register a CallOverrider to modify a Call and extensions
func WithCallOverrider(fn CallOverrider) AgentOption {
return func(a *agent) error {
if a.callOverrider != nil {
return errors.New("lb-agent call overriders already exists")
}
a.callOverrider = fn
return nil
}
}
// Create a default docker driver from agent config
func NewDockerDriver(cfg *AgentConfig) *docker.DockerDriver {
return docker.NewDocker(drivers.Config{

View File

@@ -41,6 +41,9 @@ type Call interface {
End(ctx context.Context, err error) error
}
// Interceptor in GetCall
type CallOverrider func(*models.Call, map[string]string) (map[string]string, error)
// TODO build w/o closures... lazy
type CallOpt func(c *call) error
@@ -261,6 +264,16 @@ func (a *agent) GetCall(opts ...CallOpt) (Call, error) {
return nil, errors.New("no model or request provided for call")
}
// If overrider is present, let's allow it to modify models.Call
// and call extensions
if a.callOverrider != nil {
ext, err := a.callOverrider(c.Call, c.extensions)
if err != nil {
return nil, err
}
c.extensions = ext
}
mem := c.Memory + uint64(c.TmpFsSize)
if !a.resources.IsResourcePossible(mem, uint64(c.CPUs), c.Type == models.TypeAsync) {
// if we're not going to be able to run this call on this machine, bail here.

View File

@@ -17,8 +17,6 @@ import (
"github.com/fnproject/fn/fnext"
)
type CallOverrider func(*models.Call, map[string]string) (map[string]string, error)
type lbAgent struct {
cfg AgentConfig
da DataAccess
@@ -40,7 +38,7 @@ func WithLBAgentConfig(cfg *AgentConfig) LBAgentOption {
}
// LB agents can use this to register a CallOverrider to modify a Call and extensions
func WithCallOverrider(fn CallOverrider) LBAgentOption {
func WithLBCallOverrider(fn CallOverrider) LBAgentOption {
return func(a *lbAgent) error {
if a.callOverrider != nil {
return errors.New("lb-agent call overriders already exists")