中间件

This commit is contained in:
86150 2021-08-30 14:46:31 +08:00
parent 8b676d1ad2
commit 2c6bd99ccc
5 changed files with 225 additions and 30 deletions

119
pkg/interface.go Normal file
View File

@ -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{}
}

View File

@ -1,30 +0,0 @@
package middle
type msg struct {
msgType map[string]interface{}
}
//获取权值
func GetMiddleWeight() {
}
//弹出中间件
func PopMiddle() {
}
//输入数据
func GetData() {
}
//输出数据
func OutData() {
}
//驱动
func Driver() {
}

98
pkg/middle/Middle.go Normal file
View File

@ -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)
}

View File

@ -0,0 +1,7 @@
package middle
import "testing"
func TestMiddle(t *testing.T) {
main()
}

1
pkg/middle/upload.go Normal file
View File

@ -0,0 +1 @@
package middle