Skip to content

Commit

Permalink
Port connection info changes to v2 (#433)
Browse files Browse the repository at this point in the history
* feat: add a method to get the number of connected clients (#430)
* Fix static call to session GetNumberOfConnectedClients (#431)
---------

Co-authored-by: Reinaldo Oliveira <reinaldo.oliveira@wildlifestudios.com>
Co-authored-by: Rodrigo Ueda <rodrigo.ueda@wildlifestudios.com>
Co-authored-by: rodrigoueda <rodrigoyueda@gmail.com>
  • Loading branch information
4 people authored Jan 14, 2025
1 parent 3eb2a53 commit be642be
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 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 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 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 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 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 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 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 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()
}
13 changes: 13 additions & 0 deletions static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"errors"
"github.com/topfreegames/pitaya/v2/constants"
"math"
"testing"
"time"

Expand Down Expand Up @@ -922,3 +923,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 be642be

Please sign in to comment.