overhaul everything

This commit is contained in:
Jimmy Zelinskie
2014-06-23 22:47:43 -04:00
parent 18f6c32d97
commit 3bfb3074b4
27 changed files with 1193 additions and 1143 deletions

View File

@@ -1,100 +0,0 @@
// 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.
// Package backend provides a generic interface for manipulating a
// BitTorrent tracker's consistent backend data store (usually for
// a web application).
package backend
import (
"fmt"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/storage"
)
var drivers = make(map[string]Driver)
// Driver represents an interface to a long-running connection with a
// consistent data store.
type Driver interface {
New(*config.DataStore) Conn
}
// Register makes a database driver available by the provided name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(name string, driver Driver) {
if driver == nil {
panic("backend: Register driver is nil")
}
if _, dup := drivers[name]; dup {
panic("backend: Register called twice for driver " + name)
}
drivers[name] = driver
}
// Open creates a connection specified by a storage configuration.
func Open(conf *config.DataStore) (Conn, error) {
driver, ok := drivers[conf.Driver]
if !ok {
return nil, fmt.Errorf(
"backend: unknown driver %q (forgotten import?)",
conf.Driver,
)
}
pool := driver.New(conf)
return pool, nil
}
// Conn represents a connection to the data store.
type Conn interface {
// Start is called once when the server starts.
// It starts any necessary goroutines a given driver requires, and sets
// up the driver's initial state
Start() error
// Close terminates connections to the database(s) and gracefully shuts
// down the driver
Close() error
// RecordAnnounce is called once per announce, and is passed the delta in
// statistics for the client peer since its last announce.
RecordAnnounce(delta *AnnounceDelta) error
// LoadTorrents fetches and returns the specified torrents.
LoadTorrents(ids []uint64) ([]*storage.Torrent, error)
// LoadAllTorrents fetches and returns all torrents.
LoadAllTorrents() ([]*storage.Torrent, error)
// LoadUsers fetches and returns the specified users.
LoadUsers(ids []uint64) ([]*storage.User, error)
// LoadAllUsers fetches and returns all users.
LoadAllUsers(ids []uint64) ([]*storage.User, error)
}
// AnnounceDelta contains a difference in statistics for a peer.
// It is used for communicating changes to be recorded by the driver.
type AnnounceDelta struct {
Peer *storage.Peer
Torrent *storage.Torrent
User *storage.User
// Created is true if this announce created a new peer or changed an existing peer's address
Created bool
// Uploaded contains the raw upload delta for this announce, in bytes
Uploaded uint64
// Downloaded contains the raw download delta for this announce, in bytes
Downloaded uint64
// Timestamp is the unix timestamp this announce occurred at
Timestamp int64
// Snatched is true if this announce completed the download
Snatched bool
}

View File

@@ -1,86 +0,0 @@
// 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.
// Package mock implements the storage interface for a BitTorrent tracker's
// backend storage. It can be used in production, but isn't recommended.
// Stored values will not persist if the tracker is restarted.
package mock
import (
"sync"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/storage"
"github.com/chihaya/chihaya/storage/backend"
)
type driver struct{}
// Mock is a concrete implementation of the backend.Conn interface (plus some
// debugging methods) that stores deltas in memory.
type Mock struct {
deltaHistory []*backend.AnnounceDelta
deltaHistoryM sync.RWMutex
}
func (d *driver) New(conf *config.DataStore) backend.Conn {
return &Mock{}
}
// Start returns nil.
func (m *Mock) Start() error {
return nil
}
// Close returns nil.
func (m *Mock) Close() error {
return nil
}
// RecordAnnounce adds a delta to the history.
func (m *Mock) RecordAnnounce(delta *backend.AnnounceDelta) error {
m.deltaHistoryM.Lock()
defer m.deltaHistoryM.Unlock()
m.deltaHistory = append(m.deltaHistory, delta)
return nil
}
// DeltaHistory safely copies and returns the history of recorded deltas.
func (m *Mock) DeltaHistory() []backend.AnnounceDelta {
m.deltaHistoryM.Lock()
defer m.deltaHistoryM.Unlock()
cp := make([]backend.AnnounceDelta, len(m.deltaHistory))
for index, delta := range m.deltaHistory {
cp[index] = *delta
}
return cp
}
// LoadTorrents returns (nil, nil).
func (m *Mock) LoadTorrents(ids []uint64) ([]*storage.Torrent, error) {
return nil, nil
}
// LoadAllTorrents returns (nil, nil).
func (m *Mock) LoadAllTorrents() ([]*storage.Torrent, error) {
return nil, nil
}
// LoadUsers returns (nil, nil).
func (m *Mock) LoadUsers(ids []uint64) ([]*storage.User, error) {
return nil, nil
}
// LoadAllUsers returns (nil, nil).
func (m *Mock) LoadAllUsers(ids []uint64) ([]*storage.User, error) {
return nil, nil
}
func init() {
backend.Register("mock", &driver{})
}

View File

@@ -1,57 +0,0 @@
// 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.
// Package storage implements the models for an abstraction over the
// multiple data stores used by a BitTorrent tracker.
package storage
import (
"strconv"
)
// Peer is the internal representation of a participant in a swarm.
type Peer struct {
ID string `json:"id"`
UserID uint64 `json:"user_id"`
TorrentID uint64 `json:"torrent_id"`
IP string `json:"ip"`
Port uint64 `json:"port"`
Uploaded uint64 `json:"uploaded"`
Downloaded uint64 `json:"downloaded`
Left uint64 `json:"left"`
LastAnnounce int64 `json:"last_announce"`
}
// PeerMapKey is a helper that returns the proper format for keys used for maps
// of peers (i.e. torrent.Seeders & torrent.Leechers).
func PeerMapKey(peer *Peer) string {
return peer.ID + ":" + strconv.FormatUint(peer.UserID, 36)
}
// Torrent is the internal representation of a swarm for a given torrent file.
type Torrent struct {
ID uint64 `json:"id"`
Infohash string `json:"infohash"`
Active bool `json:"active"`
Seeders map[string]Peer `json:"seeders"`
Leechers map[string]Peer `json:"leechers"`
Snatches uint64 `json:"snatches"`
UpMultiplier float64 `json:"up_multiplier"`
DownMultiplier float64 `json:"down_multiplier"`
LastAction int64 `json:"last_action"`
}
// User is the internal representation of registered user for private trackers.
type User struct {
ID uint64 `json:"id"`
Passkey string `json:"passkey"`
UpMultiplier float64 `json:"up_multiplier"`
DownMultiplier float64 `json:"down_multiplier"`
Snatches uint64 `json:"snatches"`
}

View File

@@ -1,257 +0,0 @@
// 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.
package mock
import (
"github.com/chihaya/chihaya/storage"
"github.com/chihaya/chihaya/storage/tracker"
)
type Conn struct {
*Pool
}
func (c *Conn) FindUser(passkey string) (*storage.User, bool, error) {
c.usersM.RLock()
defer c.usersM.RUnlock()
user, ok := c.users[passkey]
if !ok {
return nil, false, nil
}
u := *user
return &u, true, nil
}
func (c *Conn) FindTorrent(infohash string) (*storage.Torrent, bool, error) {
c.torrentsM.RLock()
defer c.torrentsM.RUnlock()
torrent, ok := c.torrents[infohash]
if !ok {
return nil, false, nil
}
t := *torrent
return &t, true, nil
}
func (c *Conn) ClientWhitelisted(peerID string) (bool, error) {
c.whitelistM.RLock()
defer c.whitelistM.RUnlock()
_, ok := c.whitelist[peerID]
if !ok {
return false, nil
}
return true, nil
}
func (c *Conn) RecordSnatch(u *storage.User, t *storage.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Snatches++
t.Snatches++
return nil
}
func (c *Conn) MarkActive(t *storage.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Active = true
t.Active = true
return nil
}
func (c *Conn) MarkInactive(t *storage.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Active = false
t.Active = false
return nil
}
func (c *Conn) AddLeecher(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Leechers[storage.PeerMapKey(p)] = *p
t.Leechers[storage.PeerMapKey(p)] = *p
return nil
}
func (c *Conn) AddSeeder(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Leechers[storage.PeerMapKey(p)] = *p
t.Leechers[storage.PeerMapKey(p)] = *p
return nil
}
func (c *Conn) RemoveLeecher(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
delete(torrent.Leechers, storage.PeerMapKey(p))
delete(t.Leechers, storage.PeerMapKey(p))
return nil
}
func (c *Conn) RemoveSeeder(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
delete(torrent.Seeders, storage.PeerMapKey(p))
delete(t.Seeders, storage.PeerMapKey(p))
return nil
}
func (c *Conn) SetLeecher(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Leechers[storage.PeerMapKey(p)] = *p
t.Leechers[storage.PeerMapKey(p)] = *p
return nil
}
func (c *Conn) SetSeeder(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Seeders[storage.PeerMapKey(p)] = *p
t.Seeders[storage.PeerMapKey(p)] = *p
return nil
}
func (c *Conn) LeecherFinished(t *storage.Torrent, p *storage.Peer) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, ok := c.torrents[t.Infohash]
if !ok {
return tracker.ErrMissingResource
}
torrent.Seeders[storage.PeerMapKey(p)] = *p
delete(torrent.Leechers, storage.PeerMapKey(p))
t.Seeders[storage.PeerMapKey(p)] = *p
delete(t.Leechers, storage.PeerMapKey(p))
return nil
}
func (c *Conn) AddTorrent(t *storage.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent := *t
c.torrents[t.Infohash] = &torrent
return nil
}
func (c *Conn) RemoveTorrent(t *storage.Torrent) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
delete(c.torrents, t.Infohash)
return nil
}
func (c *Conn) AddUser(u *storage.User) error {
c.usersM.Lock()
defer c.usersM.Unlock()
user := *u
c.users[u.Passkey] = &user
return nil
}
func (c *Conn) RemoveUser(u *storage.User) error {
c.usersM.Lock()
defer c.usersM.Unlock()
delete(c.users, u.Passkey)
return nil
}
func (c *Conn) WhitelistClient(peerID string) error {
c.whitelistM.Lock()
defer c.whitelistM.Unlock()
c.whitelist[peerID] = true
return nil
}
func (c *Conn) UnWhitelistClient(peerID string) error {
c.whitelistM.Lock()
defer c.whitelistM.Unlock()
delete(c.whitelist, peerID)
return nil
}

View File

@@ -1,28 +0,0 @@
// 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.
// Package mock implements the storage interface for a BitTorrent tracker
// within memory. It can be used in production, but isn't recommended.
// Stored values will not persist if the tracker is restarted.
package mock
import (
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/storage"
"github.com/chihaya/chihaya/storage/tracker"
)
type driver struct{}
func (d *driver) New(conf *config.DataStore) tracker.Pool {
return &Pool{
users: make(map[string]*storage.User),
torrents: make(map[string]*storage.Torrent),
whitelist: make(map[string]bool),
}
}
func init() {
tracker.Register("mock", &driver{})
}

View File

@@ -1,33 +0,0 @@
// 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.
package mock
import (
"sync"
"github.com/chihaya/chihaya/storage"
"github.com/chihaya/chihaya/storage/tracker"
)
type Pool struct {
users map[string]*storage.User
usersM sync.RWMutex
torrents map[string]*storage.Torrent
torrentsM sync.RWMutex
whitelist map[string]bool
whitelistM sync.RWMutex
}
func (p *Pool) Get() (tracker.Conn, error) {
return &Conn{
Pool: p,
}, nil
}
func (p *Pool) Close() error {
return nil
}

View File

@@ -1,89 +0,0 @@
// 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.
// Package tracker provides a generic interface for manipulating a
// BitTorrent tracker's fast-moving data.
package tracker
import (
"errors"
"fmt"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/storage"
)
var (
// ErrMissingResource is an error returned when a resource does not exist.
ErrMissingResource = errors.New("tracker: resource missing")
drivers = make(map[string]Driver)
)
// Driver represents an interface to pool of connections to storage used for
// the tracker.
type Driver interface {
New(*config.DataStore) Pool
}
// Register makes a database driver available by the provided name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(name string, driver Driver) {
if driver == nil {
panic("tracker: Register driver is nil")
}
if _, dup := drivers[name]; dup {
panic("tracker: Register called twice for driver " + name)
}
drivers[name] = driver
}
// Open creates a pool of data store connections specified by a storage configuration.
func Open(conf *config.DataStore) (Pool, error) {
driver, ok := drivers[conf.Driver]
if !ok {
return nil, fmt.Errorf(
"tracker: unknown driver %q (forgotten import?)",
conf.Driver,
)
}
pool := driver.New(conf)
return pool, nil
}
// Pool represents a thread-safe pool of connections to the data store
// that can be used to safely within concurrent goroutines.
type Pool interface {
Close() error
Get() (Conn, error)
}
// Conn represents a connection to the data store that can be used
// to make reads/writes.
type Conn interface {
// Reads
FindUser(passkey string) (*storage.User, bool, error)
FindTorrent(infohash string) (*storage.Torrent, bool, error)
ClientWhitelisted(peerID string) (bool, error)
// Writes
RecordSnatch(u *storage.User, t *storage.Torrent) error
MarkActive(t *storage.Torrent) error
AddLeecher(t *storage.Torrent, p *storage.Peer) error
AddSeeder(t *storage.Torrent, p *storage.Peer) error
RemoveLeecher(t *storage.Torrent, p *storage.Peer) error
RemoveSeeder(t *storage.Torrent, p *storage.Peer) error
SetLeecher(t *storage.Torrent, p *storage.Peer) error
SetSeeder(t *storage.Torrent, p *storage.Peer) error
LeecherFinished(t *storage.Torrent, p *storage.Peer) error
// Priming / Testing
AddTorrent(t *storage.Torrent) error
RemoveTorrent(t *storage.Torrent) error
AddUser(u *storage.User) error
RemoveUser(u *storage.User) error
WhitelistClient(peerID string) error
UnWhitelistClient(peerID string) error
}