Skip to content

Commit

Permalink
feat(middleware/request): Add a feature about supporting event distri…
Browse files Browse the repository at this point in the history
…bution middleware.

Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
  • Loading branch information
flc1125 committed Feb 17, 2024
1 parent d603a9c commit 0d8c776
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
65 changes: 65 additions & 0 deletions middleware/request/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package request

import (
"context"

"github.com/go-kratos/kratos/v2/middleware"

"github.com/go-kratos-ecosystem/components/v2/event"
)

const (
BeforeName = "request.before"
AfterName = "request.after"
)

type From string

const (
FromClient From = "client"
FromServer From = "server"
)

type BeforeEvent struct {
Ctx context.Context
Req interface{}
From From
}

type AfterEvent struct {
Ctx context.Context
Req interface{}
Reply interface{}
Err error
From From
}

func Server(d *event.Dispatcher) middleware.Middleware {
return newMiddleware(d, FromServer)
}

func Client(d *event.Dispatcher) middleware.Middleware {
return newMiddleware(d, FromClient)
}

func newMiddleware(d *event.Dispatcher, from From) middleware.Middleware {
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
if d != nil {
d.Dispatch(BeforeName, &BeforeEvent{
Ctx: ctx, Req: req, From: from,
})
}

reply, err = handler(ctx, req)

if d != nil {
d.Dispatch(AfterName, &AfterEvent{
Ctx: ctx, Req: req, Reply: reply, Err: err, From: from,
})
}

return
}
}
}
66 changes: 66 additions & 0 deletions middleware/request/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package request

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/go-kratos-ecosystem/components/v2/event"
)

var c = make(chan From, 4)

type listener struct {
t *testing.T
}

var _ event.Listener = (*listener)(nil)

func (l *listener) Listen() []event.Event {
return []event.Event{
BeforeName,
AfterName,
}
}

func (l *listener) Handle(event event.Event, data interface{}) {
switch e := data.(type) {
case *BeforeEvent:
assert.Equal(l.t, BeforeName, event.String())
assert.Equal(l.t, e.Req, "req")
assert.Equal(l.t, e.Ctx, context.Background())
c <- e.From
case *AfterEvent:
assert.Equal(l.t, AfterName, event.String())
assert.Equal(l.t, e.Req, "req")
assert.Equal(l.t, e.Reply, "reply")
assert.Nil(l.t, e.Err)
assert.Equal(l.t, e.Ctx, context.Background())
c <- e.From
}
}

func TestRequest(t *testing.T) {
var (
d = event.NewDispatcher()
handler = func(_ context.Context, req interface{}) (reply interface{}, err error) {
assert.Equal(t, "req", req)
return "reply", nil
}
)

d.AddListener(&listener{t: t})

reply, err := Server(d)(handler)(context.Background(), "req")
assert.Equal(t, "reply", reply)
assert.Nil(t, err)
assert.Equal(t, FromServer, <-c)
assert.Equal(t, FromServer, <-c)

reply, err = Client(d)(handler)(context.Background(), "req")
assert.Equal(t, "reply", reply)
assert.Nil(t, err)
assert.Equal(t, FromClient, <-c)
assert.Equal(t, FromClient, <-c)
}

0 comments on commit 0d8c776

Please sign in to comment.