mirror of
https://github.com/sot-tech/mochi.git
synced 2026-05-01 01:49:59 -07:00
overhaul everything
This commit is contained in:
72
drivers/backend/backend.go
Normal file
72
drivers/backend/backend.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// 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/models"
|
||||
)
|
||||
|
||||
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 models 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 {
|
||||
// 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 *models.AnnounceDelta) error
|
||||
|
||||
// LoadTorrents fetches and returns the specified torrents.
|
||||
LoadTorrents(ids []uint64) ([]*models.Torrent, error)
|
||||
|
||||
// LoadAllTorrents fetches and returns all torrents.
|
||||
LoadAllTorrents() ([]*models.Torrent, error)
|
||||
|
||||
// LoadUsers fetches and returns the specified users.
|
||||
LoadUsers(ids []uint64) ([]*models.User, error)
|
||||
|
||||
// LoadAllUsers fetches and returns all users.
|
||||
LoadAllUsers(ids []uint64) ([]*models.User, error)
|
||||
}
|
||||
81
drivers/backend/mock/driver.go
Normal file
81
drivers/backend/mock/driver.go
Normal file
@@ -0,0 +1,81 @@
|
||||
// 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 models interface for a BitTorrent tracker's
|
||||
// backend models. 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/drivers/backend"
|
||||
"github.com/chihaya/chihaya/models"
|
||||
)
|
||||
|
||||
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 []*models.AnnounceDelta
|
||||
deltaHistoryM sync.RWMutex
|
||||
}
|
||||
|
||||
func (d *driver) New(conf *config.DataStore) backend.Conn {
|
||||
return &Mock{}
|
||||
}
|
||||
|
||||
// Close returns nil.
|
||||
func (m *Mock) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RecordAnnounce adds a delta to the history.
|
||||
func (m *Mock) RecordAnnounce(delta *models.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() []models.AnnounceDelta {
|
||||
m.deltaHistoryM.Lock()
|
||||
defer m.deltaHistoryM.Unlock()
|
||||
|
||||
cp := make([]models.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) ([]*models.Torrent, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// LoadAllTorrents returns (nil, nil).
|
||||
func (m *Mock) LoadAllTorrents() ([]*models.Torrent, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// LoadUsers returns (nil, nil).
|
||||
func (m *Mock) LoadUsers(ids []uint64) ([]*models.User, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// LoadAllUsers returns (nil, nil).
|
||||
func (m *Mock) LoadAllUsers(ids []uint64) ([]*models.User, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
backend.Register("mock", &driver{})
|
||||
}
|
||||
Reference in New Issue
Block a user