Skip to content

Commit

Permalink
dev: add destroy for gc js engine
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxu19830126 committed Apr 16, 2019
1 parent 86a9dee commit 9f42bbc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
23 changes: 10 additions & 13 deletions pkg/plugin/builtin_base.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package plugin

var (
builtins = make(map[string]interface{})
const (
httpModuleName = "http"
jsonModuleName = "json"
logModuleName = "log"
redisModuleName = "redis"
)

func init() {
builtins["http"] = newHTTPModule()
builtins["json"] = &JSONModule{}
builtins["log"] = &LogModule{}
builtins["redis"] = &RedisModule{}
}

// Require require module
func Require(module string) interface{} {
return builtins[module]
}
var (
httpModule = newHTTPModule()
jsonModule = &JSONModule{}
logModule = &LogModule{}
)
3 changes: 3 additions & 0 deletions pkg/plugin/builtin_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// RedisModule redis module
type RedisModule struct {
rt *Runtime
}

// CreateRedis create redis
Expand All @@ -23,6 +24,8 @@ func (m *RedisModule) CreateRedis(cfg map[string]interface{}) *RedisOp {
},
}

m.rt.addCloser(p)

conn := p.Get()
_, err := conn.Do("PING")
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/plugin/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func (eng *Engine) LastActive() time.Time {

// Destroy destory all applied plugins
func (eng *Engine) Destroy() {
// TODO: destory
for _, rt := range eng.applied {
rt.destroy()
}
}

// UpdatePlugin update plugin
Expand Down
56 changes: 47 additions & 9 deletions pkg/plugin/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package plugin
import (
"errors"
"fmt"
"io"
"net/http"
"sync"

"github.com/fagongzi/gateway/pkg/filter"
"github.com/fagongzi/gateway/pkg/pb/metapb"
Expand All @@ -29,19 +31,24 @@ const (

// Runtime plugin runtime
type Runtime struct {
sync.RWMutex
filter.BaseFilter

meta *metapb.Plugin
vm *otto.Otto
this otto.Value
preFunc, postFunc, postErrFunc otto.Value

closers []io.Closer
}

// NewRuntime return a runtime
func NewRuntime(meta *metapb.Plugin) (*Runtime, error) {
rt := &Runtime{}

vm := otto.New()
// add require for using go module
vm.Set("require", Require)
vm.Set("require", rt.Require)
vm.Set("BreakFilterChainCode", filter.BreakFilterChainCode)
vm.Set("UsingResponse", filter.UsingResponse)

Expand Down Expand Up @@ -92,14 +99,45 @@ func NewRuntime(meta *metapb.Plugin) (*Runtime, error) {
return nil, fmt.Errorf("postErr must function")
}

return &Runtime{
meta: meta,
vm: vm,
this: this,
preFunc: preFunc,
postFunc: postFunc,
postErrFunc: postErrFunc,
}, nil
rt.meta = meta
rt.vm = vm
rt.this = this
rt.preFunc = preFunc
rt.postFunc = postFunc
rt.postErrFunc = postErrFunc
return rt, nil
}

func (rt *Runtime) destroy() {
rt.Lock()
defer rt.Unlock()

for _, closer := range rt.closers {
closer.Close()
}
}

func (rt *Runtime) addCloser(closer io.Closer) {
rt.Lock()
defer rt.Unlock()

rt.closers = append(rt.closers, closer)
}

// Require require module
func (rt *Runtime) Require(name string) interface{} {
switch name {
case httpModuleName:
return httpModule
case logModuleName:
return logModule
case jsonModuleName:
return jsonModule
case redisModuleName:
return &RedisModule{rt: rt}
default:
return nil
}
}

// Pre filter pre method
Expand Down

0 comments on commit 9f42bbc

Please sign in to comment.