Skip to content

Commit

Permalink
Make serializer type configurable via external configuration (file or…
Browse files Browse the repository at this point in the history
… env var)
  • Loading branch information
rsafonseca committed Feb 8, 2024
1 parent 1119ccc commit 17c8e37
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
8 changes: 6 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/topfreegames/pitaya/v2/pipeline"
"github.com/topfreegames/pitaya/v2/router"
"github.com/topfreegames/pitaya/v2/serialize"
"github.com/topfreegames/pitaya/v2/serialize/json"
"github.com/topfreegames/pitaya/v2/service"
"github.com/topfreegames/pitaya/v2/session"
"github.com/topfreegames/pitaya/v2/worker"
Expand Down Expand Up @@ -142,6 +141,11 @@ func NewBuilder(isFrontend bool,
panic(err)
}

serializer, err := serialize.NewSerializer(serialize.Type(config.SerializerType))
if err != nil {
logger.Log.Fatalf("error creating serializer: %s", err.Error())
}

return &Builder{
acceptors: []acceptor.Acceptor{},
postBuildHooks: make([]func(app Pitaya), 0),
Expand All @@ -150,7 +154,7 @@ func NewBuilder(isFrontend bool,
PacketDecoder: codec.NewPomeloPacketDecoder(),
PacketEncoder: codec.NewPomeloPacketEncoder(),
MessageEncoder: message.NewMessagesEncoder(config.Handler.Messages.Compression),
Serializer: json.NewSerializer(),
Serializer: serializer,
Router: router.New(),
RPCClient: rpcClient,
RPCServer: rpcServer,
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// PitayaConfig provides all the configuration for a pitaya app
type PitayaConfig struct {
SerializerType int `mapstructure:"serializertype"`
DefaultPipelines struct {
StructValidation struct {
Enabled bool `mapstructure:"enabled"`
Expand Down Expand Up @@ -60,6 +61,7 @@ type PitayaConfig struct {
// NewDefaultPitayaConfig provides default configuration for Pitaya App
func NewDefaultPitayaConfig() *PitayaConfig {
return &PitayaConfig{
SerializerType: 1,
DefaultPipelines: struct {
StructValidation struct {
Enabled bool `mapstructure:"enabled"`
Expand Down
3 changes: 2 additions & 1 deletion config/viper_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewConfig(cfgs ...*viper.Viper) *Config {

cfg.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
cfg.AutomaticEnv()
c := &Config{Viper: *cfg}
c := &Config{*cfg}
c.fillDefaultValues()
return c
}
Expand All @@ -54,6 +54,7 @@ func (c *Config) fillDefaultValues() {
pitayaConfig := NewDefaultPitayaConfig()

defaultsMap := map[string]interface{}{
"pitaya.serializertype": pitayaConfig.SerializerType,
"pitaya.buffer.agent.messages": pitayaConfig.Buffer.Agent.Messages,
// the max buffer size that nats will accept, if this buffer overflows, messages will begin to be dropped
"pitaya.buffer.handler.localprocess": pitayaConfig.Buffer.Handler.LocalProcess,
Expand Down
31 changes: 31 additions & 0 deletions serialize/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@

package serialize

import (
"errors"

"github.com/topfreegames/pitaya/v2/serialize/json"
"github.com/topfreegames/pitaya/v2/serialize/protobuf"
)

const (
JSON Type = 1
PROTOBUF Type = 2
)

type (
// Type is the Serializer type.
Type uint16

// Marshaler represents a marshal interface
Marshaler interface {
Expand All @@ -39,3 +53,20 @@ type (
GetName() string
}
)

// All recognized and expected serializer type values.

// NewSerializer returns a new serializer of the respective type (JSON or PROTOBUF) according to serializerType Type.
// If serializerType is a JSON, then a JSON serializer is returned.
// If serializerType is a PROTOBUF, then a PROTOBUF serializer is returned.
// Otherwise, if serializerType is not a valid serializer type, then it returns nil.
func NewSerializer(serializerType Type) (Serializer, error) { //nolint:ireturn
switch serializerType {
case JSON:
return json.NewSerializer(), nil
case PROTOBUF:
return protobuf.NewSerializer(), nil
default:
return nil, errors.New("serializer type unknown")
}
}

0 comments on commit 17c8e37

Please sign in to comment.