-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from gozix/registry
Added connection registry
- Loading branch information
Showing
3 changed files
with
172 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Package goredis provides implementation of go-redis client. | ||
package goredis | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"sync" | ||
"time" | ||
|
||
"github.com/go-redis/redis" | ||
) | ||
|
||
// DEFAULT is default connection name. | ||
const DEFAULT = "default" | ||
|
||
// ConfigKey is root config key. | ||
const configKey = "redis" | ||
|
||
type ( | ||
// Config is registry configuration item. | ||
Config struct { | ||
Host string `json:"host"` | ||
Port string `json:"port"` | ||
DB int `json:"db"` | ||
Password string `json:"password"` | ||
MaxRetries int `json:"max_retries"` | ||
IdleTimeout time.Duration `json:"idle_timeout"` | ||
ReadTimeout time.Duration `json:"read_timeout"` | ||
WriteTimeout time.Duration `json:"write_timeout"` | ||
} | ||
|
||
// Configs is registry configurations. | ||
Configs map[string]Config | ||
|
||
// Registry is database connection registry. | ||
Registry struct { | ||
mux sync.Mutex | ||
clients map[string]*redis.Client | ||
conf Configs | ||
} | ||
) | ||
|
||
var ( | ||
// ErrUnknownConnection is error triggered when connection with provided name not founded. | ||
ErrUnknownConnection = errors.New("unknown connection") | ||
) | ||
|
||
// NewRegistry is registry constructor. | ||
func NewRegistry(conf Configs) *Registry { | ||
return &Registry{ | ||
clients: make(map[string]*redis.Client, 1), | ||
conf: conf, | ||
} | ||
} | ||
|
||
// Close is method for close connections. | ||
func (r *Registry) Close() (err error) { | ||
r.mux.Lock() | ||
defer r.mux.Unlock() | ||
|
||
for key, client := range r.clients { | ||
if errClose := client.Close(); errClose != nil { | ||
err = errClose | ||
} | ||
|
||
delete(r.clients, key) | ||
} | ||
|
||
return err | ||
} | ||
|
||
// Connection is default connection getter. | ||
func (r *Registry) Connection() (*redis.Client, error) { | ||
return r.ConnectionWithName(DEFAULT) | ||
} | ||
|
||
// ConnectionWithName is connection getter by name. | ||
func (r *Registry) ConnectionWithName(name string) (_ *redis.Client, err error) { | ||
r.mux.Lock() | ||
defer r.mux.Unlock() | ||
|
||
var client, initialized = r.clients[name] | ||
if initialized { | ||
return client, nil | ||
} | ||
|
||
var cfg, exists = r.conf[name] | ||
if !exists { | ||
return nil, ErrUnknownConnection | ||
} | ||
|
||
var options = &redis.Options{ | ||
Addr: net.JoinHostPort( | ||
cfg.Host, | ||
cfg.Port, | ||
), | ||
DB: cfg.DB, | ||
MaxRetries: cfg.MaxRetries, | ||
IdleTimeout: cfg.IdleTimeout, | ||
ReadTimeout: cfg.ReadTimeout, | ||
WriteTimeout: cfg.WriteTimeout, | ||
} | ||
|
||
if client = redis.NewClient(options); client == nil { | ||
return nil, err | ||
} | ||
|
||
if _, err = client.Ping().Result(); err != nil { | ||
return nil, err | ||
} | ||
|
||
r.clients[name] = client | ||
|
||
return client, nil | ||
} |