Skip to content

Commit

Permalink
Merge pull request topfreegames#134 from felippeduran/refactor/hardco…
Browse files Browse the repository at this point in the history
…ded-configs

Refactor/hardcoded configs
  • Loading branch information
leohahn authored Sep 18, 2020
2 parents c1c8e84 + 6420d49 commit 108e5f0
Show file tree
Hide file tree
Showing 59 changed files with 1,633 additions and 903 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ 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[%]"

mocks: agent-mock session-mock networkentity-mock pitaya-mock serializer-mock acceptor-mock

agent-mock:
@mockgen github.com/topfreegames/pitaya/v2/agent Agent,AgentFactory | sed 's/mock_agent/mocks/' > agent/mocks/agent.go

Expand All @@ -148,10 +150,10 @@ networkentity-mock:
@mockgen github.com/topfreegames/pitaya/v2/networkentity NetworkEntity | sed 's/mock_networkentity/mocks/' > networkentity/mocks/networkentity.go

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

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

acceptor-mock:
@mockgen github.com/topfreegames/pitaya/v2/acceptor Acceptor | sed 's/mock_acceptor/mocks/' > mocks/acceptor.go
@mockgen github.com/topfreegames/pitaya/v2/acceptor PlayerConn,Acceptor | sed 's/mock_acceptor/mocks/' > mocks/acceptor.go
10 changes: 2 additions & 8 deletions acceptorwrapper/rate_limiting_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@ type RateLimitingWrapper struct {
}

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

r.BaseWrapper = NewBaseWrapper(func(conn acceptor.PlayerConn) acceptor.PlayerConn {
var (
limit = c.GetInt("pitaya.conn.ratelimiting.limit")
interval = c.GetDuration("pitaya.conn.ratelimiting.interval")
forceDisable = c.GetBool("pitaya.conn.ratelimiting.forcedisable")
)

return NewRateLimiter(app, conn, limit, interval, forceDisable)
return NewRateLimiter(app, conn, c.Limit, c.Interval, c.ForceDisable)
})

return r
Expand Down
11 changes: 1 addition & 10 deletions acceptorwrapper/rate_limiting_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"time"

"github.com/golang/mock/gomock"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/topfreegames/pitaya/v2/config"
"github.com/topfreegames/pitaya/v2/metrics"
Expand All @@ -39,15 +38,7 @@ func TestNewRateLimitingWrapper(t *testing.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)
c.Set("pitaya.router.ratelimiting.interval", time.Second)
c.Set("pitaya.router.ratelimiting.forceDisable", false)
return config.NewConfig(c)
}

rateLimitingWrapper := NewRateLimitingWrapper(mockedApp, getConfig())
rateLimitingWrapper := NewRateLimitingWrapper(mockedApp, config.NewDefaultRateLimitingConfig())
expected := NewRateLimiter(mockedApp, nil, 20, time.Second, false)
assert.Equal(t, expected, rateLimitingWrapper.wrapConn(nil))
}
32 changes: 32 additions & 0 deletions agent/mocks/agent.go

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

23 changes: 8 additions & 15 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (

"github.com/golang/protobuf/proto"
opentracing "github.com/opentracing/opentracing-go"
"github.com/spf13/viper"
"github.com/topfreegames/pitaya/v2/acceptor"
"github.com/topfreegames/pitaya/v2/cluster"
"github.com/topfreegames/pitaya/v2/component"
Expand Down Expand Up @@ -75,7 +74,6 @@ type Pitaya interface {
SetDebug(debug bool)
SetHeartbeatTime(interval time.Duration)
GetServerID() string
GetConfig() *config.Config
GetMetricsReporters() []metrics.Reporter
GetServer() *cluster.Server
GetServerByID(id string) (*cluster.Server, error)
Expand All @@ -101,7 +99,7 @@ type Pitaya interface {
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
opts *worker.EnqueueOpts,
opts *config.EnqueueOpts,
) (jid string, err error)

SendPushToUsers(route string, v interface{}, uids []string, frontendType string) ([]string, error)
Expand Down Expand Up @@ -131,7 +129,7 @@ type Pitaya interface {
// App is the base app struct
type App struct {
acceptors []acceptor.Acceptor
config *config.Config
config config.PitayaConfig
debug bool
dieChan chan bool
heartbeat time.Duration
Expand Down Expand Up @@ -174,10 +172,11 @@ func NewApp(
groups groups.GroupService,
sessionPool session.SessionPool,
metricsReporters []metrics.Reporter,
cfgs ...*viper.Viper,
config config.PitayaConfig,
) *App {
app := &App{
server: server,
config: config,
rpcClient: rpcClient,
rpcServer: rpcServer,
worker: worker,
Expand All @@ -200,9 +199,8 @@ func NewApp(
modulesArr: []moduleWrapper{},
sessionPool: sessionPool,
}
app.config = config.NewConfig(cfgs...)
if app.heartbeat == time.Duration(0) {
app.heartbeat = app.config.GetDuration("pitaya.heartbeat.interval")
app.heartbeat = config.HearbeatInterval
}

app.initSysRemotes()
Expand All @@ -229,11 +227,6 @@ func (app *App) GetServerID() string {
return app.server.ID
}

// GetConfig gets the pitaya config instance
func (app *App) GetConfig() *config.Config {
return app.config
}

// GetMetricsReporters gets registered metrics reporters
func (app *App) GetMetricsReporters() []metrics.Reporter {
return app.metricsReporters
Expand Down Expand Up @@ -273,7 +266,7 @@ func (app *App) initSysRemotes() {
}

func (app *App) periodicMetrics() {
period := app.config.GetDuration("pitaya.metrics.periodicMetrics.period")
period := app.config.MetricsPeriod
go metrics.ReportSysMetrics(app.metricsReporters, period)

if app.worker.Started() {
Expand Down Expand Up @@ -345,7 +338,7 @@ func (app *App) listen() {
timer.GlobalTicker = time.NewTicker(timer.Precision)

logger.Log.Infof("starting server %s:%s", app.server.Type, app.server.ID)
for i := 0; i < app.config.GetInt("pitaya.concurrency.handler.dispatch"); i++ {
for i := 0; i < app.config.ConcurrencyHandlerDispatch; i++ {
go app.handlerService.Dispatch(i)
}
for _, acc := range app.acceptors {
Expand All @@ -363,7 +356,7 @@ func (app *App) listen() {
logger.Log.Infof("listening with acceptor %s on addr %s", reflect.TypeOf(a), a.GetAddr())
}

if app.serverMode == Cluster && app.server.Frontend && app.config.GetBool("pitaya.session.unique") {
if app.serverMode == Cluster && app.server.Frontend && app.config.SessionUnique {
unique := mods.NewUniqueSession(app.server, app.rpcServer, app.rpcClient, app.sessionPool)
app.remoteService.AddRemoteBindingListener(unique)
app.RegisterModule(unique, "uniqueSession")
Expand Down
Loading

0 comments on commit 108e5f0

Please sign in to comment.