initial redis work, storage.Storage->storage.Conn,

among other new options in the config file for storage timeouts
This commit is contained in:
Jimmy Zelinskie 2013-06-24 14:18:32 -04:00
parent 79a6c79067
commit 56132e3d64
5 changed files with 60 additions and 10 deletions

View file

@ -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 {

View file

@ -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,

View file

@ -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")
}

View file

@ -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{})
}

View file

@ -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)