Skip to content

Commit

Permalink
Port connection info changes to v3 (#433) (#435)
Browse files Browse the repository at this point in the history
* feat(nats): add compression config (#432)

In NATS client and server, if the connection uses websocket we can enable
compression by passing the Compression config - it needs to be enbaled on
both client and server conn setup. Thus creating a new config to enbale
on both sides - if not websocket, this config has no effect

* Port connection info changes to v2 (#433)
  • Loading branch information
hspedro authored Jan 14, 2025
1 parent 2d40833 commit 07c6331
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ type Pitaya interface {
RegisterModuleAfter(module interfaces.Module, name string) error
RegisterModuleBefore(module interfaces.Module, name string) error
GetModule(name string) (interfaces.Module, error)

GetNumberOfConnectedClients() int64
}

// App is the base app struct
Expand Down Expand Up @@ -553,3 +555,8 @@ func (app *App) RegisterRPCJob(rpcJob worker.RPCJob) error {
err := app.worker.RegisterRPCJob(rpcJob)
return err
}

// GetNumberOfConnectedClients returns the number of connected clients
func (app *App) GetNumberOfConnectedClients() int64 {
return app.sessionPool.GetSessionCount()
}
14 changes: 14 additions & 0 deletions pkg/mocks/app.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pkg/session/mocks/session.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type SessionPool interface {
OnSessionClose(f func(s Session))
CloseAll()
AddHandshakeValidator(name string, f func(data *HandshakeData) error)
GetNumberOfConnectedClients() int64
}

// HandshakeClientData represents information about the client sent on the handshake.
Expand Down Expand Up @@ -310,6 +311,11 @@ func (pool *sessionPoolImpl) AddHandshakeValidator(name string, f func(data *Han
pool.handshakeValidators[name] = f
}

// GetNumberOfConnectedClients returns the number of connected clients
func (pool *sessionPoolImpl) GetNumberOfConnectedClients() int64 {
return pool.GetSessionCount()
}

func (s *sessionImpl) updateEncodedData() error {
var b []byte
b, err := json.Marshal(s.data)
Expand Down
15 changes: 15 additions & 0 deletions pkg/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1507,3 +1507,18 @@ func TestSessionValidateHandshake(t *testing.T) {
})
}
}

func TestGetNumberOfConnectedClients(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

entity := mocks.NewMockNetworkEntity(ctrl)
sessionPool := NewSessionPool()
connections := sessionPool.GetNumberOfConnectedClients()
assert.Equal(t, int64(0), connections)

ss := sessionPool.NewSession(entity, true)
assert.NotNil(t, ss)
connections = sessionPool.GetNumberOfConnectedClients()
assert.Equal(t, int64(1), connections)
}
5 changes: 5 additions & 0 deletions pkg/session/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ func OnSessionClose(f func(s Session)) {
func CloseAll() {
DefaultSessionPool.CloseAll()
}

// GetNumberOfConnectedClients returns the number of connected clients
func GetNumberOfConnectedClients() int64 {
return DefaultSessionPool.GetNumberOfConnectedClients()
}
13 changes: 13 additions & 0 deletions pkg/session/test/static_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"math"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -115,3 +116,15 @@ func TestStaticCloseAll(t *testing.T) {
session.DefaultSessionPool = sessionPool
session.CloseAll()
}

func TestGetNumberOfConnectedClients(t *testing.T) {
ctrl := gomock.NewController(t)

expected := int64(math.MaxInt64)
sessionPool := mocks.NewMockSessionPool(ctrl)
sessionPool.EXPECT().GetNumberOfConnectedClients().Return(expected)

session.DefaultSessionPool = sessionPool
numberOfConnections := session.GetNumberOfConnectedClients()
require.Equal(t, expected, numberOfConnections)
}
4 changes: 4 additions & 0 deletions pkg/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,7 @@ func RegisterModuleBefore(module interfaces.Module, name string) error {
func GetModule(name string) (interfaces.Module, error) {
return DefaultApp.GetModule(name)
}

func GetNumberOfConnectedClients() int64 {
return DefaultApp.GetNumberOfConnectedClients()
}
16 changes: 15 additions & 1 deletion pkg/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ package pitaya
import (
"context"
"errors"
"github.com/topfreegames/pitaya/v3/pkg/constants"
"math"
"testing"
"time"

"github.com/topfreegames/pitaya/v3/pkg/constants"

"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto"
"github.com/google/uuid"
Expand Down Expand Up @@ -922,3 +924,15 @@ func TestStaticGetModule(t *testing.T) {
})
}
}

func TestGetNumberOfConnectedClients(t *testing.T) {
ctrl := gomock.NewController(t)

expected := int64(math.MaxInt64)

app := mocks.NewMockPitaya(ctrl)
app.EXPECT().GetNumberOfConnectedClients().Return(expected)

DefaultApp = app
require.Equal(t, expected, GetNumberOfConnectedClients())
}

0 comments on commit 07c6331

Please sign in to comment.