Skip to content

Commit

Permalink
fix nil
Browse files Browse the repository at this point in the history
Signed-off-by: xuleiming <[email protected]>
  • Loading branch information
xuleiming committed Dec 3, 2024
1 parent 5ed7898 commit ef6405e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
38 changes: 19 additions & 19 deletions bfe_modules/mod_wasmplug/conf_mod_wasmplug.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ type PluginMeta struct {

type FilterRule struct {
Cond condition.Condition // condition for plugin
PluginList []*bfe_wasmplug.WasmPlugin
PluginList []bfe_wasmplug.WasmPlugin
}

type RuleList []FilterRule
type ProductRules map[string]*RuleList // product => list of filter rules
type ProductRules map[string]RuleList // product => list of filter rules

func updatePluginConf(t *PluginTable, conf PluginConfFile, pluginPath string) error {
if conf.Version != nil && *conf.Version != t.GetVersion() {
pluginMapNew := make(map[string]*bfe_wasmplug.WasmPlugin)
pluginMapNew := make(map[string]bfe_wasmplug.WasmPlugin)
var beforeLocationRulesNew RuleList
productRulesNew := make(ProductRules)

Expand All @@ -116,16 +116,16 @@ func updatePluginConf(t *PluginTable, conf PluginConfFile, pluginPath string) er
pm := t.GetPluginMap()
if conf.PluginMap != nil {
for pn, p := range *conf.PluginMap {
plugOld := (*pm)[pn]
plugOld := pm[pn]
// check whether plugin version changed.
if plugOld != nil {
configOld := (*plugOld).GetConfig()
configOld := plugOld.GetConfig()
if configOld.WasmVersion == p.WasmVersion && configOld.ConfigVersion == p.ConfVersion {
// not change, just copy to new map
pluginMapNew[pn] = plugOld

// ensure instance num
actual := (*plugOld).EnsureInstanceNum(p.InstanceNum)
actual := plugOld.EnsureInstanceNum(p.InstanceNum)
if actual != p.InstanceNum {
return fmt.Errorf("can not EnsureInstanceNum, plugin:%s, num:%d", pn, p.InstanceNum)
}
Expand All @@ -151,7 +151,7 @@ func updatePluginConf(t *PluginTable, conf PluginConfFile, pluginPath string) er

// plug.OnPluginStart()

pluginMapNew[pn] = &plug
pluginMapNew[pn] = plug
}
}

Expand Down Expand Up @@ -194,19 +194,19 @@ func updatePluginConf(t *PluginTable, conf PluginConfFile, pluginPath string) er
}
rulelist = append(rulelist, rule)
}
productRulesNew[product] = &rulelist
productRulesNew[product] = rulelist
}
}

// 3. update PluginTable
t.Update(*conf.Version, &beforeLocationRulesNew, productRulesNew, &pluginMapNew)
t.Update(*conf.Version, beforeLocationRulesNew, productRulesNew, pluginMapNew)

// 4. stop & clear old plugins
for pn, plug := range *pm {
for pn, plug := range pm {
if _, ok := unchanged[pn]; !ok {
// stop plug
(*plug).OnPluginDestroy()
(*plug).Clear()
plug.OnPluginDestroy()
plug.Clear()
}
}
}
Expand All @@ -216,19 +216,19 @@ func updatePluginConf(t *PluginTable, conf PluginConfFile, pluginPath string) er
type PluginTable struct {
lock sync.RWMutex
version string
beforeLocationRules *RuleList
beforeLocationRules RuleList
productRules ProductRules
pluginMap *map[string]*bfe_wasmplug.WasmPlugin
pluginMap map[string]bfe_wasmplug.WasmPlugin
}

func NewPluginTable() *PluginTable {
t := new(PluginTable)
t.productRules = make(ProductRules)
t.pluginMap = new(map[string]*bfe_wasmplug.WasmPlugin)
t.pluginMap = make(map[string]bfe_wasmplug.WasmPlugin)
return t
}

func (t *PluginTable) Update(version string, beforeLocationRules *RuleList, productRules ProductRules, pluginMap *map[string]*bfe_wasmplug.WasmPlugin) {
func (t *PluginTable) Update(version string, beforeLocationRules RuleList, productRules ProductRules, pluginMap map[string]bfe_wasmplug.WasmPlugin) {
t.lock.Lock()

t.version = version
Expand All @@ -245,19 +245,19 @@ func (t *PluginTable) GetVersion() string {
return t.version
}

func (t *PluginTable) GetPluginMap() *map[string]*bfe_wasmplug.WasmPlugin {
func (t *PluginTable) GetPluginMap() map[string]bfe_wasmplug.WasmPlugin {
defer t.lock.RUnlock()
t.lock.RLock()
return t.pluginMap
}

func (t *PluginTable) GetBeforeLocationRules() *RuleList {
func (t *PluginTable) GetBeforeLocationRules() RuleList {
defer t.lock.RUnlock()
t.lock.RLock()
return t.beforeLocationRules
}

func (t *PluginTable) Search(product string) (*RuleList, bool) {
func (t *PluginTable) Search(product string) (RuleList, bool) {
t.lock.RLock()
productRules := t.productRules
t.lock.RUnlock()
Expand Down
8 changes: 4 additions & 4 deletions bfe_modules/mod_wasmplug/mod_wasmplug.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func (m *ModuleWasm) init(cfg *ConfModWasm, cbs *bfe_module.BfeCallbacks,

//
func (m *ModuleWasm) wasmBeforeLocationHandler(request *bfe_basic.Request) (int, *bfe_http.Response) {
var pl []*bfe_wasmplug.WasmPlugin
var pl []bfe_wasmplug.WasmPlugin
rl := m.pluginTable.GetBeforeLocationRules()
for _, rule := range *rl {
for _, rule := range rl {
if rule.Cond.Match(request) {
// find pluginlist
pl = rule.PluginList
Expand Down Expand Up @@ -162,9 +162,9 @@ func (m *ModuleWasm) wasmBeforeLocationHandler(request *bfe_basic.Request) (int,

//
func (m *ModuleWasm) wasmRequestHandler(request *bfe_basic.Request) (int, *bfe_http.Response) {
var pl []*bfe_wasmplug.WasmPlugin
var pl []bfe_wasmplug.WasmPlugin
rl, _ := m.pluginTable.Search(request.Route.Product)
for _, rule := range *rl {
for _, rule := range rl {
if rule.Cond.Match(request) {
// find pluginlist
pl = rule.PluginList
Expand Down
10 changes: 5 additions & 5 deletions bfe_wasmplug/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func newContextID(rootContextID int32) int32 {
}
}

func NewFilter(plugin *WasmPlugin, request *bfe_basic.Request) *Filter {
instance := (*plugin).GetInstance()
rootContextID := (*plugin).GetRootContextID()
func NewFilter(plugin WasmPlugin, request *bfe_basic.Request) *Filter {
instance := plugin.GetInstance()
rootContextID := plugin.GetRootContextID()

filter := &Filter{
plugin: *plugin,
plugin: plugin,
instance: instance,
rootContextID: rootContextID,
contextID: newContextID(rootContextID),
Expand All @@ -69,7 +69,7 @@ func NewFilter(plugin *WasmPlugin, request *bfe_basic.Request) *Filter {
log.Logger.Info("[proxywasm][filter] abi version: %v", filter.abi.Name())
if filter.abi != nil {
// v1
imports := &v1Imports{plugin: *plugin, filter: filter}
imports := &v1Imports{plugin: plugin, filter: filter}
imports.DefaultImportsHandler.Instance = instance
filter.abi.SetImports(imports)
filter.exports = filter.abi.GetExports()
Expand Down

0 comments on commit ef6405e

Please sign in to comment.