2013-06-22 03:43:11 +02: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 12:50:52 +02:00
|
|
|
// Package redis implements the storage interface for a BitTorrent tracker.
|
2013-07-24 09:08:38 +02:00
|
|
|
//
|
2013-07-24 06:18:43 +02:00
|
|
|
// The client whitelist is represented as a set with the key name "whitelist"
|
2013-08-24 06:24:42 +02:00
|
|
|
// with an optional prefix. Torrents and users are represented as hashes.
|
2013-07-24 09:08:38 +02:00
|
|
|
// Torrents' keys are named "torrent:<infohash>" with an optional prefix.
|
2013-08-24 06:24:42 +02:00
|
|
|
// Users' keys are named "user:<passkey>" with an optional prefix. The
|
|
|
|
// seeders and leechers attributes of torrent hashes are strings that represent
|
|
|
|
// the key for those hashes within redis. This is done because redis cannot
|
|
|
|
// nest their hash data type.
|
2013-06-22 01:31:32 +02:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2013-07-24 06:18:43 +02:00
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
2013-06-26 03:58:06 +02:00
|
|
|
"time"
|
|
|
|
|
2013-06-24 20:18:32 +02:00
|
|
|
"github.com/garyburd/redigo/redis"
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
"github.com/pushrax/chihaya/cache"
|
2013-06-24 20:18:32 +02:00
|
|
|
"github.com/pushrax/chihaya/config"
|
2013-08-23 21:39:42 +02:00
|
|
|
"github.com/pushrax/chihaya/models"
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
2013-06-24 20:18:32 +02:00
|
|
|
|
|
|
|
type driver struct{}
|
|
|
|
|
2013-08-26 20:38:45 +02:00
|
|
|
func (d *driver) New(conf *config.DataStore) cache.Pool {
|
2013-07-25 16:03:04 +02:00
|
|
|
return &Pool{
|
2013-06-26 03:58:06 +02:00
|
|
|
conf: conf,
|
2013-07-25 16:03:04 +02:00
|
|
|
pool: redis.Pool{
|
2013-07-05 12:50:52 +02:00
|
|
|
MaxIdle: conf.MaxIdleConn,
|
|
|
|
IdleTimeout: conf.IdleTimeout.Duration,
|
2013-06-26 05:08:54 +02:00
|
|
|
Dial: makeDialFunc(conf),
|
|
|
|
TestOnBorrow: testOnBorrow,
|
2013-06-26 03:58:06 +02:00
|
|
|
},
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
2013-06-24 20:18:32 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 20:38:45 +02:00
|
|
|
func makeDialFunc(conf *config.DataStore) func() (redis.Conn, error) {
|
2013-08-14 02:21:01 +02:00
|
|
|
return func() (conn redis.Conn, err error) {
|
2013-08-29 05:44:45 +02:00
|
|
|
conn, err = redis.Dial(conf.Network, conf.Host+":"+conf.Port)
|
2013-06-26 05:08:54 +02:00
|
|
|
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-25 16:03:04 +02:00
|
|
|
type Pool struct {
|
2013-08-26 20:38:45 +02:00
|
|
|
conf *config.DataStore
|
2013-07-25 16:03:04 +02:00
|
|
|
pool redis.Pool
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
func (p *Pool) Close() error {
|
|
|
|
return p.pool.Close()
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (p *Pool) Get() (cache.Tx, error) {
|
2013-07-25 16:03:04 +02:00
|
|
|
return &Tx{
|
|
|
|
conf: p.conf,
|
|
|
|
done: false,
|
|
|
|
multi: false,
|
|
|
|
Conn: p.pool.Get(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tx represents a transaction for Redis with one gotcha:
|
|
|
|
// all reads must be done prior to any writes. Writes will
|
|
|
|
// check if the MULTI command has been sent to redis and will
|
|
|
|
// send it if it hasn't.
|
|
|
|
//
|
|
|
|
// Internally a transaction looks like:
|
|
|
|
// WATCH keyA
|
|
|
|
// GET keyA
|
|
|
|
// WATCH keyB
|
|
|
|
// GET keyB
|
|
|
|
// MULTI
|
|
|
|
// SET keyA
|
|
|
|
// SET keyB
|
|
|
|
// EXEC
|
|
|
|
type Tx struct {
|
2013-08-26 20:38:45 +02:00
|
|
|
conf *config.DataStore
|
2013-07-25 16:03:04 +02:00
|
|
|
done bool
|
|
|
|
multi bool
|
|
|
|
redis.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) close() {
|
|
|
|
if tx.done {
|
|
|
|
panic("redis: transaction closed twice")
|
|
|
|
}
|
|
|
|
tx.done = true
|
|
|
|
tx.Conn.Close()
|
|
|
|
}
|
|
|
|
|
2013-08-13 08:20:44 +02:00
|
|
|
func (tx *Tx) initiateWrite() error {
|
|
|
|
if tx.done {
|
2013-08-23 21:39:42 +02:00
|
|
|
return cache.ErrTxDone
|
2013-08-13 08:20:44 +02:00
|
|
|
}
|
|
|
|
if tx.multi != true {
|
|
|
|
return tx.Send("MULTI")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-13 22:06:22 +02:00
|
|
|
func (tx *Tx) initiateRead() error {
|
|
|
|
if tx.done {
|
2013-08-23 21:39:42 +02:00
|
|
|
return cache.ErrTxDone
|
2013-08-13 22:06:22 +02:00
|
|
|
}
|
|
|
|
if tx.multi == true {
|
2013-08-13 22:36:10 +02:00
|
|
|
panic("Tried to read during MULTI")
|
2013-08-13 22:06:22 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
func (tx *Tx) Commit() error {
|
|
|
|
if tx.done {
|
2013-08-23 21:39:42 +02:00
|
|
|
return cache.ErrTxDone
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
if tx.multi == true {
|
|
|
|
_, err := tx.Do("EXEC")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) Rollback() error {
|
|
|
|
if tx.done {
|
2013-08-23 21:39:42 +02:00
|
|
|
return cache.ErrTxDone
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
// Redis doesn't need to do anything. Exec is atomic.
|
|
|
|
tx.close()
|
|
|
|
return nil
|
|
|
|
}
|
2013-06-26 03:58:06 +02:00
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) FindUser(passkey string) (*models.User, bool, error) {
|
2013-08-13 22:06:22 +02:00
|
|
|
err := tx.initiateRead()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
key := tx.conf.Prefix + "user:" + passkey
|
2013-08-13 22:06:22 +02:00
|
|
|
_, err = tx.Do("WATCH", key)
|
2013-07-25 16:03:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
reply, err := redis.String(tx.Do("GET", key))
|
2013-06-26 03:58:06 +02:00
|
|
|
if err != nil {
|
2013-07-24 06:18:43 +02:00
|
|
|
if err == redis.ErrNil {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
return nil, false, err
|
2013-06-26 03:58:06 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
user := &models.User{}
|
2013-07-24 06:18:43 +02:00
|
|
|
err = json.NewDecoder(strings.NewReader(reply)).Decode(user)
|
2013-06-26 03:58:06 +02:00
|
|
|
if err != nil {
|
2013-06-28 22:29:02 +02:00
|
|
|
return nil, true, err
|
2013-06-26 03:58:06 +02:00
|
|
|
}
|
|
|
|
return user, true, nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) FindTorrent(infohash string) (*models.Torrent, bool, error) {
|
2013-08-13 22:06:22 +02:00
|
|
|
err := tx.initiateRead()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-06-26 03:58:06 +02:00
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
key := tx.conf.Prefix + "torrent:" + infohash
|
2013-08-13 22:06:22 +02:00
|
|
|
_, err = tx.Do("WATCH", key)
|
2013-07-25 16:03:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
reply, err := redis.String(tx.Do("GET", key))
|
2013-06-26 03:58:06 +02:00
|
|
|
if err != nil {
|
2013-07-24 06:18:43 +02:00
|
|
|
if err == redis.ErrNil {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2013-06-26 03:58:06 +02:00
|
|
|
return nil, false, err
|
|
|
|
}
|
2013-07-05 12:50:52 +02:00
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
torrent := &models.Torrent{}
|
2013-07-24 06:18:43 +02:00
|
|
|
err = json.NewDecoder(strings.NewReader(reply)).Decode(torrent)
|
2013-06-26 03:58:06 +02:00
|
|
|
if err != nil {
|
2013-06-28 22:29:02 +02:00
|
|
|
return nil, true, err
|
2013-06-26 03:58:06 +02:00
|
|
|
}
|
|
|
|
return torrent, true, nil
|
|
|
|
}
|
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
func (tx *Tx) ClientWhitelisted(peerID string) (exists bool, err error) {
|
2013-08-13 22:06:22 +02:00
|
|
|
err = tx.initiateRead()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2013-07-05 12:50:52 +02:00
|
|
|
}
|
2013-07-04 00:24:03 +02:00
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
key := tx.conf.Prefix + "whitelist"
|
|
|
|
_, err = tx.Do("WATCH", key)
|
2013-07-04 00:24:03 +02:00
|
|
|
if err != nil {
|
2013-07-25 16:03:04 +02:00
|
|
|
return
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
2013-07-05 12:50:52 +02:00
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
// TODO
|
|
|
|
return
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) RecordSnatch(user *models.User, torrent *models.Torrent) error {
|
2013-08-13 08:20:44 +02:00
|
|
|
if err := tx.initiateWrite(); err != nil {
|
|
|
|
return err
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
2013-07-22 02:15:48 +02:00
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
// TODO
|
2013-07-22 02:15:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) MarkActive(t *models.Torrent) error {
|
2013-08-13 08:20:44 +02:00
|
|
|
if err := tx.initiateWrite(); err != nil {
|
|
|
|
return err
|
2013-07-05 12:50:52 +02:00
|
|
|
}
|
2013-07-25 16:03:04 +02:00
|
|
|
|
2013-07-22 02:15:48 +02:00
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) AddLeecher(t *models.Torrent, p *models.Peer) error {
|
2013-08-13 08:20:44 +02:00
|
|
|
if err := tx.initiateWrite(); err != nil {
|
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
2013-07-22 02:15:48 +02:00
|
|
|
// TODO
|
2013-07-05 12:50:52 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) SetLeecher(t *models.Torrent, p *models.Peer) error {
|
2013-08-13 08:20:44 +02:00
|
|
|
if err := tx.initiateWrite(); err != nil {
|
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 03:23:43 +02:00
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) RemoveLeecher(t *models.Torrent, p *models.Peer) error {
|
2013-08-13 08:20:44 +02:00
|
|
|
if err := tx.initiateWrite(); err != nil {
|
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
2013-07-22 02:15:48 +02:00
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) AddSeeder(t *models.Torrent, p *models.Peer) error {
|
2013-08-13 08:20:44 +02:00
|
|
|
if err := tx.initiateWrite(); err != nil {
|
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
2013-07-22 02:15:48 +02:00
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) SetSeeder(t *models.Torrent, p *models.Peer) error {
|
2013-08-13 08:20:44 +02:00
|
|
|
if err := tx.initiateWrite(); err != nil {
|
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 03:23:43 +02:00
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) RemoveSeeder(t *models.Torrent, p *models.Peer) error {
|
2013-08-13 08:20:44 +02:00
|
|
|
if err := tx.initiateWrite(); err != nil {
|
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
return nil
|
2013-07-22 02:15:48 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) IncrementSlots(u *models.User) error {
|
2013-08-13 08:20:44 +02:00
|
|
|
if err := tx.initiateWrite(); err != nil {
|
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
return nil
|
2013-07-22 02:15:48 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) DecrementSlots(u *models.User) error {
|
2013-08-13 08:20:44 +02:00
|
|
|
if err := tx.initiateWrite(); err != nil {
|
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
return nil
|
2013-06-24 20:18:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2013-08-23 21:39:42 +02:00
|
|
|
cache.Register("redis", &driver{})
|
2013-06-24 20:18:32 +02:00
|
|
|
}
|