2013-06-21 21:43:11 -04:00
|
|
|
// Copyright 2013 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
2013-07-05 06:50:52 -04:00
|
|
|
// Package redis implements the storage interface for a BitTorrent tracker.
|
2013-07-24 03:08:38 -04:00
|
|
|
//
|
2013-07-24 00:18:43 -04:00
|
|
|
// The client whitelist is represented as a set with the key name "whitelist"
|
|
|
|
// with an optional prefix. Torrents and users are JSON-formatted strings.
|
2013-07-24 03:08:38 -04:00
|
|
|
// Torrents' keys are named "torrent:<infohash>" with an optional prefix.
|
|
|
|
// Users' keys are named "user:<passkey>" with an optional prefix.
|
2013-06-21 19:31:32 -04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2013-07-24 00:18:43 -04:00
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
2013-06-25 21:58:06 -04:00
|
|
|
"time"
|
|
|
|
|
2013-06-24 14:18:32 -04:00
|
|
|
"github.com/garyburd/redigo/redis"
|
|
|
|
|
|
|
|
"github.com/pushrax/chihaya/config"
|
2013-06-21 21:43:11 -04:00
|
|
|
"github.com/pushrax/chihaya/storage"
|
2013-06-21 19:31:32 -04:00
|
|
|
)
|
2013-06-24 14:18:32 -04:00
|
|
|
|
|
|
|
type driver struct{}
|
|
|
|
|
2013-07-05 06:50:52 -04:00
|
|
|
func (d *driver) New(conf *config.Storage) storage.DS {
|
|
|
|
return &DS{
|
2013-06-25 21:58:06 -04:00
|
|
|
conf: conf,
|
2013-07-05 06:50:52 -04:00
|
|
|
Pool: redis.Pool{
|
|
|
|
MaxIdle: conf.MaxIdleConn,
|
|
|
|
IdleTimeout: conf.IdleTimeout.Duration,
|
2013-06-25 23:08:54 -04:00
|
|
|
Dial: makeDialFunc(conf),
|
|
|
|
TestOnBorrow: testOnBorrow,
|
2013-06-25 21:58:06 -04:00
|
|
|
},
|
2013-07-03 18:24:03 -04:00
|
|
|
}
|
2013-06-24 14:18:32 -04:00
|
|
|
}
|
|
|
|
|
2013-06-25 23:08:54 -04:00
|
|
|
func makeDialFunc(conf *config.Storage) func() (redis.Conn, error) {
|
|
|
|
return func() (redis.Conn, error) {
|
|
|
|
var (
|
|
|
|
conn redis.Conn
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2013-07-05 06:50:52 -04:00
|
|
|
if conf.ConnTimeout != nil {
|
2013-06-25 23:08:54 -04:00
|
|
|
conn, err = redis.DialTimeout(
|
|
|
|
conf.Network,
|
|
|
|
conf.Addr,
|
2013-07-05 06:50:52 -04:00
|
|
|
conf.ConnTimeout.Duration, // Connect Timeout
|
|
|
|
conf.ConnTimeout.Duration, // Read Timeout
|
|
|
|
conf.ConnTimeout.Duration, // Write Timeout
|
2013-06-25 23:08:54 -04:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
conn, err = redis.Dial(conf.Network, conf.Addr)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testOnBorrow(c redis.Conn, t time.Time) error {
|
|
|
|
_, err := c.Do("PING")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-05 06:50:52 -04:00
|
|
|
type DS struct {
|
2013-07-03 18:24:03 -04:00
|
|
|
conf *config.Storage
|
2013-07-05 06:50:52 -04:00
|
|
|
redis.Pool
|
2013-07-03 18:24:03 -04:00
|
|
|
}
|
|
|
|
|
2013-07-05 06:50:52 -04:00
|
|
|
func (ds *DS) FindUser(passkey string) (*storage.User, bool, error) {
|
|
|
|
conn := ds.Get()
|
|
|
|
defer conn.Close()
|
2013-06-25 21:58:06 -04:00
|
|
|
|
2013-07-24 03:08:38 -04:00
|
|
|
key := ds.conf.Prefix + "user:" + passkey
|
2013-07-24 00:18:43 -04:00
|
|
|
reply, err := redis.String(conn.Do("GET", key))
|
2013-06-25 21:58:06 -04:00
|
|
|
if err != nil {
|
2013-07-24 00:18:43 -04:00
|
|
|
if err == redis.ErrNil {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
return nil, false, err
|
2013-06-25 21:58:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
user := &storage.User{}
|
2013-07-24 00:18:43 -04:00
|
|
|
err = json.NewDecoder(strings.NewReader(reply)).Decode(user)
|
2013-06-25 21:58:06 -04:00
|
|
|
if err != nil {
|
2013-06-28 16:29:02 -04:00
|
|
|
return nil, true, err
|
2013-06-25 21:58:06 -04:00
|
|
|
}
|
|
|
|
return user, true, nil
|
|
|
|
}
|
|
|
|
|
2013-07-05 06:50:52 -04:00
|
|
|
func (ds *DS) FindTorrent(infohash string) (*storage.Torrent, bool, error) {
|
|
|
|
conn := ds.Get()
|
|
|
|
defer conn.Close()
|
2013-06-25 21:58:06 -04:00
|
|
|
|
2013-07-24 03:08:38 -04:00
|
|
|
key := ds.conf.Prefix + "torrent:" + infohash
|
2013-07-24 00:18:43 -04:00
|
|
|
reply, err := redis.String(conn.Do("GET", key))
|
2013-06-25 21:58:06 -04:00
|
|
|
if err != nil {
|
2013-07-24 00:18:43 -04:00
|
|
|
if err == redis.ErrNil {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2013-06-25 21:58:06 -04:00
|
|
|
return nil, false, err
|
|
|
|
}
|
2013-07-05 06:50:52 -04:00
|
|
|
|
2013-06-25 21:58:06 -04:00
|
|
|
torrent := &storage.Torrent{}
|
2013-07-24 00:18:43 -04:00
|
|
|
err = json.NewDecoder(strings.NewReader(reply)).Decode(torrent)
|
2013-06-25 21:58:06 -04:00
|
|
|
if err != nil {
|
2013-06-28 16:29:02 -04:00
|
|
|
return nil, true, err
|
2013-06-25 21:58:06 -04:00
|
|
|
}
|
|
|
|
return torrent, true, nil
|
|
|
|
}
|
|
|
|
|
2013-07-05 06:50:52 -04:00
|
|
|
func (ds *DS) ClientWhitelisted(peerID string) (bool, error) {
|
|
|
|
conn := ds.Get()
|
|
|
|
defer conn.Close()
|
|
|
|
|
2013-07-24 00:18:43 -04:00
|
|
|
key := ds.conf.Prefix + "whitelist"
|
|
|
|
exists, err := redis.Bool(conn.Do("SISMEMBER", key, peerID))
|
2013-07-05 06:50:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return exists, nil
|
|
|
|
}
|
|
|
|
|
2013-07-03 18:24:03 -04:00
|
|
|
type Tx struct {
|
2013-07-05 06:50:52 -04:00
|
|
|
conf *config.Storage
|
|
|
|
done bool
|
|
|
|
redis.Conn
|
2013-07-03 18:24:03 -04:00
|
|
|
}
|
|
|
|
|
2013-07-05 06:50:52 -04:00
|
|
|
func (ds *DS) Begin() (storage.Tx, error) {
|
|
|
|
conn := ds.Get()
|
|
|
|
err := conn.Send("MULTI")
|
2013-07-03 18:24:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-05 06:50:52 -04:00
|
|
|
return &Tx{
|
|
|
|
conf: ds.conf,
|
|
|
|
Conn: conn,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2013-07-21 20:15:48 -04:00
|
|
|
func (tx *Tx) close() {
|
|
|
|
if tx.done {
|
2013-07-05 06:50:52 -04:00
|
|
|
panic("redis: transaction closed twice")
|
|
|
|
}
|
2013-07-21 20:15:48 -04:00
|
|
|
tx.done = true
|
|
|
|
tx.Conn.Close()
|
2013-07-03 18:24:03 -04:00
|
|
|
}
|
|
|
|
|
2013-07-21 20:15:48 -04:00
|
|
|
func (tx *Tx) Commit() error {
|
|
|
|
if tx.done {
|
2013-07-05 06:50:52 -04:00
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
2013-07-21 20:15:48 -04:00
|
|
|
_, err := tx.Do("EXEC")
|
2013-07-03 18:24:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-21 20:15:48 -04:00
|
|
|
|
|
|
|
tx.close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redis doesn't need to rollback. Exec is atomic.
|
|
|
|
func (tx *Tx) Rollback() error {
|
|
|
|
if tx.done {
|
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
|
|
|
tx.close()
|
2013-07-03 18:24:03 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-21 20:15:48 -04:00
|
|
|
func (tx *Tx) Snatch(user *storage.User, torrent *storage.Torrent) error {
|
|
|
|
if tx.done {
|
2013-07-05 06:50:52 -04:00
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
2013-07-21 20:15:48 -04:00
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-24 03:08:38 -04:00
|
|
|
func (tx *Tx) Active(t *storage.Torrent) error {
|
2013-07-21 20:15:48 -04:00
|
|
|
if tx.done {
|
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
2013-07-24 03:08:38 -04:00
|
|
|
key := tx.conf.Prefix + "torrent:" + t.Infohash
|
|
|
|
err := activeScript.Send(tx.Conn, key)
|
|
|
|
return err
|
2013-07-21 20:15:48 -04:00
|
|
|
}
|
2013-07-05 06:50:52 -04:00
|
|
|
|
2013-07-21 20:15:48 -04:00
|
|
|
func (tx *Tx) NewLeecher(t *storage.Torrent, p *storage.Peer) error {
|
|
|
|
if tx.done {
|
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
|
|
|
// TODO
|
2013-07-05 06:50:52 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-23 21:23:43 -04:00
|
|
|
func (tx *Tx) SetLeecher(t *storage.Torrent, p *storage.Peer) error {
|
|
|
|
if tx.done {
|
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-21 20:15:48 -04:00
|
|
|
func (tx *Tx) RmLeecher(t *storage.Torrent, p *storage.Peer) error {
|
|
|
|
if tx.done {
|
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) NewSeeder(t *storage.Torrent, p *storage.Peer) error {
|
|
|
|
if tx.done {
|
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-23 21:23:43 -04:00
|
|
|
func (tx *Tx) SetSeeder(t *storage.Torrent, p *storage.Peer) error {
|
|
|
|
if tx.done {
|
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-21 20:15:48 -04:00
|
|
|
func (tx *Tx) RmSeeder(t *storage.Torrent, p *storage.Peer) error {
|
|
|
|
if tx.done {
|
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
2013-07-24 03:08:38 -04:00
|
|
|
key := tx.conf.Prefix + "torrent:" + t.Infohash
|
|
|
|
err := rmSeederScript.Send(tx.Conn, key, p.ID)
|
|
|
|
return err
|
2013-07-21 20:15:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) IncrementSlots(u *storage.User) error {
|
|
|
|
if tx.done {
|
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
2013-07-24 03:08:38 -04:00
|
|
|
key := tx.conf.Prefix + "user:" + u.Passkey
|
|
|
|
err := incSlotsScript.Send(tx.Conn, key)
|
|
|
|
return err
|
2013-07-21 20:15:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) DecrementSlots(u *storage.User) error {
|
|
|
|
if tx.done {
|
2013-07-05 06:50:52 -04:00
|
|
|
return storage.ErrTxDone
|
|
|
|
}
|
2013-07-24 03:08:38 -04:00
|
|
|
key := tx.conf.Prefix + "user:" + u.Passkey
|
|
|
|
err := decSlotsScript.Send(tx.Conn, key)
|
|
|
|
return err
|
2013-06-24 14:18:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
storage.Register("redis", &driver{})
|
|
|
|
}
|