Merge pull request !77 from bandl/feat-pporf-plugins
This commit is contained in:
bandl 2021-10-25 07:49:21 +00:00 committed by Gitee
commit 2a69f393e5
5 changed files with 42 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package conf
import ( import (
"log" "log"
"sync"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -10,7 +11,10 @@ const (
linuxPath = "/etc/wheat-cache/" linuxPath = "/etc/wheat-cache/"
) )
var confLock sync.Once
func init() { func init() {
confLock.Do(func() {
setDefaultConfValue() setDefaultConfValue()
err := LoadConf("") err := LoadConf("")
switch err.(type) { switch err.(type) {
@ -22,6 +26,8 @@ func init() {
log.Fatalf("the resolution of the profile failed, err: %v", err) log.Fatalf("the resolution of the profile failed, err: %v", err)
} }
},
)
} }
func setDefaultConfValue() { func setDefaultConfValue() {

View File

@ -49,3 +49,5 @@ gateway:
port: 5891 port: 5891
target: ["127.0.0.1:5890"] target: ["127.0.0.1:5890"]
mock-plugin:
pprof-addr: "127.0.0.1:8000"

View File

@ -12,8 +12,9 @@ import (
func GetMiddlewareMap() map[string]plugins.PluginInterface { func GetMiddlewareMap() map[string]plugins.PluginInterface {
mockPlugin := mockPlugin.NewPlugin() mockPlugin := mockPlugin.NewPlugin()
return map[string]plugins.PluginInterface{ mockPlugin.Init()
return map[string]plugins.PluginInterface{
mockPlugin.Name(): mockPlugin, mockPlugin.Name(): mockPlugin,
} }
} }

View File

@ -14,9 +14,10 @@ import (
func GetMiddlewareMap() map[string]plugins.PluginInterface { func GetMiddlewareMap() map[string]plugins.PluginInterface {
{%for dir in dirs %} {%for dir in dirs %}
{{dir[0]}}:={{dir[0]}}.NewPlugin() {{dir[0]}}:={{dir[0]}}.NewPlugin()
{%- endfor%} {{dir[0]}}.Init()
{% endfor%}
return map[string]plugins.PluginInterface{ return map[string]plugins.PluginInterface{
{%for dir in dirs %} {%for dir in dirs -%}
{{dir[0]}}.Name():{{dir[0]}}, {{dir[0]}}.Name():{{dir[0]}},
{%- endfor%} {%- endfor%}
} }

View File

@ -1,11 +1,27 @@
package mockplugin package mockplugin
import "fmt" import (
"fmt"
"net/http"
_ "net/http/pprof"
_ "gitee.com/timedb/wheatCache/conf"
"gitee.com/timedb/wheatCache/pkg/logx"
"github.com/spf13/viper"
)
type MockPlugin struct { type MockPlugin struct {
} }
func (m *MockPlugin) Init() { func (m *MockPlugin) Init() {
addr := viper.GetString("mock-plugin.pprof-addr")
if addr != "" {
go func() {
logx.Debugln(http.ListenAndServe(addr, nil))
}()
}
logx.Debug("mock-plugins status: start")
} }
func (m *MockPlugin) Exec(msg interface{}) (interface{}, error) { func (m *MockPlugin) Exec(msg interface{}) (interface{}, error) {