Skip to content

Commit

Permalink
Create Pitaya interface and remove static methods and app instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Felippe Durán committed Jul 8, 2020
1 parent 9b5df35 commit f773c36
Show file tree
Hide file tree
Showing 34 changed files with 1,066 additions and 432 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ test-coverage-func coverage-func: test-coverage merge-profiles
@echo "\033[1;34m=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\033[0m"
@go tool cover -func=coverage-all.out | egrep -v "100.0[%]"

pitaya-mock:
@mockgen github.com/topfreegames/pitaya Pitaya | sed 's/mock_pitaya/mocks/' > mocks/app.go

serializer-mock:
@mockgen github.com/topfreegames/pitaya/serialize Serializer | sed 's/mock_serialize/mocks/' > serialize/mocks/serializer.go

Expand Down
5 changes: 4 additions & 1 deletion acceptorwrapper/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
// be prepared to handle it.
type RateLimiter struct {
acceptor.PlayerConn
app pitaya.Pitaya
limit int
interval time.Duration
times list.List
Expand All @@ -50,13 +51,15 @@ type RateLimiter struct {

// NewRateLimiter returns an initialized *RateLimiting
func NewRateLimiter(
app pitaya.Pitaya,
conn acceptor.PlayerConn,
limit int,
interval time.Duration,
forceDisable bool,
) *RateLimiter {
r := &RateLimiter{
PlayerConn: conn,
app: app,
limit: limit,
interval: interval,
forceDisable: forceDisable,
Expand All @@ -82,7 +85,7 @@ func (r *RateLimiter) GetNextMessage() (msg []byte, err error) {
now := time.Now()
if r.shouldRateLimit(now) {
logger.Log.Errorf("Data=%s, Error=%s", msg, constants.ErrRateLimitExceeded)
metrics.ReportExceededRateLimiting(pitaya.GetMetricsReporters())
metrics.ReportExceededRateLimiting(r.app.GetMetricsReporters())
continue
}

Expand Down
10 changes: 8 additions & 2 deletions acceptorwrapper/rate_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/topfreegames/pitaya/metrics"
"github.com/topfreegames/pitaya/mocks"
)

Expand Down Expand Up @@ -106,8 +107,10 @@ func TestRateLimiterGetNextMessage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockConn = mocks.NewMockPlayerConn(ctrl)
mockedApp := mocks.NewMockPitaya(ctrl)
mockedApp.EXPECT().GetMetricsReporters().Return([]metrics.Reporter{}).AnyTimes()

r = NewRateLimiter(mockConn, limit, interval, table.forceDisable)
r = NewRateLimiter(mockedApp, mockConn, limit, interval, table.forceDisable)

table.mock()
buf, err := r.GetNextMessage()
Expand Down Expand Up @@ -162,7 +165,10 @@ func TestRateLimiterShouldRateLimit(t *testing.T) {

for name, table := range tables {
t.Run(name, func(t *testing.T) {
r = NewRateLimiter(nil, limit, interval, false)
ctrl := gomock.NewController(t)
mockedApp := mocks.NewMockPitaya(ctrl)
mockedApp.EXPECT().GetMetricsReporters().Return([]metrics.Reporter{}).AnyTimes()
r = NewRateLimiter(mockedApp, nil, limit, interval, false)

table.before()
should := r.shouldRateLimit(now)
Expand Down
5 changes: 3 additions & 2 deletions acceptorwrapper/rate_limiting_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package acceptorwrapper

import (
"github.com/topfreegames/pitaya"
"github.com/topfreegames/pitaya/acceptor"
"github.com/topfreegames/pitaya/config"
)
Expand All @@ -32,7 +33,7 @@ type RateLimitingWrapper struct {
}

// NewRateLimitingWrapper returns an instance of *RateLimitingWrapper
func NewRateLimitingWrapper(c *config.Config) *RateLimitingWrapper {
func NewRateLimitingWrapper(app pitaya.Pitaya, c *config.Config) *RateLimitingWrapper {
r := &RateLimitingWrapper{}

r.BaseWrapper = NewBaseWrapper(func(conn acceptor.PlayerConn) acceptor.PlayerConn {
Expand All @@ -42,7 +43,7 @@ func NewRateLimitingWrapper(c *config.Config) *RateLimitingWrapper {
forceDisable = c.GetBool("pitaya.conn.ratelimiting.forcedisable")
)

return NewRateLimiter(conn, limit, interval, forceDisable)
return NewRateLimiter(app, conn, limit, interval, forceDisable)
})

return r
Expand Down
11 changes: 9 additions & 2 deletions acceptorwrapper/rate_limiting_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/topfreegames/pitaya/config"
"github.com/topfreegames/pitaya/metrics"
"github.com/topfreegames/pitaya/mocks"
)

func TestNewRateLimitingWrapper(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
mockedApp := mocks.NewMockPitaya(ctrl)
mockedApp.EXPECT().GetMetricsReporters().Return([]metrics.Reporter{}).AnyTimes()

getConfig := func() *config.Config {
c := viper.New()
c.Set("pitaya.router.ratelimiting.limit", 20)
Expand All @@ -40,7 +47,7 @@ func TestNewRateLimitingWrapper(t *testing.T) {
return config.NewConfig(c)
}

rateLimitingWrapper := NewRateLimitingWrapper(getConfig())
expected := NewRateLimiter(nil, 20, time.Second, false)
rateLimitingWrapper := NewRateLimitingWrapper(mockedApp, getConfig())
expected := NewRateLimiter(mockedApp, nil, 20, time.Second, false)
assert.Equal(t, expected, rateLimitingWrapper.wrapConn(nil))
}
Loading

0 comments on commit f773c36

Please sign in to comment.