From d3c3c2bfe1ac5f5ac90408840ab490b30069e2ec Mon Sep 17 00:00:00 2001 From: Sodesnei <1452401269@qq.com> Date: Sun, 19 Sep 2021 18:11:43 +0800 Subject: [PATCH] Merge branch 'cache-middle-zlj' --- pkg/middle/Data.go | 24 +++++++++++++++++++ pkg/middle/Middle.go | 36 ++++++++--------------------- pkg/middle/Send.go | 24 +++++++++++++++++++ pkg/middle/Storage.go | 24 +++++++++++++++++++ pkg/middle/{upload.go => Upload.go} | 24 +++++++++++-------- 5 files changed, 96 insertions(+), 36 deletions(-) create mode 100644 pkg/middle/Data.go create mode 100644 pkg/middle/Send.go create mode 100644 pkg/middle/Storage.go rename pkg/middle/{upload.go => Upload.go} (85%) diff --git a/pkg/middle/Data.go b/pkg/middle/Data.go new file mode 100644 index 0000000..18dcc80 --- /dev/null +++ b/pkg/middle/Data.go @@ -0,0 +1,24 @@ +package middle + +// 数据类型 +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 +} diff --git a/pkg/middle/Middle.go b/pkg/middle/Middle.go index c79d7d3..2c0e1a4 100644 --- a/pkg/middle/Middle.go +++ b/pkg/middle/Middle.go @@ -1,36 +1,11 @@ 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 @@ -49,6 +24,7 @@ func NewDriver() *Driver { return new(Driver) } +// 驱动 func (d *Driver) Start(msg Msg) { for _, m := range d.conf { @@ -56,9 +32,9 @@ func (d *Driver) Start(msg Msg) { go m.Middle.Put(msg) msg = m.Middle.Out() - fmt.Println(msg.Get("1")) } + } func main() { @@ -67,6 +43,14 @@ func main() { Weight: 1, Middle: NewUpload(), }, + { + Weight: 2, + Middle: NewStorage(), + }, + { + Weight: 3, + Middle: NewSend(), + }, } da := NewDriver() diff --git a/pkg/middle/Send.go b/pkg/middle/Send.go new file mode 100644 index 0000000..ff93d2d --- /dev/null +++ b/pkg/middle/Send.go @@ -0,0 +1,24 @@ +package middle + +import "fmt" + +type Send struct { + ch chan *data +} + +func NewSend() *Send { + return &Send{ + make(chan *data), + } +} + +func (m *Send) Put(msg Msg) { + da := NewData() + da.Put("1", msg) + fmt.Println("传入Send") + m.ch <- da +} + +func (m *Send) Out() Msg { + return <-m.ch +} diff --git a/pkg/middle/Storage.go b/pkg/middle/Storage.go new file mode 100644 index 0000000..5450ebd --- /dev/null +++ b/pkg/middle/Storage.go @@ -0,0 +1,24 @@ +package middle + +import "fmt" + +type Storage struct { + ch chan *data +} + +func NewStorage() *Storage { + return &Storage{ + make(chan *data), + } +} + +func (m *Storage) Put(msg Msg) { + da := NewData() + da.Put("1", msg) + fmt.Println("传入Storage") + m.ch <- da +} + +func (m *Storage) Out() Msg { + return <-m.ch +} diff --git a/pkg/middle/upload.go b/pkg/middle/Upload.go similarity index 85% rename from pkg/middle/upload.go rename to pkg/middle/Upload.go index 9e0e0e0..0e160c2 100644 --- a/pkg/middle/upload.go +++ b/pkg/middle/Upload.go @@ -1,21 +1,25 @@ package middle +import "fmt" + 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), } } + +func (m *Upload) Put(msg Msg) { + da := NewData() + da.Put("1", msg) + fmt.Println("传入Upload") + + m.ch <- da +} + +func (m *Upload) Out() Msg { + return <-m.ch +}