handlers are bound to config, so have to live in the instance struct

This commit is contained in:
Javier Guerra
2019-11-15 21:00:36 -05:00
committed by Guilherme Salazar
parent 8bebb13883
commit ca3b80fff1
2 changed files with 91 additions and 17 deletions

View File

@@ -54,7 +54,17 @@ type pluginData struct {
code *plugin.Plugin code *plugin.Plugin
constructor func() interface{} constructor func() interface{}
config interface{} config interface{}
}
// --- instanceData --- //
type instanceData struct {
id int
plugin *pluginData
initialized bool
config interface{}
handlers map[string]func(kong *pdk.PDK) handlers map[string]func(kong *pdk.PDK)
ipc chan string
pdk *pdk.PDK
} }
type ( type (
@@ -67,9 +77,8 @@ type (
logger interface{ Log(*pdk.PDK) } logger interface{ Log(*pdk.PDK) }
) )
func (plug *pluginData) setHandlers() { func getHandlers(config interface{}) map[string]func(kong *pdk.PDK) {
handlers := map[string]func(kong *pdk.PDK){} handlers := map[string]func(kong *pdk.PDK){}
config := plug.constructor()
if h, ok := config.(certificater); ok { handlers["certificate"] = h.Certificate } if h, ok := config.(certificater); ok { handlers["certificate"] = h.Certificate }
if h, ok := config.(rewriter) ; ok { handlers["rewrite"] = h.Rewrite } if h, ok := config.(rewriter) ; ok { handlers["rewrite"] = h.Rewrite }
@@ -79,17 +88,7 @@ func (plug *pluginData) setHandlers() {
if h, ok := config.(prereader) ; ok { handlers["preread"] = h.Preread } if h, ok := config.(prereader) ; ok { handlers["preread"] = h.Preread }
if h, ok := config.(logger) ; ok { handlers["log"] = h.Log } if h, ok := config.(logger) ; ok { handlers["log"] = h.Log }
plug.handlers = handlers return handlers
}
// --- instanceData --- //
type instanceData struct {
id int
plugin *pluginData
initialized bool
config interface{}
ipc chan string
pdk *pdk.PDK
} }
// --- PluginServer --- // // --- PluginServer --- //
@@ -149,7 +148,6 @@ func (s PluginServer) loadPlugin(name string) (plug *pluginData, err error) {
constructor: constructor, constructor: constructor,
config: constructor(), config: constructor(),
} }
plug.setHandlers()
s.lock.Lock() s.lock.Lock()
s.plugins[name] = plug s.plugins[name] = plug
@@ -159,7 +157,6 @@ func (s PluginServer) loadPlugin(name string) (plug *pluginData, err error) {
} }
func getSchemaType(t reflect.Type) (string, bool) { func getSchemaType(t reflect.Type) (string, bool) {
//log.Printf("SCHEMA TYPE FOR T IS %s\n", t.String())
switch t.Kind() { switch t.Kind() {
case reflect.String: case reflect.String:
return `"string"`, true return `"string"`, true
@@ -231,9 +228,11 @@ func (s PluginServer) GetPluginInfo(name string, info *PluginInfo) error {
*info = PluginInfo{Name: name} *info = PluginInfo{Name: name}
info.Phases = make([]string, len(plug.handlers)) handlers := getHandlers(plug.config)
info.Phases = make([]string, len(handlers))
var i = 0 var i = 0
for name := range plug.handlers { for name := range handlers {
info.Phases[i] = name info.Phases[i] = name
i++ i++
} }
@@ -287,12 +286,40 @@ func (s *PluginServer) StartInstance(config PluginConfig, status *InstanceStatus
return fmt.Errorf("Decoding config: %w", err) return fmt.Errorf("Decoding config: %w", err)
} }
ipc := make(chan string)
instance := instanceData{ instance := instanceData{
id: s.nextId, id: s.nextId,
plugin: plug, plugin: plug,
config: instanceConfig, config: instanceConfig,
ipc: ipc,
pdk: pdk.Init(ipc),
} }
//log.Printf("Will launch goroutine for key %d / operation %s\n", key, op)
go func() {
_ = <-ipc
//if run == "run" {
// //log.Printf("Received run as expected: %s\n", run)
//} else {
// //log.Printf("Unexpected message: %s\n", run)
//}
// if !plug.initialized {
// init, _ := plug.code.Lookup("Init")
// if init != nil {
// //log.Printf("will call Init\n")
// init.(func(*pdk.PDK))(conn.pdk)
// }
// plug.initialized = true
// }
// plug.tryEvent(op, conn.pdk)
// conn.Done()
ipc <- "ret"
}()
s.lock.Lock() s.lock.Lock()
s.nextId++ s.nextId++
s.instances[instance.id] = instance s.instances[instance.id] = instance
@@ -307,6 +334,23 @@ func (s *PluginServer) StartInstance(config PluginConfig, status *InstanceStatus
return nil return nil
} }
func (s PluginServer) InstanceStatus(id int, status *InstanceStatus) error {
s.lock.RLock()
instance, ok := s.instances[id]
s.lock.RUnlock()
if !ok {
return fmt.Errorf("No plugin instance %d", id)
}
*status = InstanceStatus{
Name: instance.plugin.name,
Id: instance.id,
Config: instance.config,
}
return nil
}
/// exported method /// exported method
func (s PluginServer) CloseInstance(id int, status *InstanceStatus) error { func (s PluginServer) CloseInstance(id int, status *InstanceStatus) error {
s.lock.RLock() s.lock.RLock()
@@ -330,3 +374,27 @@ func (s PluginServer) CloseInstance(id int, status *InstanceStatus) error {
return nil return nil
} }
type StepIn struct {
instanceId int
method string
params interface{}
}
type StepOut struct {
callback string
params interface{}
}
/// exported method
func (s PluginServer) Step(in StepIn, out *StepOut) error {
s.lock.RLock()
instance, ok := s.instances[in.instanceId]
s.lock.RUnlock()
if !ok {
return fmt.Errorf("No plugin instance %d", in.instanceId)
}
return nil
}

View File

@@ -1,5 +1,9 @@
#!/bin/bash #!/bin/bash
if [ "$1" == "-v" ]; then
VERBOSE='true'
fi
SOCKET='sock' SOCKET='sock'
if pgrep go-pluginserver -l; then if pgrep go-pluginserver -l; then
@@ -11,8 +15,10 @@ else
fi fi
msg() { msg() {
[ -v VERBOSE ] && rq <<< "$1"
METHOD="$(rq <<< "$1" -- 'at([2])')" METHOD="$(rq <<< "$1" -- 'at([2])')"
response="$(rq <<< "$1" -M | nc -U "$SOCKET" -N | rq -m)" response="$(rq <<< "$1" -M | nc -U "$SOCKET" -N | rq -m)"
[ -v VERBOSE ] && rq <<< "$response"
ERROR="$(rq <<< "$response" -- 'at [2] ')" ERROR="$(rq <<< "$response" -- 'at [2] ')"
RESULT="$(rq <<< "$response" -- 'at [3] ')" RESULT="$(rq <<< "$response" -- 'at [3] ')"