|
1 |
| -// Copyright (c) 2019 The BFE Authors. |
| 1 | +// Copyright (c) 2024 The BFE Authors. |
2 | 2 | //
|
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | // you may not use this file except in compliance with the License.
|
|
15 | 15 | package mod_wasmplugin
|
16 | 16 |
|
17 | 17 | import (
|
18 |
| - "fmt" |
19 |
| - "os" |
20 |
| - "path" |
21 |
| - "sync" |
22 |
| - |
23 | 18 | "github.com/baidu/go-lib/log"
|
24 |
| - "github.com/bfenetworks/bfe/bfe_basic/condition" |
25 | 19 | "github.com/bfenetworks/bfe/bfe_util"
|
26 |
| - "github.com/bfenetworks/bfe/bfe_util/json" |
27 |
| - "github.com/bfenetworks/bfe/bfe_wasmplugin" |
28 | 20 | gcfg "gopkg.in/gcfg.v1"
|
29 | 21 | )
|
30 | 22 |
|
@@ -74,218 +66,3 @@ func (cfg *ConfModWasm) Check(confRoot string) error {
|
74 | 66 |
|
75 | 67 | return nil
|
76 | 68 | }
|
77 |
| - |
78 |
| -type PluginConfFile struct { |
79 |
| - Version *string // version of the config |
80 |
| - BeforeLocationRules *[]FilterRuleFile // rule list for BeforeLocation |
81 |
| - FoundProductRules *map[string][]FilterRuleFile // product --> rule list for FoundProduct |
82 |
| - PluginMap *map[string]PluginMeta |
83 |
| -} |
84 |
| - |
85 |
| -type FilterRuleFile struct { |
86 |
| - Cond *string // condition for plugin |
87 |
| - PluginList *[]string |
88 |
| -} |
89 |
| - |
90 |
| -type PluginMeta struct { |
91 |
| - Name string |
92 |
| - WasmVersion string |
93 |
| - ConfVersion string |
94 |
| - // Md5 string |
95 |
| - InstanceNum int |
96 |
| - Product string |
97 |
| -} |
98 |
| - |
99 |
| -type FilterRule struct { |
100 |
| - Cond condition.Condition // condition for plugin |
101 |
| - PluginList []bfe_wasmplugin.WasmPlugin |
102 |
| -} |
103 |
| - |
104 |
| -type RuleList []FilterRule |
105 |
| -type ProductRules map[string]RuleList // product => list of filter rules |
106 |
| - |
107 |
| -func updatePluginConf(t *PluginTable, conf PluginConfFile, pluginPath string) error { |
108 |
| - if conf.Version != nil && *conf.Version != t.GetVersion() { |
109 |
| - pluginMapNew := make(map[string]bfe_wasmplugin.WasmPlugin) |
110 |
| - var beforeLocationRulesNew RuleList |
111 |
| - productRulesNew := make(ProductRules) |
112 |
| - |
113 |
| - // 1. check plugin map |
114 |
| - unchanged := make(map[string]bool) |
115 |
| - |
116 |
| - pm := t.GetPluginMap() |
117 |
| - if conf.PluginMap != nil { |
118 |
| - for pn, p := range *conf.PluginMap { |
119 |
| - plugOld := pm[pn] |
120 |
| - // check whether plugin version changed. |
121 |
| - if plugOld != nil { |
122 |
| - configOld := plugOld.GetConfig() |
123 |
| - if configOld.WasmVersion == p.WasmVersion && configOld.ConfigVersion == p.ConfVersion { |
124 |
| - // not change, just copy to new map |
125 |
| - pluginMapNew[pn] = plugOld |
126 |
| - |
127 |
| - // ensure instance num |
128 |
| - actual := plugOld.EnsureInstanceNum(p.InstanceNum) |
129 |
| - if actual != p.InstanceNum { |
130 |
| - return fmt.Errorf("can not EnsureInstanceNum, plugin:%s, num:%d", pn, p.InstanceNum) |
131 |
| - } |
132 |
| - |
133 |
| - unchanged[pn] = true |
134 |
| - continue |
135 |
| - } |
136 |
| - } |
137 |
| - // if changed, construct a new plugin. |
138 |
| - wasmconf := bfe_wasmplugin.WasmPluginConfig { |
139 |
| - PluginName: pn, |
140 |
| - WasmVersion: p.WasmVersion, |
141 |
| - ConfigVersion: p.ConfVersion, |
142 |
| - InstanceNum: p.InstanceNum, |
143 |
| - Path: path.Join(pluginPath, pn), |
144 |
| - // Md5: p.Md5, |
145 |
| - } |
146 |
| - plug, err := bfe_wasmplugin.NewWasmPlugin(wasmconf) |
147 |
| - if err != nil { |
148 |
| - // build plugin error |
149 |
| - return err |
150 |
| - } |
151 |
| - |
152 |
| - // plug.OnPluginStart() |
153 |
| - |
154 |
| - pluginMapNew[pn] = plug |
155 |
| - } |
156 |
| - } |
157 |
| - |
158 |
| - // 2. construct product rules |
159 |
| - if conf.BeforeLocationRules != nil { |
160 |
| - for _, r := range *conf.BeforeLocationRules { |
161 |
| - rule := FilterRule{} |
162 |
| - cond, err := condition.Build(*r.Cond) |
163 |
| - if err != nil { |
164 |
| - return err |
165 |
| - } |
166 |
| - rule.Cond =cond |
167 |
| - for _, pn := range *r.PluginList { |
168 |
| - plug := pluginMapNew[pn] |
169 |
| - if plug == nil { |
170 |
| - return fmt.Errorf("unknown plugin: %s", pn) |
171 |
| - } |
172 |
| - rule.PluginList = append(rule.PluginList, plug) |
173 |
| - } |
174 |
| - beforeLocationRulesNew = append(beforeLocationRulesNew, rule) |
175 |
| - } |
176 |
| - } |
177 |
| - |
178 |
| - if conf.FoundProductRules != nil { |
179 |
| - for product, rules := range *conf.FoundProductRules { |
180 |
| - var rulelist RuleList |
181 |
| - for _, r := range rules { |
182 |
| - rule := FilterRule{} |
183 |
| - cond, err := condition.Build(*r.Cond) |
184 |
| - if err != nil { |
185 |
| - return err |
186 |
| - } |
187 |
| - rule.Cond =cond |
188 |
| - for _, pn := range *r.PluginList { |
189 |
| - plug := pluginMapNew[pn] |
190 |
| - if plug == nil { |
191 |
| - return fmt.Errorf("unknown plugin: %s", pn) |
192 |
| - } |
193 |
| - rule.PluginList = append(rule.PluginList, plug) |
194 |
| - } |
195 |
| - rulelist = append(rulelist, rule) |
196 |
| - } |
197 |
| - productRulesNew[product] = rulelist |
198 |
| - } |
199 |
| - } |
200 |
| - |
201 |
| - // 3. update PluginTable |
202 |
| - t.Update(*conf.Version, beforeLocationRulesNew, productRulesNew, pluginMapNew) |
203 |
| - |
204 |
| - // 4. stop & clear old plugins |
205 |
| - for pn, plug := range pm { |
206 |
| - if _, ok := unchanged[pn]; !ok { |
207 |
| - // stop plug |
208 |
| - plug.OnPluginDestroy() |
209 |
| - plug.Clear() |
210 |
| - } |
211 |
| - } |
212 |
| - } |
213 |
| - return nil |
214 |
| -} |
215 |
| - |
216 |
| -type PluginTable struct { |
217 |
| - lock sync.RWMutex |
218 |
| - version string |
219 |
| - beforeLocationRules RuleList |
220 |
| - productRules ProductRules |
221 |
| - pluginMap map[string]bfe_wasmplugin.WasmPlugin |
222 |
| -} |
223 |
| - |
224 |
| -func NewPluginTable() *PluginTable { |
225 |
| - t := new(PluginTable) |
226 |
| - t.productRules = make(ProductRules) |
227 |
| - t.pluginMap = make(map[string]bfe_wasmplugin.WasmPlugin) |
228 |
| - return t |
229 |
| -} |
230 |
| - |
231 |
| -func (t *PluginTable) Update(version string, beforeLocationRules RuleList, productRules ProductRules, pluginMap map[string]bfe_wasmplugin.WasmPlugin) { |
232 |
| - t.lock.Lock() |
233 |
| - |
234 |
| - t.version = version |
235 |
| - t.beforeLocationRules = beforeLocationRules |
236 |
| - t.productRules = productRules |
237 |
| - t.pluginMap = pluginMap |
238 |
| - |
239 |
| - t.lock.Unlock() |
240 |
| -} |
241 |
| - |
242 |
| -func (t *PluginTable) GetVersion() string { |
243 |
| - defer t.lock.RUnlock() |
244 |
| - t.lock.RLock() |
245 |
| - return t.version |
246 |
| -} |
247 |
| - |
248 |
| -func (t *PluginTable) GetPluginMap() map[string]bfe_wasmplugin.WasmPlugin { |
249 |
| - defer t.lock.RUnlock() |
250 |
| - t.lock.RLock() |
251 |
| - return t.pluginMap |
252 |
| -} |
253 |
| - |
254 |
| -func (t *PluginTable) GetBeforeLocationRules() RuleList { |
255 |
| - defer t.lock.RUnlock() |
256 |
| - t.lock.RLock() |
257 |
| - return t.beforeLocationRules |
258 |
| -} |
259 |
| - |
260 |
| -func (t *PluginTable) Search(product string) (RuleList, bool) { |
261 |
| - t.lock.RLock() |
262 |
| - productRules := t.productRules |
263 |
| - t.lock.RUnlock() |
264 |
| - |
265 |
| - rules, ok := productRules[product] |
266 |
| - return rules, ok |
267 |
| -} |
268 |
| - |
269 |
| - |
270 |
| -func pluginConfLoad(filename string) (PluginConfFile, error) { |
271 |
| - var conf PluginConfFile |
272 |
| - |
273 |
| - /* open the file */ |
274 |
| - file, err := os.Open(filename) |
275 |
| - |
276 |
| - if err != nil { |
277 |
| - return conf, err |
278 |
| - } |
279 |
| - |
280 |
| - /* decode the file */ |
281 |
| - decoder := json.NewDecoder(file) |
282 |
| - |
283 |
| - err = decoder.Decode(&conf) |
284 |
| - file.Close() |
285 |
| - |
286 |
| - if err != nil { |
287 |
| - return conf, err |
288 |
| - } |
289 |
| - |
290 |
| - return conf, nil |
291 |
| -} |
0 commit comments