Skip to content
This repository was archived by the owner on May 2, 2025. It is now read-only.

Commit 17415ee

Browse files
authored
Refactor Redis url into servers in configuration (#494)
1 parent c806afc commit 17415ee

File tree

7 files changed

+84
-37
lines changed

7 files changed

+84
-37
lines changed

cmd/chirpstack-application-server/cmd/configfile.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,40 @@ max_idle_connections={{ .PostgreSQL.MaxIdleConnections }}
8484
#
8585
# Please note that Redis 2.6.0+ is required.
8686
[redis]
87-
# Redis url (e.g. redis://user:password@hostname/0)
87+
88+
# Server address or addresses.
89+
#
90+
# Set multiple addresses when connecting to a cluster.
91+
servers=[{{ range $index, $elm := .Redis.Servers }}
92+
"{{ $elm }}",{{ end }}
93+
]
94+
95+
# Password.
8896
#
89-
# For more information about the Redis URL format, see:
90-
# https://www.iana.org/assignments/uri-schemes/prov/redis
91-
url="{{ .Redis.URL }}"
97+
# Set the password when connecting to Redis requires password authentication.
98+
password="{{ .Redis.Password }}"
99+
100+
# Database index.
101+
#
102+
# By default, this can be a number between 0-15.
103+
database={{ .Redis.Database }}
104+
92105
93106
# Redis Cluster.
94107
#
95-
# Set this to true when the provided URL is pointing to a Redis Cluster
108+
# Set this to true when the provided URLs are pointing to a Redis Cluster
96109
# instance.
97110
cluster={{ .Redis.Cluster }}
98111
99-
# The master name.
112+
# Master name.
100113
#
101-
# Set the master name when the provided URL is pointing to a Redis Sentinel
114+
# Set the master name when the provided URLs are pointing to a Redis Sentinel
102115
# instance.
103116
master_name="{{ .Redis.MasterName }}"
104117
105118
# Connection pool size.
106119
#
107-
# Default is 10 connections per every CPU.
120+
# Default (when set to 0) is 10 connections per every CPU.
108121
pool_size={{ .Redis.PoolSize }}
109122
110123

cmd/chirpstack-application-server/cmd/root.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/brocaar/chirpstack-application-server/internal/config"
12+
"github.com/go-redis/redis/v7"
1213
"github.com/spf13/viper"
1314

1415
log "github.com/sirupsen/logrus"
@@ -40,7 +41,7 @@ func init() {
4041
viper.SetDefault("postgresql.dsn", "postgres://localhost/chirpstack_as?sslmode=disable")
4142
viper.SetDefault("postgresql.automigrate", true)
4243
viper.SetDefault("postgresql.max_idle_connections", 2)
43-
viper.SetDefault("redis.url", "redis://localhost:6379")
44+
viper.SetDefault("redis.servers", []string{"localhost:6379"})
4445
viper.SetDefault("application_server.api.public_host", "localhost:8001")
4546
viper.SetDefault("application_server.id", "6d5db27e-4ce2-4b2b-b5d7-91f069397978")
4647
viper.SetDefault("application_server.api.bind", "0.0.0.0:8001")
@@ -135,6 +136,17 @@ func initConfig() {
135136
if config.C.ApplicationServer.Integration.Backend != "" {
136137
config.C.ApplicationServer.Integration.Enabled = []string{config.C.ApplicationServer.Integration.Backend}
137138
}
139+
140+
if config.C.Redis.URL != "" {
141+
opt, err := redis.ParseURL(config.C.Redis.URL)
142+
if err != nil {
143+
log.WithError(err).Fatal("redis url error")
144+
}
145+
146+
config.C.Redis.Servers = []string{opt.Addr}
147+
config.C.Redis.Database = opt.DB
148+
config.C.Redis.Password = opt.Password
149+
}
138150
}
139151

140152
func viperBindEnvs(iface interface{}, parts ...string) {

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
- kafka
1616
environment:
1717
- TEST_POSTGRES_DSN=postgres://chirpstack_as:chirpstack_as@postgres/chirpstack_as?sslmode=disable
18-
- TEST_REDIS_URL=redis://redis:6379
18+
- TEST_REDIS_SERVERS=redis:6379
1919
- TEST_MQTT_SERVER=tcp://mosquitto:1883
2020
- TEST_RABBITMQ_URL=amqp://guest:guest@rabbitmq:5672/
2121
- TEST_KAFKA_BROKER=kafka:9092

docs/content/install/config.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,27 +137,40 @@ max_idle_connections=2
137137
#
138138
# Please note that Redis 2.6.0+ is required.
139139
[redis]
140-
# Redis url (e.g. redis://user:password@hostname/0)
140+
141+
# Server address or addresses.
142+
#
143+
# Set multiple addresses when connecting to a cluster.
144+
servers=[
145+
"localhost:6379",
146+
]
147+
148+
# Password.
141149
#
142-
# For more information about the Redis URL format, see:
143-
# https://www.iana.org/assignments/uri-schemes/prov/redis
144-
url="redis://localhost:6379"
150+
# Set the password when connecting to Redis requires password authentication.
151+
password=""
152+
153+
# Database index.
154+
#
155+
# By default, this can be a number between 0-15.
156+
database=0
157+
145158

146159
# Redis Cluster.
147160
#
148-
# Set this to true when the provided URL is pointing to a Redis Cluster
161+
# Set this to true when the provided URLs are pointing to a Redis Cluster
149162
# instance.
150163
cluster=false
151164

152-
# The master name.
165+
# Master name.
153166
#
154-
# Set the master name when the provided URL is pointing to a Redis Sentinel
167+
# Set the master name when the provided URLs are pointing to a Redis Sentinel
155168
# instance.
156169
master_name=""
157170

158171
# Connection pool size.
159172
#
160-
# Default is 10 connections per every CPU.
173+
# Default (when set to 0) is 10 connections per every CPU.
161174
pool_size=0
162175

163176

internal/config/config.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ type Config struct {
2020
} `mapstructure:"postgresql"`
2121

2222
Redis struct {
23-
URL string `mapstructure:"url"`
24-
Cluster bool `mapstructure:"cluster"`
25-
MasterName string `mapstructure:"master_name"`
26-
PoolSize int `mapstructure:"pool_size"`
23+
URL string `mapstructure:"url"` // deprecated
24+
Servers []string `mapstructure:"servers"`
25+
Cluster bool `mapstructure:"cluster"`
26+
MasterName string `mapstructure:"master_name"`
27+
PoolSize int `mapstructure:"pool_size"`
28+
Password string `mapstructure:"password"`
29+
Database int `mapstructure:"database"`
2730
} `mapstructure:"redis"`
2831

2932
ApplicationServer struct {

internal/storage/storage.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,32 @@ func Setup(c config.Config) error {
5757
)
5858

5959
log.Info("storage: setting up Redis client")
60-
opt, err := redis.ParseURL(c.Redis.URL)
61-
if err != nil {
62-
return errors.Wrap(err, "parse redis url error")
60+
if len(c.Redis.Servers) == 0 {
61+
return errors.New("at least one redis server must be configured")
6362
}
64-
opt.PoolSize = c.Redis.PoolSize
63+
6564
if c.Redis.Cluster {
6665
redisClient = redis.NewClusterClient(&redis.ClusterOptions{
67-
Addrs: []string{opt.Addr},
68-
PoolSize: opt.PoolSize,
69-
Password: opt.Password,
66+
Addrs: c.Redis.Servers,
67+
PoolSize: c.Redis.PoolSize,
68+
Password: c.Redis.Password,
7069
})
7170
} else if c.Redis.MasterName != "" {
7271
redisClient = redis.NewFailoverClient(&redis.FailoverOptions{
7372
MasterName: c.Redis.MasterName,
74-
SentinelAddrs: []string{opt.Addr},
75-
SentinelPassword: opt.Password,
76-
DB: opt.DB,
77-
PoolSize: opt.PoolSize,
73+
SentinelAddrs: c.Redis.Servers,
74+
SentinelPassword: c.Redis.Password,
75+
DB: c.Redis.Database,
76+
PoolSize: c.Redis.PoolSize,
77+
Password: c.Redis.Password,
7878
})
7979
} else {
80-
redisClient = redis.NewClient(opt)
80+
redisClient = redis.NewClient(&redis.Options{
81+
Addr: c.Redis.Servers[0],
82+
DB: c.Redis.Database,
83+
Password: c.Redis.Password,
84+
PoolSize: c.Redis.PoolSize,
85+
})
8186
}
8287

8388
log.Info("storage: connecting to PostgreSQL database")

internal/test/test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package test
22

33
import (
44
"os"
5+
"strings"
56

67
"github.com/jmoiron/sqlx"
78
migrate "github.com/rubenv/sql-migrate"
@@ -23,7 +24,7 @@ func GetConfig() config.Config {
2324
var c config.Config
2425

2526
c.PostgreSQL.DSN = "postgres://localhost/chirpstack_as_test?sslmode=disable"
26-
c.Redis.URL = "redis://localhost:6379"
27+
c.Redis.Servers = []string{"localhost:6379"}
2728
c.ApplicationServer.Integration.MQTT.Server = "tcp://localhost:1883"
2829
c.ApplicationServer.ID = "6d5db27e-4ce2-4b2b-b5d7-91f069397978"
2930
c.ApplicationServer.Integration.AMQP.EventRoutingKeyTemplate = "application.{{ .ApplicationID }}.device.{{ .DevEUI }}.event.{{ .EventType }}"
@@ -34,8 +35,8 @@ func GetConfig() config.Config {
3435
c.PostgreSQL.DSN = v
3536
}
3637

37-
if v := os.Getenv("TEST_REDIS_URL"); v != "" {
38-
c.Redis.URL = v
38+
if v := os.Getenv("TEST_REDIS_SERVERS"); v != "" {
39+
c.Redis.Servers = strings.Split(v, ",")
3940
}
4041

4142
if v := os.Getenv("TEST_MQTT_SERVER"); v != "" {

0 commit comments

Comments
 (0)