-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware/request): Add a feature about supporting event distri…
…bution middleware. Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
- Loading branch information
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |