forked from p53841790/wheat-cache
Compare commits
11 Commits
master
...
feat-middl
Author | SHA1 | Date |
---|---|---|
Sodesnei | 89ee969ef7 | |
Sodesnei | 67747a5b6a | |
Sodesnei | 12f594b246 | |
Sodesnei | 60a4a4a3de | |
Sodesnei | dca19238da | |
Sodesnei | 79bef5ba72 | |
黎白南 | f351183ee9 | |
黎白南 | 55f5d7ecd9 | |
Sodesnei | 06b7c05a6d | |
Sodesnei | e08646b6a1 | |
Sodesnei | 02b048a24f |
|
@ -14,8 +14,17 @@ lruCache:
|
|||
eventDriverSize: 2000
|
||||
workTime: 1
|
||||
|
||||
|
||||
logPrint:
|
||||
stath: [
|
||||
"debug",
|
||||
"error"
|
||||
]
|
||||
|
||||
middleware-driver:
|
||||
driverCount: 1000,
|
||||
middleConsumerCount: 5
|
||||
|
||||
plugins-control:
|
||||
LogContext: ["logMiddle"]
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -3,7 +3,7 @@ package middle_msg
|
|||
import "time"
|
||||
|
||||
var (
|
||||
EventNameLog = "LogContext"
|
||||
EventNameLog = "logcontext"
|
||||
|
||||
EventKeyLog = "LogContext"
|
||||
)
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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")
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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()
|
||||
|
||||
}
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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%}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,10 @@
|
|||
package plugins
|
||||
|
||||
import "gitee.com/timedb/wheatCache/pkg/event"
|
||||
|
||||
type MiddleToolsInterface interface {
|
||||
Init() // 初始化
|
||||
Exec(interface{}) (interface{}, error) // 处理用户发送事件
|
||||
Name() string // 获取中间件名称
|
||||
Init() // 初始化
|
||||
Exec(e *event.Event) (*event.Event, error) // 处理用户发送事件
|
||||
Name() string // 获取中间件名称
|
||||
Describe() string // 描述
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue