Skip to content

Commit

Permalink
feat: replace mem cache with redis
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Simpson committed Apr 5, 2024
1 parent 875ad1c commit dc978e3
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 100 deletions.
2 changes: 1 addition & 1 deletion cmd/character/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewServerContext(ctx context.Context, conf *config.GlobalConfig, tracer tra

server.KeycloakClient.RegisterMiddlewares(gocloak.OpenTelemetryMiddleware)

postgres, err := repository.ConnectDB(server.GlobalConfig.Character.Postgres)
postgres, err := repository.ConnectDB(conf.Character.Postgres, conf.Redis)
if err != nil {
return nil, fmt.Errorf("connecting to postgres: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/chat/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewServerContext(ctx context.Context, conf *config.GlobalConfig, tracer tra

server.KeycloakClient.RegisterMiddlewares(gocloak.OpenTelemetryMiddleware)

db, err := repository.ConnectDB(conf.Chat.Postgres)
db, err := repository.ConnectDB(conf.Chat.Postgres, conf.Redis)
if err != nil {
return nil, fmt.Errorf("connecting to postgres database: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gamebackend/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func NewServerContext(ctx context.Context, conf *config.GlobalConfig, tracer tra
}
}

db, err := repository.ConnectDB(conf.GameBackend.Postgres)
db, err := repository.ConnectDB(conf.GameBackend.Postgres, conf.Redis)
if err != nil {
return nil, fmt.Errorf("connecting to postgres database: %w", err)
}
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ services:
networks:
- backend

redis-cluster:
image: grokzen/redis-cluster:7.0.10
environment:
IP: 0.0.0.0
INITIAL_PORT: 7000
MASTERS: 3
SLAVES_PER_MASTER: 1
restart: always
ports:
- '7000-7050:7000-7050'
- '5000-5010:5000-5010'
networks:
- backend

prometheus:
image: prom/prometheus:latest
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ require (
require (
dario.cat/mergo v1.0.0 // indirect
github.com/WilSimpson/gocloak/v13 v13.11.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-gorm/caches/v4 v4.0.0 // indirect
Expand All @@ -56,6 +58,9 @@ require (
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5 // indirect
github.com/redis/go-redis/extra/redisotel/v9 v9.0.5 // indirect
github.com/redis/go-redis/v9 v9.5.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,20 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
github.com/bsm/gomega v1.26.0/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bxcodec/faker/v4 v4.0.0-beta.3 h1:gqYNBvN72QtzKkYohNDKQlm+pg+uwBDVMN28nWHS18k=
github.com/bxcodec/faker/v4 v4.0.0-beta.3/go.mod h1:m6+Ch1Lj3fqW/unZmvkXIdxWS5+XQWPWxcbbQW2X+Ho=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down Expand Up @@ -130,6 +135,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/docker/cli v25.0.3+incompatible h1:KLeNs7zws74oFuVhgZQ5ONGZiXUUdgsdy6/EsX/6284=
github.com/docker/cli v25.0.3+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/docker v25.0.3+incompatible h1:D5fy/lYmY7bvZa0XTZ5/UJPljor41F+vdyJG5luQLfQ=
Expand Down Expand Up @@ -511,6 +518,13 @@ github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8b
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5 h1:EaDatTxkdHG+U3Bk4EUr+DZ7fOGwTfezUiUJMaIcaho=
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5/go.mod h1:fyalQWdtzDBECAQFBJuQe5bzQ02jGd5Qcbgb97Flm7U=
github.com/redis/go-redis/extra/redisotel/v9 v9.0.5 h1:EfpWLLCyXw8PSM2/XNJLjI3Pb27yVE+gIAfeqp8LUCc=
github.com/redis/go-redis/extra/redisotel/v9 v9.0.5/go.mod h1:WZjPDy7VNzn77AAfnAfVjZNvfJTYfPetfZk5yoSTLaQ=
github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
Expand Down
12 changes: 10 additions & 2 deletions pkg/config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import "fmt"

// DBConfig Information on how to connect to the database
type DBConfig struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
ServerAddress
Name string `yaml:"name"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Expand Down Expand Up @@ -48,3 +47,12 @@ func (c DBConfig) MongoDSN() string {
c.Port,
)
}

func (p DBPoolConfig) Addresses() []string {
addrs := make([]string, len(p.Slaves)+1)
for idx, dbConf := range p.Slaves {
addrs[idx+1] = dbConf.Address()
}

return addrs
}
4 changes: 2 additions & 2 deletions pkg/config/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ = Describe("Db config", func() {
AfterEach(func() {
Expect(dsn).To(ContainSubstring(config.Username))
Expect(dsn).To(ContainSubstring(config.Password))
Expect(dsn).To(ContainSubstring(config.Host))
Expect(dsn).To(ContainSubstring(config.Port))
Expect(dsn).To(ContainSubstring(config.ServerAddress.Host))
Expect(dsn).To(ContainSubstring(config.ServerAddress.Port))
})
})
85 changes: 67 additions & 18 deletions pkg/config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type GlobalConfig struct {
Agones AgonesConfig `json:"agones"`
Keycloak KeycloakGlobal `yaml:"keycloak"`
Version string
Redis DBPoolConfig
}

type SROServer struct {
Expand Down Expand Up @@ -80,7 +81,7 @@ type AgonesConfig struct {
}

type ServerAddress struct {
Port uint `yaml:"port"`
Port string `yaml:"port"`
Host string `yaml:"host"`
}

Expand All @@ -89,11 +90,11 @@ func NewGlobalConfig(ctx context.Context) (*GlobalConfig, error) {
Character: CharacterServer{
SROServer: SROServer{
Local: ServerAddress{
Port: 8081,
Port: "8081",
Host: "",
},
Remote: ServerAddress{
Port: 8081,
Port: "8081",
Host: "",
},
Mode: LocalMode,
Expand All @@ -106,17 +107,21 @@ func NewGlobalConfig(ctx context.Context) (*GlobalConfig, error) {
},
Postgres: DBPoolConfig{
Master: DBConfig{
Host: "localhost",
Port: "5432",
ServerAddress: ServerAddress{
Host: "localhost",
Port: "5432",
},
Name: "characters",
Username: "postgres",
Password: "password",
},
},
Mongo: DBPoolConfig{
Master: DBConfig{
Host: "localhost",
Port: "27017",
ServerAddress: ServerAddress{
Host: "localhost",
Port: "27017",
},
Username: "mongo",
Password: "password",
Name: "sro",
Expand All @@ -126,11 +131,11 @@ func NewGlobalConfig(ctx context.Context) (*GlobalConfig, error) {
GameBackend: GamebackendServer{
SROServer: SROServer{
Local: ServerAddress{
Port: 8082,
Port: "8082",
Host: "",
},
Remote: ServerAddress{
Port: 8082,
Port: "8082",
Host: "",
},
Mode: LocalMode,
Expand All @@ -143,8 +148,10 @@ func NewGlobalConfig(ctx context.Context) (*GlobalConfig, error) {
},
Postgres: DBPoolConfig{
Master: DBConfig{
Host: "localhost",
Port: "5432",
ServerAddress: ServerAddress{
Host: "localhost",
Port: "5432",
},
Name: "gamebackend",
Username: "postgres",
Password: "password",
Expand All @@ -155,11 +162,11 @@ func NewGlobalConfig(ctx context.Context) (*GlobalConfig, error) {
Chat: ChatServer{
SROServer: SROServer{
Local: ServerAddress{
Port: 8180,
Port: "8180",
Host: "",
},
Remote: ServerAddress{
Port: 8180,
Port: "8180",
Host: "",
},
Mode: LocalMode,
Expand All @@ -171,13 +178,15 @@ func NewGlobalConfig(ctx context.Context) (*GlobalConfig, error) {
},
},
Kafka: ServerAddress{
Port: 29092,
Port: "29092",
Host: "localhost",
},
Postgres: DBPoolConfig{
Master: DBConfig{
Host: "localhost",
Port: "5432",
ServerAddress: ServerAddress{
Host: "localhost",
Port: "5432",
},
Name: "chat",
Username: "postgres",
Password: "password",
Expand All @@ -194,14 +203,54 @@ func NewGlobalConfig(ctx context.Context) (*GlobalConfig, error) {
CaCertFile: "/etc/sro/auth/agones/ca/ca",
Namespace: "default",
Allocator: ServerAddress{
Port: 443,
Port: "443",
Host: "localhost",
},
},
Keycloak: KeycloakGlobal{
BaseURL: "http://localhost:80801/",
Realm: "default",
},
Redis: DBPoolConfig{
Master: DBConfig{
ServerAddress: ServerAddress{
Port: "7000",
Host: "localhost",
},
},
Slaves: []DBConfig{
{
ServerAddress: ServerAddress{
Port: "7001",
Host: "localhost",
},
},
{
ServerAddress: ServerAddress{
Port: "7002",
Host: "localhost",
},
},
{
ServerAddress: ServerAddress{
Port: "7003",
Host: "localhost",
},
},
{
ServerAddress: ServerAddress{
Port: "7004",
Host: "localhost",
},
},
{
ServerAddress: ServerAddress{
Port: "7005",
Host: "localhost",
},
},
},
},
Version: Version,
}

Expand Down Expand Up @@ -274,5 +323,5 @@ func bindRecursive(key string, val reflect.Value) {
}

func (s *ServerAddress) Address() string {
return fmt.Sprintf("%s:%d", s.Host, s.Port)
return fmt.Sprintf("%s:%s", s.Host, s.Port)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package repository
package cacher

import (
"context"
Expand Down
Loading

0 comments on commit dc978e3

Please sign in to comment.