Skip to content

Commit

Permalink
Merge pull request #772 from gofiber/redis-sentinel
Browse files Browse the repository at this point in the history
Support for Redis Universal Client
  • Loading branch information
ReneWerner87 committed Apr 13, 2023
2 parents 83edce7 + dff02a2 commit 106c5f8
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 53 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/test-redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ jobs:
--tls-key-file ./redis/tests/tls/redis.key \
--tls-ca-cert-file ./redis/tests/tls/ca.crt&
- name: Setup Redis Cluster
uses: vishnudxb/[email protected]
with:
master1-port: 7000
master2-port: 7001
master3-port: 7002
slave1-port: 7003
slave2-port: 7004
slave3-port: 7005

- name: Install Go
uses: actions/setup-go@v4
with:
Expand Down
69 changes: 54 additions & 15 deletions redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() *redis.Client
func (s *Storage) Conn() redis.UniversalClient
```
### Installation
Redis is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
Expand All @@ -26,13 +26,13 @@ go mod init github.com/<user>/<repo>
```
And then install the redis implementation:
```bash
go get github.com/gofiber/storage/redis
go get github.com/gofiber/storage/redis/v2
```

### Examples
Import the storage package.
```go
import "github.com/gofiber/storage/redis"
import "github.com/gofiber/storage/redis/v2"
```

You can use the following possibilities to create a storage:
Expand All @@ -46,13 +46,23 @@ store := redis.New(redis.Config{
Port: 6379,
Username: "",
Password: "",
URL: "",
Database: 0,
Reset: false,
TLSConfig: nil,
PoolSize: 10 * runtime.GOMAXPROCS(0),
})

// Initialize Redis Failover Client
store := redis.New(redis.Config{
MasterName: "master-name",
Addrs: []string{":6379"},
})

// Initialize Redis Cluster Client
store := redis.New(redis.Config{
Addrs: []string{":6379", ":6380"},
})

// or just the url with all information
store = redis.New(redis.Config{
URL: "redis://<user>:<pass>@127.0.0.1:6379/<db>",
Expand Down Expand Up @@ -88,12 +98,37 @@ type Config struct {
// Optional. Default is 0
Database int

// URL the standard format redis url to parse all other options. If this is set all other config options, Host, Port, Username, Password, Database have no effect.
// URL standard format Redis URL. If this is set all other config options, Host, Port, Username, Password, Database have no effect.
//
// Example: redis://<user>:<pass>@localhost:6379/<db>
// Optional. Default is ""
URL string

// Either a single address or a seed list of host:port addresses, this enables FailoverClient and ClusterClient
//
// Optional. Default is []string{}
Addrs []string

// MasterName is the sentinel master's name
//
// Optional. Default is ""
MasterName string

// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
//
// Optional. Default is ""
ClientName string

// SentinelUsername
//
// Optional. Default is ""
SentinelUsername string

// SentinelPassword
//
// Optional. Default is ""
SentinelPassword string

// Reset clears any existing keys in existing Collection
//
// Optional. Default is false
Expand All @@ -109,20 +144,24 @@ type Config struct {
// Optional. Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS.
PoolSize int
}

```

### Default Config
```go
var ConfigDefault = Config{
Host: "127.0.0.1",
Port: 6379,
Username: "",
Password: "",
URL: "",
Database: 0,
Reset: false,
TLSConfig: nil,
PoolSize: 10 * runtime.GOMAXPROCS(0),
Host: "127.0.0.1",
Port: 6379,
Username: "",
Password: "",
URL: "",
Database: 0,
Reset: false,
TLSConfig: nil,
PoolSize: 10 * runtime.GOMAXPROCS(0),
Addrs: []string{},
MasterName: "",
ClientName: "",
SentinelUsername: "",
SentinelPassword: "",
}
```
58 changes: 42 additions & 16 deletions redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,69 @@ type Config struct {
// Optional. Default is 0
Database int

// URL the standard format redis url to parse all other options. If this is set all other config options, Host, Port, Username, Password, Database have no effect.
// URL standard format Redis URL. If this is set all other config options, Host, Port, Username, Password, Database have no effect.
//
// Example: redis://<user>:<pass>@localhost:6379/<db>
// Optional. Default is ""
URL string

// Either a single address or a seed list of host:port addresses, this enables FailoverClient and ClusterClient
//
// Optional. Default is []string{}
Addrs []string

// MasterName is the sentinel master's name
//
// Optional. Default is ""
MasterName string

// ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
//
// Optional. Default is ""
ClientName string

// SentinelUsername
//
// Optional. Default is ""
SentinelUsername string

// SentinelPassword
//
// Optional. Default is ""
SentinelPassword string

// Reset clears any existing keys in existing Collection
//
// Optional. Default is false
Reset bool

// TLS Config to use. When set TLS will be negotiated.
//
// Optional. Default is nil
TLSConfig *tls.Config

// Maximum number of socket connections.
//
// Optional. Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS.
PoolSize int

////////////////////////////////////
// Adaptor related config options //
////////////////////////////////////

// https://pkg.go.dev/github.com/go-redis/redis/v8#Options
}

// ConfigDefault is the default config
var ConfigDefault = Config{
Host: "127.0.0.1",
Port: 6379,
Username: "",
Password: "",
URL: "",
Database: 0,
Reset: false,
TLSConfig: nil,
PoolSize: 10 * runtime.GOMAXPROCS(0),
Host: "127.0.0.1",
Port: 6379,
Username: "",
Password: "",
URL: "",
Database: 0,
Reset: false,
TLSConfig: nil,
PoolSize: 10 * runtime.GOMAXPROCS(0),
Addrs: []string{},
MasterName: "",
ClientName: "",
SentinelUsername: "",
SentinelPassword: "",
}

// Helper function to set default values
Expand Down
2 changes: 1 addition & 1 deletion redis/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/gofiber/storage/redis
module github.com/gofiber/storage/redis/v2

go 1.18

Expand Down
49 changes: 28 additions & 21 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,46 @@ import (

// Storage interface that is implemented by storage providers
type Storage struct {
db *redis.Client
db redis.UniversalClient
}

// New creates a new redis storage
func New(config ...Config) *Storage {
// Set default config
cfg := configDefault(config...)

// Create new redis client
var options *redis.Options
var err error
// Create new redis universal client
var db redis.UniversalClient

// Parse the URL and update config values accordingly
if cfg.URL != "" {
options, err = redis.ParseURL(cfg.URL)

options, err := redis.ParseURL(cfg.URL)
if err != nil {
panic(err)
}

options.TLSConfig = cfg.TLSConfig
options.PoolSize = cfg.PoolSize
} else {
options = &redis.Options{
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
DB: cfg.Database,
Username: cfg.Username,
Password: cfg.Password,
TLSConfig: cfg.TLSConfig,
PoolSize: cfg.PoolSize,
}
// Update the config values with the parsed URL values
cfg.Username = options.Username
cfg.Password = options.Password
cfg.Database = options.DB
cfg.Addrs = []string{options.Addr}
} else if len(cfg.Addrs) == 0 {
// Fallback to Host and Port values if Addrs is empty
cfg.Addrs = []string{fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)}
}

db := redis.NewClient(options)
db = redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: cfg.Addrs,
MasterName: cfg.MasterName,
ClientName: cfg.ClientName,
SentinelUsername: cfg.SentinelUsername,
SentinelPassword: cfg.SentinelPassword,
DB: cfg.Database,
Username: cfg.Username,
Password: cfg.Password,
TLSConfig: cfg.TLSConfig,
PoolSize: cfg.PoolSize,
})

// Test connection
if err := db.Ping(context.Background()).Err(); err != nil {
Expand All @@ -62,6 +69,8 @@ func New(config ...Config) *Storage {
}
}

// ...

// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 {
Expand All @@ -76,7 +85,6 @@ func (s *Storage) Get(key string) ([]byte, error) {

// Set key with value
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
// Ain't Nobody Got Time For That
if len(key) <= 0 || len(val) <= 0 {
return nil
}
Expand All @@ -85,7 +93,6 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {

// Delete key by key
func (s *Storage) Delete(key string) error {
// Ain't Nobody Got Time For That
if len(key) <= 0 {
return nil
}
Expand All @@ -103,6 +110,6 @@ func (s *Storage) Close() error {
}

// Return database client
func (s *Storage) Conn() *redis.Client {
func (s *Storage) Conn() redis.UniversalClient {
return s.db
}
Loading

0 comments on commit 106c5f8

Please sign in to comment.