Skip to content

Commit

Permalink
Add support for Redis key prefix config.
Browse files Browse the repository at this point in the history
  • Loading branch information
brocaar committed Mar 16, 2021
1 parent f10a425 commit 83dfc20
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
8 changes: 8 additions & 0 deletions cmd/chirpstack-network-server/cmd/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ pool_size={{ .Redis.PoolSize }}
# used by the server.
tls_enabled={{ .Redis.TLSEnabled }}
# Key prefix.
#
# A key prefix can be used to avoid key collisions when multiple regions
# are using the same Redis database and it is not possible to separate
# keys by database index (e.g. when using Redis Cluster, which does not
# support multiple databases).
key_prefix="{{ .Redis.KeyPrefix }}"
# Network-server settings.
[network_server]
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
Password string `mapstructure:"password"`
Database int `mapstructure:"database"`
TLSEnabled bool `mapstructure:"tls_enabled"`
KeyPrefix string `mapstructure:"key_prefix"`
} `mapstructure:"redis"`

NetworkServer struct {
Expand Down
6 changes: 5 additions & 1 deletion internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ var deviceSessionTTL time.Duration
// scheduler runs.
var schedulerInterval time.Duration

// keyPrefix for Redis.
var keyPrefix string

// Setup configures the storage backend.
func Setup(c config.Config) error {
log.Info("storage: setting up storage module")

deviceSessionTTL = c.NetworkServer.DeviceSessionTTL
schedulerInterval = c.NetworkServer.Scheduler.SchedulerInterval
keyPrefix = c.Redis.KeyPrefix

log.Info("storage: setting up Redis client")
if len(c.Redis.Servers) == 0 {
Expand Down Expand Up @@ -126,5 +130,5 @@ func Transaction(f func(tx sqlx.Ext) error) error {

// GetRedisKey returns the Redis key given a template and parameters.
func GetRedisKey(tmpl string, params ...interface{}) string {
return fmt.Sprintf(tmpl, params...)
return keyPrefix + fmt.Sprintf(tmpl, params...)
}
13 changes: 10 additions & 3 deletions internal/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ func TestGetRedisKey(t *testing.T) {
assert := require.New(t)

tests := []struct {
template string
params []interface{}
expected string
keyPrefix string
template string
params []interface{}
expected string
}{
{
keyPrefix: "eu868:",
template: "foo:bar:key",
expected: "eu868:foo:bar:key",
},
{
template: "foo:bar:key",
expected: "foo:bar:key",
Expand All @@ -71,6 +77,7 @@ func TestGetRedisKey(t *testing.T) {
}

for _, tst := range tests {
keyPrefix = tst.keyPrefix
out := GetRedisKey(tst.template, tst.params...)
assert.Equal(tst.expected, out)
}
Expand Down

0 comments on commit 83dfc20

Please sign in to comment.