-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: wasm module hot reload Co-authored-by: seeflood <349895584@qq.com>
- Loading branch information
Showing
4 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package wasm | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
v2 "mosn.io/mosn/pkg/config/v2" | ||
"mosn.io/mosn/pkg/log" | ||
"mosn.io/mosn/pkg/wasm" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
) | ||
|
||
var ( | ||
watcher *fsnotify.Watcher | ||
configs = make(map[string]*filterConfig) | ||
pluginNames = make(map[string]string) | ||
) | ||
|
||
func init() { | ||
var err error | ||
watcher, err = fsnotify.NewWatcher() | ||
if err != nil { | ||
log.DefaultLogger.Errorf("[proxywasm] [watcher] init fail to create watcher: %v", err) | ||
return | ||
} | ||
go runWatcher() | ||
} | ||
|
||
func runWatcher() { | ||
for { | ||
select { | ||
case event, ok := <-watcher.Events: | ||
if !ok { | ||
log.DefaultLogger.Errorf("[proxywasm] [watcher] runWatcher exit") | ||
return | ||
} | ||
log.DefaultLogger.Debugf("[proxywasm] [watcher] runWatcher got event, %s", event) | ||
|
||
if pathIsWasmFile(event.Name) { | ||
if event.Op&fsnotify.Chmod == fsnotify.Chmod || | ||
event.Op&fsnotify.Rename == fsnotify.Rename { | ||
continue | ||
} else if event.Op&fsnotify.Remove == fsnotify.Remove { | ||
// rewatch the file if it exists | ||
// remove this file then nename other file to this name will cause this case | ||
if fileExist(event.Name) { | ||
_ = watcher.Add(event.Name) | ||
} | ||
continue | ||
} else if event.Op&fsnotify.Create == fsnotify.Create { | ||
if fileExist(event.Name) { | ||
_ = watcher.Add(event.Name) | ||
} | ||
} | ||
reloadWasm(event.Name) | ||
} | ||
case err, ok := <-watcher.Errors: | ||
if !ok { | ||
log.DefaultLogger.Errorf("[proxywasm] [watcher] runWatcher exit") | ||
return | ||
} | ||
log.DefaultLogger.Errorf("[proxywasm] [watcher] runWatcher got errors, err: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func addWatchFile(cfg *filterConfig, pluginName string) { | ||
path := cfg.VmConfig.Path | ||
if err := watcher.Add(path); err != nil { | ||
log.DefaultLogger.Errorf("[proxywasm] [watcher] addWatchFile fail to watch wasm file, err: %v", err) | ||
return | ||
} | ||
|
||
dir := filepath.Dir(path) | ||
if err := watcher.Add(dir); err != nil { | ||
log.DefaultLogger.Errorf("[proxywasm] [watcher] addWatchFile fail to watch wasm dir, err: %v", err) | ||
return | ||
} | ||
|
||
configs[path] = cfg | ||
pluginNames[path] = pluginName | ||
log.DefaultLogger.Infof("[proxywasm] [watcher] addWatchFile start to watch wasm file and its dir: %s", path) | ||
} | ||
|
||
func reloadWasm(fullPath string) { | ||
found := false | ||
|
||
for path, config := range configs { | ||
if strings.HasSuffix(fullPath, path) { | ||
found = true | ||
pluginName := pluginNames[path] | ||
|
||
err := wasm.GetWasmManager().UninstallWasmPluginByName(pluginName) | ||
if err != nil { | ||
log.DefaultLogger.Errorf("[proxywasm] [watcher] reloadWasm fail to uninstall plugin, err: %v", err) | ||
} | ||
|
||
v2Config := v2.WasmPluginConfig{ | ||
PluginName: pluginName, | ||
VmConfig: config.VmConfig, | ||
InstanceNum: config.InstanceNum, | ||
} | ||
err = wasm.GetWasmManager().AddOrUpdateWasm(v2Config) | ||
if err != nil { | ||
log.DefaultLogger.Errorf("[proxywasm] [watcher] reloadWasm fail to add plugin, err: %v", err) | ||
return | ||
} | ||
|
||
pw := wasm.GetWasmManager().GetWasmPluginWrapperByName(pluginName) | ||
if pw == nil { | ||
log.DefaultLogger.Errorf("[proxywasm] [watcher] reloadWasm plugin not found") | ||
return | ||
} | ||
|
||
factory := &FilterConfigFactory{ | ||
pluginName: pluginName, | ||
config: config, | ||
} | ||
pw.RegisterPluginHandler(factory) | ||
|
||
log.DefaultLogger.Infof("[proxywasm] [watcher] reloadWasm reload wasm success: %s", path) | ||
} | ||
} | ||
|
||
if !found { | ||
log.DefaultLogger.Errorf("[proxywasm] [watcher] reloadWasm WasmPluginConfig not found: %s", fullPath) | ||
} | ||
} | ||
|
||
func fileExist(file string) bool { | ||
_, err := os.Stat(file) | ||
if err != nil && !os.IsExist(err) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func pathIsWasmFile(fullPath string) bool { | ||
for path, _ := range configs { | ||
if strings.HasSuffix(fullPath, path) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |