From 56132e3d64e76c8a50e544138682b2e09bf9f857 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Mon, 24 Jun 2013 14:18:32 -0400 Subject: [PATCH] initial redis work, storage.Storage->storage.Conn, among other new options in the config file for storage timeouts --- config/config.go | 10 +++++++--- example/config.json | 7 ++++++- server/server.go | 6 +++--- storage/redis/redis.go | 41 +++++++++++++++++++++++++++++++++++++++++ storage/storage.go | 6 +++--- 5 files changed, 60 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index f10bf65..6ba6646 100644 --- a/config/config.go +++ b/config/config.go @@ -33,12 +33,16 @@ type Client struct { type Storage struct { Driver string `json:"driver"` - Protocol string `json:"protocol"` + Network string `json:"network` Addr string `json:"addr"` Username string `json:"user"` Password string `json:"pass"` - Schema string `json:"schema"` - Encoding string `json:"encoding"` + Schema string `json:"schema,omitempty"` + Encoding string `json:"encoding,omitempty"` + + ConnectTimeout *Duration `json:"conn_timeout,omitempty"` + ReadTimeout *Duration `json:"read_timeout,omitempty"` + WriteTimeout *Duration `json:"write_timeout,omitempty"` } type Config struct { diff --git a/example/config.json b/example/config.json index be6a111..6fb9dc2 100644 --- a/example/config.json +++ b/example/config.json @@ -1,11 +1,16 @@ { + "network": "tcp", "addr": ":34000", "storage": { "driver": "redis", "addr": "127.0.0.1:6379", "user": "root", - "pass": "" + "pass": "", + + "conn_timeout": "5s", + "read_timeout": "5s", + "write_timeout": "5s" }, "private": true, diff --git a/server/server.go b/server/server.go index 457bf6a..9458021 100644 --- a/server/server.go +++ b/server/server.go @@ -25,7 +25,7 @@ import ( type Server struct { conf *config.Config listener net.Listener - storage storage.Storage + storage storage.Conn serving bool startTime time.Time @@ -39,7 +39,7 @@ type Server struct { } func New(conf *config.Config) (*Server, error) { - store, err := storage.New(&conf.Storage) + store, err := storage.Open(&conf.Storage) if err != nil { return nil, err } @@ -129,7 +129,7 @@ func fail(err error, w http.ResponseWriter, r *http.Request) { w.(http.Flusher).Flush() } -func validatePasskey(dir string, s storage.Storage) (*storage.User, error) { +func validatePasskey(dir string, s storage.Conn) (*storage.User, error) { if len(dir) != 34 { return nil, errors.New("Your passkey is invalid") } diff --git a/storage/redis/redis.go b/storage/redis/redis.go index be82e7d..4c7fe0f 100644 --- a/storage/redis/redis.go +++ b/storage/redis/redis.go @@ -5,5 +5,46 @@ package redis import ( + "github.com/garyburd/redigo/redis" + + "github.com/pushrax/chihaya/config" "github.com/pushrax/chihaya/storage" ) + +type driver struct{} + +func (d *driver) New(conf *config.Storage) (storage.Conn, error) { + var ( + conn redis.Conn + err error + ) + + if conf.ConnectTimeout != nil && + conf.ReadTimeout != nil && + conf.WriteTimeout != nil { + + conn, err = redis.DialTimeout( + conf.Network, + conf.Addr, + conf.ConnectTimeout.Duration, + conf.ReadTimeout.Duration, + conf.WriteTimeout.Duration, + ) + } else { + conn, err = redis.Dial(conf.Network, conf.Addr) + } + if err != nil { + return nil, err + } + return &Conn{ + conn, + }, nil +} + +type Conn struct { + conn redis.Conn +} + +func init() { + storage.Register("redis", &driver{}) +} diff --git a/storage/storage.go b/storage/storage.go index 600d591..3d1406b 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -15,7 +15,7 @@ import ( var drivers = make(map[string]StorageDriver) type StorageDriver interface { - New(*config.Storage) (Storage, error) + New(*config.Storage) (Conn, error) } func Register(name string, driver StorageDriver) { @@ -28,7 +28,7 @@ func Register(name string, driver StorageDriver) { drivers[name] = driver } -func New(conf *config.Storage) (Storage, error) { +func Open(conf *config.Storage) (Conn, error) { driver, ok := drivers[conf.Driver] if !ok { return nil, fmt.Errorf( @@ -43,7 +43,7 @@ func New(conf *config.Storage) (Storage, error) { return store, nil } -type Storage interface { +type Conn interface { Close() error FindUser(passkey string) (*User, bool, error)