Skip to content

Commit

Permalink
Brings backs connection registry abstraction, this abstraction will b…
Browse files Browse the repository at this point in the history
…e responsible for managing connection settings
  • Loading branch information
ksysoev committed Apr 24, 2024
1 parent 29c4119 commit 3b610b9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 14 deletions.
1 change: 1 addition & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ packages:
Connection:
Request:
Channel:
ConnectionRegistry:
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ test:
go test -v --race ./...

lint:
golangci-lint run
golangci-lint run

mocks:
mockery --all --keeptree
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ func main() {
dispatcher.Use(ErrHandler)
dispatcher.Use(request.NewTrottlerMiddleware(10))

// We create a new connection registry with channel.NewConnectionRegistry.
// This registry keeps track of all active connections
// and responsible for managing connection's settings.
connRegistry := channel.NewConnectionRegistry()

// We create a new server with wasabi.NewServer and add a channel to it with server.AddChannel.
// The server listens on port 8080 and the channel handles all requests to the / path.
channel := channel.NewChannel("/", dispatcher)
channel := channel.NewChannel("/", dispatcher, connRegistry)
server := server.NewServer(Port)
server.AddChannel(channel)

Expand Down
5 changes: 3 additions & 2 deletions channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type Channel struct {
path string
disptacher wasabi.Dispatcher
connRegistry *ConnectionRegistry
connRegistry wasabi.ConnectionRegistry
ctx context.Context
middlewares []Middlewere
config channelConfig
Expand All @@ -33,6 +33,7 @@ type Option func(*channelConfig)
func NewChannel(
path string,
dispatcher wasabi.Dispatcher,
connRegistry wasabi.ConnectionRegistry,
opts ...Option,
) *Channel {
config := channelConfig{
Expand All @@ -46,7 +47,7 @@ func NewChannel(
return &Channel{
path: path,
disptacher: dispatcher,
connRegistry: NewConnectionRegistry(),
connRegistry: connRegistry,
middlewares: make([]Middlewere, 0),
config: config,
}
Expand Down
18 changes: 9 additions & 9 deletions channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestNewChannel(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

if channel.path != path {
t.Errorf("Unexpected path: got %q, expected %q", channel.path, path)
Expand All @@ -31,7 +31,7 @@ func TestChannel_Path(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

if channel.Path() != path {
t.Errorf("Unexpected path: got %q, expected %q", channel.Path(), path)
Expand All @@ -41,7 +41,7 @@ func TestChannel_Handler(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())
channel.SetContext(context.Background())

// Call the Handler method
Expand All @@ -55,7 +55,7 @@ func TestChannel_SetContext(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

ctx := context.Background()
channel.SetContext(ctx)
Expand All @@ -68,7 +68,7 @@ func TestChannel_Use(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -87,7 +87,7 @@ func TestChannel_wrapMiddleware(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

// Create a mock handler
mockHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestChannel_SetContextMiddleware(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

// Create a mock handler
var ctx context.Context
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestChannel_WithOriginPatterns(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

if len(channel.config.originPatterns) != 1 {
t.Errorf("Unexpected number of origin patterns: got %d, expected %d", len(channel.config.originPatterns), 1)
Expand All @@ -166,7 +166,7 @@ func TestChannel_WithOriginPatterns(t *testing.T) {
t.Errorf("Unexpected to get default origin pattern: got %s, expected %s", channel.config.originPatterns[0], "*")
}

channel = NewChannel(path, dispatcher, WithOriginPatterns("test", "test2"))
channel = NewChannel(path, dispatcher, NewConnectionRegistry(), WithOriginPatterns("test", "test2"))

if len(channel.config.originPatterns) != 2 {
t.Errorf("Unexpected number of origin patterns: got %d, expected %d", len(channel.config.originPatterns), 1)
Expand Down
2 changes: 1 addition & 1 deletion examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
dispatcher.Use(ErrHandler)
dispatcher.Use(request.NewTrottlerMiddleware(100))

channel := channel.NewChannel("/", dispatcher, channel.WithOriginPatterns("*"))
channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

server := server.NewServer(Port)
server.AddChannel(channel)
Expand Down
10 changes: 10 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ type Channel interface {
SetContext(ctx context.Context)
Handler() http.Handler
}

// ConnectionRegistry is interface for connection registries
type ConnectionRegistry interface {
AddConnection(
ctx context.Context,
ws *websocket.Conn,
cb OnMessage,
) Connection
GetConnection(id string) Connection
}

0 comments on commit 3b610b9

Please sign in to comment.