Skip to content

Commit

Permalink
Refactor request parser to include context parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Jun 9, 2024
1 parent 43e7c02 commit df2bee9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 18 deletions.
8 changes: 6 additions & 2 deletions dispatch/common.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion dispatch/router_dipatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions dispatch/router_dipatcher_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dispatch

import (
"context"
"fmt"
"testing"

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

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("*"))

Expand Down
4 changes: 2 additions & 2 deletions examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("*"))
Expand Down
2 changes: 1 addition & 1 deletion examples/passthrough/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("*"))
Expand Down
4 changes: 2 additions & 2 deletions tests/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("*"))

Expand Down

0 comments on commit df2bee9

Please sign in to comment.