From df2bee9204ddb0a88b2b5283a7920646fdf1579c Mon Sep 17 00:00:00 2001 From: Kirill Sysoev Date: Sun, 9 Jun 2024 19:24:41 +0800 Subject: [PATCH 1/3] Refactor request parser to include context parameter --- dispatch/common.go | 8 ++++++-- dispatch/router_dipatcher.go | 2 +- dispatch/router_dipatcher_test.go | 25 +++++++++++++++++-------- examples/echo/main.go | 4 ++-- examples/http_backend/main.go | 4 ++-- examples/passthrough/main.go | 2 +- tests/echo_test.go | 4 ++-- 7 files changed, 31 insertions(+), 18 deletions(-) diff --git a/dispatch/common.go b/dispatch/common.go index b4b8d30..6a39af2 100644 --- a/dispatch/common.go +++ b/dispatch/common.go @@ -1,6 +1,10 @@ package dispatch -import "github.com/ksysoev/wasabi" +import ( + "context" + + "github.com/ksysoev/wasabi" +) // RequestMiddlewere is interface for request middleweres type RequestMiddlewere func(next wasabi.RequestHandler) wasabi.RequestHandler @@ -13,4 +17,4 @@ func (f RequestHandlerFunc) Handle(conn wasabi.Connection, req wasabi.Request) e return f(conn, req) } -type RequestParser func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request +type RequestParser func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request diff --git a/dispatch/router_dipatcher.go b/dispatch/router_dipatcher.go index d8ac17b..d4e01b1 100644 --- a/dispatch/router_dipatcher.go +++ b/dispatch/router_dipatcher.go @@ -44,7 +44,7 @@ func (d *RouterDispatcher) AddBackend(backend wasabi.RequestHandler, routingKeys // determining the appropriate backend, and handling the request using middleware. // If an error occurs during handling, it is logged. func (d *RouterDispatcher) Dispatch(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) { - req := d.parser(conn, msgType, data) + req := d.parser(conn, msgType, data, conn.Context()) if req == nil { return diff --git a/dispatch/router_dipatcher_test.go b/dispatch/router_dipatcher_test.go index 97c7065..b13de0d 100644 --- a/dispatch/router_dipatcher_test.go +++ b/dispatch/router_dipatcher_test.go @@ -1,6 +1,7 @@ package dispatch import ( + "context" "fmt" "testing" @@ -11,7 +12,7 @@ import ( func TestNewRouterDispatcher(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { + parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { return mocks.NewMockRequest(t) } @@ -27,7 +28,7 @@ func TestNewRouterDispatcher(t *testing.T) { } func TestRouterDispatcher_AddBackend(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { + parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { return mocks.NewMockRequest(t) } dispatcher := NewRouterDispatcher(defaultBackend, parser) @@ -62,7 +63,9 @@ func TestRouterDispatcher_DispatchDefaultBackend(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) req := mocks.NewMockRequest(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { return req } + parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + return req + } dispatcher := NewRouterDispatcher(defaultBackend, parser) conn := mocks.NewMockConnection(t) @@ -80,7 +83,9 @@ func TestRouterDispatcher_DispatchDefaultBackend(t *testing.T) { func TestRouterDispatcher_DispatchByRoutingKey(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) req := mocks.NewMockRequest(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { return req } + parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + return req + } dispatcher := NewRouterDispatcher(defaultBackend, parser) conn := mocks.NewMockConnection(t) @@ -99,7 +104,9 @@ func TestRouterDispatcher_DispatchByRoutingKey(t *testing.T) { func TestRouterDispatcher_DispatchWrongRequest(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { return nil } + parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + return nil + } dispatcher := NewRouterDispatcher(defaultBackend, parser) conn := mocks.NewMockConnection(t) @@ -111,7 +118,9 @@ func TestRouterDispatcher_DispatchWrongRequest(t *testing.T) { func TestRouterDispatcher_DispatchErrorHandlingRequest(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) req := mocks.NewMockRequest(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { return req } + parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + return req + } dispatcher := NewRouterDispatcher(defaultBackend, parser) conn := mocks.NewMockConnection(t) @@ -128,7 +137,7 @@ func TestRouterDispatcher_DispatchErrorHandlingRequest(t *testing.T) { } func TestRouterDispatcher_Use(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { + parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { return mocks.NewMockRequest(t) } dispatcher := NewRouterDispatcher(defaultBackend, parser) @@ -149,7 +158,7 @@ func TestRouterDispatcher_UseMiddleware(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) defaultBackend.EXPECT().Handle(mockConn, mockReq).Return(testError) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { + parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { return mocks.NewMockRequest(t) } dispatcher := NewRouterDispatcher(defaultBackend, parser) diff --git a/examples/echo/main.go b/examples/echo/main.go index 6db6aa5..1655780 100644 --- a/examples/echo/main.go +++ b/examples/echo/main.go @@ -25,8 +25,8 @@ func main() { return conn.Send(wasabi.MsgTypeText, req.Data()) }) - dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request { - return dispatch.NewRawRequest(conn.Context(), msgType, data) + dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request { + return dispatch.NewRawRequest(ctx, msgType, data) }) channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*")) diff --git a/examples/http_backend/main.go b/examples/http_backend/main.go index d71bd14..f149710 100644 --- a/examples/http_backend/main.go +++ b/examples/http_backend/main.go @@ -30,8 +30,8 @@ func main() { return httpReq, nil }) - dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request { - return dispatch.NewRawRequest(conn.Context(), msgType, data) + dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request { + return dispatch.NewRawRequest(ctx, msgType, data) }) channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*")) diff --git a/examples/passthrough/main.go b/examples/passthrough/main.go index e5ea65c..099ea32 100644 --- a/examples/passthrough/main.go +++ b/examples/passthrough/main.go @@ -37,7 +37,7 @@ func main() { }, ) - dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request { + dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request { return dispatch.NewRawRequest(conn.Context(), msgType, data) }) channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*")) diff --git a/tests/echo_test.go b/tests/echo_test.go index 3cb881d..6c32837 100644 --- a/tests/echo_test.go +++ b/tests/echo_test.go @@ -22,8 +22,8 @@ func TestEcho(t *testing.T) { return conn.Send(wasabi.MsgTypeText, req.Data()) }) - dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request { - return dispatch.NewRawRequest(conn.Context(), msgType, data) + dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request { + return dispatch.NewRawRequest(ctx, msgType, data) }) ch := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*")) From 154f2f7e542048f639ce5ba8630665c253aca613 Mon Sep 17 00:00:00 2001 From: Kirill Sysoev Date: Sun, 9 Jun 2024 19:34:20 +0800 Subject: [PATCH 2/3] Refactor router dispatcher tests to include context parameter --- dispatch/router_dipatcher_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dispatch/router_dipatcher_test.go b/dispatch/router_dipatcher_test.go index b13de0d..ca8f66e 100644 --- a/dispatch/router_dipatcher_test.go +++ b/dispatch/router_dipatcher_test.go @@ -69,6 +69,8 @@ func TestRouterDispatcher_DispatchDefaultBackend(t *testing.T) { dispatcher := NewRouterDispatcher(defaultBackend, parser) conn := mocks.NewMockConnection(t) + conn.EXPECT().Context().Return(context.Background()) + data := []byte("test data") // Test case 1: Request with existing routing key @@ -89,6 +91,8 @@ func TestRouterDispatcher_DispatchByRoutingKey(t *testing.T) { dispatcher := NewRouterDispatcher(defaultBackend, parser) conn := mocks.NewMockConnection(t) + conn.EXPECT().Context().Return(context.Background()) + data := []byte("test data") // Test case 1: Request with existing routing key @@ -110,6 +114,8 @@ func TestRouterDispatcher_DispatchWrongRequest(t *testing.T) { dispatcher := NewRouterDispatcher(defaultBackend, parser) conn := mocks.NewMockConnection(t) + conn.EXPECT().Context().Return(context.Background()) + data := []byte("test data") dispatcher.Dispatch(conn, wasabi.MsgTypeText, data) @@ -124,6 +130,8 @@ func TestRouterDispatcher_DispatchErrorHandlingRequest(t *testing.T) { dispatcher := NewRouterDispatcher(defaultBackend, parser) conn := mocks.NewMockConnection(t) + conn.EXPECT().Context().Return(context.Background()) + data := []byte("test data") routingKey := "key1" From 0049d3f80c80cd99b3e5b1a74f084010844e438d Mon Sep 17 00:00:00 2001 From: Kirill Sysoev Date: Sun, 9 Jun 2024 19:39:05 +0800 Subject: [PATCH 3/3] Refactor request parser reorder arguments --- dispatch/common.go | 2 +- dispatch/router_dipatcher.go | 2 +- dispatch/router_dipatcher_test.go | 16 ++++++++-------- examples/echo/main.go | 2 +- examples/http_backend/main.go | 2 +- examples/passthrough/main.go | 2 +- tests/echo_test.go | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/dispatch/common.go b/dispatch/common.go index 6a39af2..665d3d7 100644 --- a/dispatch/common.go +++ b/dispatch/common.go @@ -17,4 +17,4 @@ func (f RequestHandlerFunc) Handle(conn wasabi.Connection, req wasabi.Request) e return f(conn, req) } -type RequestParser func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request +type RequestParser func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request diff --git a/dispatch/router_dipatcher.go b/dispatch/router_dipatcher.go index d4e01b1..fe50ad4 100644 --- a/dispatch/router_dipatcher.go +++ b/dispatch/router_dipatcher.go @@ -44,7 +44,7 @@ func (d *RouterDispatcher) AddBackend(backend wasabi.RequestHandler, routingKeys // determining the appropriate backend, and handling the request using middleware. // If an error occurs during handling, it is logged. func (d *RouterDispatcher) Dispatch(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) { - req := d.parser(conn, msgType, data, conn.Context()) + req := d.parser(conn, conn.Context(), msgType, data) if req == nil { return diff --git a/dispatch/router_dipatcher_test.go b/dispatch/router_dipatcher_test.go index ca8f66e..05b8421 100644 --- a/dispatch/router_dipatcher_test.go +++ b/dispatch/router_dipatcher_test.go @@ -12,7 +12,7 @@ import ( func TestNewRouterDispatcher(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request { return mocks.NewMockRequest(t) } @@ -28,7 +28,7 @@ func TestNewRouterDispatcher(t *testing.T) { } func TestRouterDispatcher_AddBackend(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request { return mocks.NewMockRequest(t) } dispatcher := NewRouterDispatcher(defaultBackend, parser) @@ -63,7 +63,7 @@ func TestRouterDispatcher_DispatchDefaultBackend(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) req := mocks.NewMockRequest(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request { return req } dispatcher := NewRouterDispatcher(defaultBackend, parser) @@ -85,7 +85,7 @@ func TestRouterDispatcher_DispatchDefaultBackend(t *testing.T) { func TestRouterDispatcher_DispatchByRoutingKey(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) req := mocks.NewMockRequest(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request { return req } dispatcher := NewRouterDispatcher(defaultBackend, parser) @@ -108,7 +108,7 @@ func TestRouterDispatcher_DispatchByRoutingKey(t *testing.T) { func TestRouterDispatcher_DispatchWrongRequest(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request { return nil } dispatcher := NewRouterDispatcher(defaultBackend, parser) @@ -124,7 +124,7 @@ func TestRouterDispatcher_DispatchWrongRequest(t *testing.T) { func TestRouterDispatcher_DispatchErrorHandlingRequest(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) req := mocks.NewMockRequest(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request { return req } dispatcher := NewRouterDispatcher(defaultBackend, parser) @@ -145,7 +145,7 @@ func TestRouterDispatcher_DispatchErrorHandlingRequest(t *testing.T) { } func TestRouterDispatcher_Use(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request { return mocks.NewMockRequest(t) } dispatcher := NewRouterDispatcher(defaultBackend, parser) @@ -166,7 +166,7 @@ func TestRouterDispatcher_UseMiddleware(t *testing.T) { defaultBackend := mocks.NewMockBackend(t) defaultBackend.EXPECT().Handle(mockConn, mockReq).Return(testError) - parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request { + parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request { return mocks.NewMockRequest(t) } dispatcher := NewRouterDispatcher(defaultBackend, parser) diff --git a/examples/echo/main.go b/examples/echo/main.go index 1655780..7d7b74e 100644 --- a/examples/echo/main.go +++ b/examples/echo/main.go @@ -25,7 +25,7 @@ func main() { return conn.Send(wasabi.MsgTypeText, req.Data()) }) - dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request { + dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request { return dispatch.NewRawRequest(ctx, msgType, data) }) channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*")) diff --git a/examples/http_backend/main.go b/examples/http_backend/main.go index f149710..4325076 100644 --- a/examples/http_backend/main.go +++ b/examples/http_backend/main.go @@ -30,7 +30,7 @@ func main() { return httpReq, nil }) - dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request { + dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request { return dispatch.NewRawRequest(ctx, msgType, data) }) diff --git a/examples/passthrough/main.go b/examples/passthrough/main.go index 099ea32..12f04e8 100644 --- a/examples/passthrough/main.go +++ b/examples/passthrough/main.go @@ -37,7 +37,7 @@ func main() { }, ) - dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request { + dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request { return dispatch.NewRawRequest(conn.Context(), msgType, data) }) channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*")) diff --git a/tests/echo_test.go b/tests/echo_test.go index 6c32837..0234044 100644 --- a/tests/echo_test.go +++ b/tests/echo_test.go @@ -22,7 +22,7 @@ func TestEcho(t *testing.T) { return conn.Send(wasabi.MsgTypeText, req.Data()) }) - dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request { + dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request { return dispatch.NewRawRequest(ctx, msgType, data) }) ch := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))