From 2c6bd99cccc0d11bd793e2779c4b4f9037cbae6e Mon Sep 17 00:00:00 2001 From: 86150 <1452401269@qq.com> Date: Mon, 30 Aug 2021 14:46:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/interface.go | 119 ++++++++++++++++++++++++++++++++++++++ pkg/middle/MIDDLE.go | 30 ---------- pkg/middle/Middle.go | 98 +++++++++++++++++++++++++++++++ pkg/middle/Middle_test.go | 7 +++ pkg/middle/upload.go | 1 + 5 files changed, 225 insertions(+), 30 deletions(-) create mode 100644 pkg/interface.go delete mode 100644 pkg/middle/MIDDLE.go create mode 100644 pkg/middle/Middle.go create mode 100644 pkg/middle/Middle_test.go create mode 100644 pkg/middle/upload.go diff --git a/pkg/interface.go b/pkg/interface.go new file mode 100644 index 0000000..c165027 --- /dev/null +++ b/pkg/interface.go @@ -0,0 +1,119 @@ +package pkg + +import "time" + +type Value interface { + Get(key string) interface{} + Put(key string, val interface{}) error +} + +type Data struct { + m map[string]interface{} +} + +func (d *Data) Get(key string) interface{} { + return d.m[key] +} + +func (d *Data) Put(key string, val interface{}) error { + d.m[key] = val + return nil +} + +func NewData() *Data { + return &Data{ + m: make(map[string]interface{}), + } +} + +type MiddlewareConf struct { + Weight int + Middle Middleware + IsOut bool +} + +type Driver struct { + conf []*MiddlewareConf +} + +func (d *Driver) Stat(value Value) error { + for _, m := range d.conf { + m.Middle.Put(value) // 协程 + value = m.Middle.Out() + } + + return nil +} + +func NewDriver() *Driver { + return new(Driver) +} + +func main() { + conf := []*MiddlewareConf{ + { + Weight: 1, + Middle: NewM1(), + IsOut: false, + }, + { + Weight: 2, + Middle: NewM2(), + IsOut: true, + }, + } + + driver := NewDriver() + driver.conf = conf + + // 调用中间件驱动 + val := NewData() + val.Put("2", 2) + + driver.Stat(val) + +} + +type Middleware interface { + Put(val Value) + Out() Value +} + +type M1 struct { + ch chan *Data +} + +func (m *M1) Put(val Value) { + time.Sleep(200 * time.Second) + da := NewData() + da.Put("1", val) + m.ch <- da +} + +func (m *M1) Out() Value { + return <-m.ch +} + +func NewM1() *M1 { + return &M1{ + make(chan *Data), + } +} + +type M2 struct { + da Value +} + +func (m *M2) Put(val Value) { + da := NewData() + m.da = da +} + +func (m *M2) Out() Value { + m.da.Put("2", "plp") + return m.da +} + +func NewM2() *M2 { + return &M2{} +} diff --git a/pkg/middle/MIDDLE.go b/pkg/middle/MIDDLE.go deleted file mode 100644 index 8df26c6..0000000 --- a/pkg/middle/MIDDLE.go +++ /dev/null @@ -1,30 +0,0 @@ -package middle - -type msg struct { - msgType map[string]interface{} -} - -//获取权值 -func GetMiddleWeight() { - -} - -//弹出中间件 -func PopMiddle() { - -} - -//输入数据 -func GetData() { - -} - -//输出数据 -func OutData() { - -} - -//驱动 -func Driver() { - -} diff --git a/pkg/middle/Middle.go b/pkg/middle/Middle.go new file mode 100644 index 0000000..d70117a --- /dev/null +++ b/pkg/middle/Middle.go @@ -0,0 +1,98 @@ +package middle + +import "fmt" + +// 获取数据的接口 +type Msg interface { + Put(key string, val interface{}) error + Get(key string) interface{} +} + +// 数据类型 +type data struct { + m map[string]interface{} +} + +// 数据初始化,分配内存 +func NewData() *data { + return &data{ + make(map[string]interface{}), + } +} + +// 获取数据 +func (d *data) Get(key string) interface{} { + return d.m[key] +} + +// 推入数据 +func (d *data) Put(key string, val interface{}) error { + d.m[key] = val + return nil +} + +type middleWare interface { + Put(msg Msg) + Out() Msg +} + +type Upload struct { + ch chan *data +} + +func (m *Upload) Put(msg Msg) { + da := NewData() + da.Put("1", msg) + m.ch <- da +} + +func (m *Upload) Out() Msg { + return <-m.ch +} + +func NewUpload() *Upload { + return &Upload{ + make(chan *data), + } +} + +type MiddlewareConf struct { + Weight int + Middle middleWare +} + +type Driver struct { + conf []*MiddlewareConf +} + +func NewDriver() *Driver { + return new(Driver) +} + +func (d *Driver) Start(msg Msg) { + + for _, m := range d.conf { + + go m.Middle.Put(msg) + + msg = m.Middle.Out() + fmt.Println(msg.Get("1")) + + } +} + +func main() { + conf := []*MiddlewareConf{ + { + Weight: 1, + Middle: NewUpload(), + }, + } + + da := NewDriver() + da.conf = conf + val := NewData() + val.Put("200", 2) + + da.Start(val) +} diff --git a/pkg/middle/Middle_test.go b/pkg/middle/Middle_test.go new file mode 100644 index 0000000..79f255a --- /dev/null +++ b/pkg/middle/Middle_test.go @@ -0,0 +1,7 @@ +package middle + +import "testing" + +func TestMiddle(t *testing.T) { + main() +} diff --git a/pkg/middle/upload.go b/pkg/middle/upload.go new file mode 100644 index 0000000..94eaada --- /dev/null +++ b/pkg/middle/upload.go @@ -0,0 +1 @@ +package middle