Compare commits

...

11 Commits

Author SHA1 Message Date
Sodesnei 89ee969ef7 fix(middleware): fix plugins Interface 2021-10-09 18:46:57 +08:00
Sodesnei 67747a5b6a fix(middle): add middle worker function 2021-10-09 18:33:42 +08:00
Sodesnei 12f594b246 feat(middle): add middledriver
loadPlugins function
2021-10-09 18:22:06 +08:00
Sodesnei 60a4a4a3de fix(middle):fix wheat-cache.yaml 2021-10-09 17:55:37 +08:00
Sodesnei dca19238da feat(middle): add middle worker function 2021-10-09 17:52:30 +08:00
Sodesnei 79bef5ba72 feat(middle): add middle worker function 2021-10-09 17:52:30 +08:00
黎白南 f351183ee9 fix(log) fix produce msg, remove more route 2021-10-09 17:52:30 +08:00
黎白南 55f5d7ecd9 fix(log) fix produce msg, remove more route 2021-10-09 17:52:30 +08:00
Sodesnei 06b7c05a6d fix(middle-template): update middle template 2021-10-08 19:58:19 +08:00
Sodesnei e08646b6a1 feat(middleware_driver): add middle driver 2021-10-08 19:58:19 +08:00
Sodesnei 02b048a24f fix(middle-driver):updata middledriver Init() func 2021-10-08 19:58:19 +08:00
13 changed files with 174 additions and 19 deletions

View File

@ -14,8 +14,17 @@ lruCache:
eventDriverSize: 2000
workTime: 1
logPrint:
stath: [
"debug",
"error"
]
middleware-driver:
driverCount: 1000,
middleConsumerCount: 5
plugins-control:
LogContext: ["logMiddle"]

View File

@ -11,7 +11,12 @@ import (
"time"
)
var (
floor = 3
)
func With(ctx context.Context, p event.ProduceInterface) *upLogger {
floor = 4
return &upLogger{
ctx: ctx,
produce: p,
@ -40,7 +45,12 @@ func (l *upLogger) Print(level string, format string, msg ...interface{}) {
Print(level, format, msg...)
eventMiddle := event.NewEvent(middleMsg.EventNameLog)
eventMiddle.SetValue(middleMsg.EventKeyLog, middleMsg.LogContext{})
eventMiddle.SetValue(middleMsg.EventKeyLog, middleMsg.LogContext{
Level: level,
Data: time.Now(),
Msg: fmt.Sprintf(format, msg...),
Route: findPlace(),
})
l.produce.Call(l.ctx, eventMiddle)
}
@ -80,7 +90,7 @@ func findPlace() string {
var (
place string
i = 0
i = floor
)
for {

View File

@ -3,7 +3,7 @@ package middle_msg
import "time"
var (
EventNameLog = "LogContext"
EventNameLog = "logcontext"
EventKeyLog = "LogContext"
)

View File

@ -1,7 +1,17 @@
package middle
import getMiddlewareMap "gitee.com/timedb/wheatCache/plugins/config"
import (
"sync"
func Init() {
getMiddlewareMap.GetMiddlewareMap()
"gitee.com/timedb/wheatCache/plugins"
getMiddlewareMap "gitee.com/timedb/wheatCache/plugins/config"
)
func Init() map[string]plugins.MiddleToolsInterface {
return getMiddlewareMap.GetMiddlewareMap()
}
var (
oneMiddle sync.Once
MiddleWareDriver *MiddleWare
)

View File

@ -0,0 +1,52 @@
package middle
import (
_ "gitee.com/timedb/wheatCache/conf"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/plugins"
pcg "gitee.com/timedb/wheatCache/plugins/config"
"github.com/spf13/viper"
)
type MiddleWare struct {
eventDriver event.DriverInterface
eventConsumer event.ConsumerInterface
plugins map[string][]plugins.MiddleToolsInterface
consumerCount int
}
func NewMiddleWare() *MiddleWare {
oneMiddle.Do(func() {
driver := event.NewDriver(1000)
MiddleWareDriver = &MiddleWare{
eventDriver: driver,
eventConsumer: event.NewConsumer(driver),
}
MiddleWareDriver.loadPlugins()
})
return MiddleWareDriver
}
func (m *MiddleWare) GetEventDriver() event.DriverInterface {
return m.eventDriver
}
func (m *MiddleWare) loadPlugins() {
plug := viper.GetStringMapStringSlice("plugins-control")
pluginsMap := pcg.GetMiddlewareMap()
pluginsContext := make(map[string][]plugins.MiddleToolsInterface)
for middleMsg, pluNames := range plug {
pulgSingle := make([]plugins.MiddleToolsInterface, 0)
for _, name := range pluNames {
pulgSingle = append(pulgSingle, pluginsMap[name])
}
pluginsContext[middleMsg] = pulgSingle
}
m.plugins = pluginsContext
}

View File

@ -0,0 +1,12 @@
package middle
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_middleware_driver(t *testing.T) {
middleware := NewMiddleWare()
require.Equal(t, middleware.plugins["logcontext"][0].Name(), "logMiddle")
}

26
pkg/middle/worker.go Normal file
View File

@ -0,0 +1,26 @@
package middle
import (
"context"
msg "gitee.com/timedb/wheatCache/pkg/middle-msg"
)
func (m *MiddleWare) middleWorker() {
ctx := context.Background()
workEvent := m.eventConsumer.Receive(ctx)
m.loadPlugins()
switch workEvent.GetEventName() {
case msg.EventNameLog:
for middleMsg, singles := range m.plugins {
if middleMsg == msg.EventNameLog {
for _, single := range singles {
single.Exec(workEvent)
}
}
}
}
}

17
pkg/middle/worker_test.go Normal file
View File

@ -0,0 +1,17 @@
package middle
import (
"testing"
"gitee.com/timedb/wheatCache/pkg/event"
)
func TestWorker(t *testing.T) {
event := event.NewEvent("logcontext")
m := NewMiddleWare()
m.eventDriver.Put(event)
m.middleWorker()
}

View File

@ -12,9 +12,11 @@ import (
func GetMiddlewareMap() map[string]plugins.MiddleToolsInterface {
logMiddle := logMiddle.NewMiddleware()
mapKey := mapKey.NewMiddleware()
return map[string]plugins.MiddleToolsInterface{
"logMiddle": logMiddle.NewMiddleware(),
"mapKey": mapKey.NewMiddleware(),
logMiddle.Name(): logMiddle,
mapKey.Name(): mapKey,
}
}

View File

@ -12,10 +12,12 @@ import (
func GetMiddlewareMap() map[string]plugins.MiddleToolsInterface {
{%for dir in dirs %}
{{dir[0]}}:={{dir[0]}}.NewMiddleware()
{%- endfor%}
return map[string]plugins.MiddleToolsInterface{
{%for dir in dirs %}
"{{dir[0]}}":{{dir[0]}}.NewMiddleware(),
{{dir[0]}}.Name():{{dir[0]}},
{%- endfor%}
}
}

View File

@ -1,7 +1,10 @@
package plugins
import "gitee.com/timedb/wheatCache/pkg/event"
type MiddleToolsInterface interface {
Init() // 初始化
Exec(interface{}) (interface{}, error) // 处理用户发送事件
Exec(e *event.Event) (*event.Event, error) // 处理用户发送事件
Name() string // 获取中间件名称
Describe() string // 描述
}

View File

@ -3,6 +3,7 @@ package log_middle
import (
"fmt"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/plugins"
)
@ -12,15 +13,19 @@ type logMiddle struct {
func (i *logMiddle) Init() {
}
func (i *logMiddle) Exec(interface{}) (interface{}, error) {
fmt.Println(1)
return nil, nil
func (i *logMiddle) Exec(e *event.Event) (*event.Event, error) {
fmt.Println(e.GetEventName())
return e, nil
}
func (i *logMiddle) Name() string {
return "logMiddle"
}
func (i *logMiddle) Describe() string {
return ""
}
func NewMiddleware() plugins.MiddleToolsInterface {
return &logMiddle{}
}

View File

@ -1,6 +1,9 @@
package log_middle
import (
"fmt"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/plugins"
)
@ -10,15 +13,19 @@ type mapKey struct {
func (i *mapKey) Init() {
}
func (i *mapKey) Exec(interface{}) (interface{}, error) {
return nil, nil
func (i *mapKey) Exec(e *event.Event) (*event.Event, error) {
fmt.Println(e.GetEventName())
return e, nil
}
func (i *mapKey) Name() string {
return "mapKey"
}
func (i *mapKey) Describe() string {
return ""
}
func NewMiddleware() plugins.MiddleToolsInterface {
return &mapKey{}
}