tracker/backend/backend.go

76 lines
2.2 KiB
Go
Raw Normal View History

2015-01-01 18:02:25 +01:00
// Copyright 2015 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-10-21 09:29:35 +02:00
// Package backend provides a generic interface for manipulating a
// BitTorrent tracker's consistent backend data store (usually for
// a web application).
2013-10-21 09:29:35 +02:00
package backend
import (
"fmt"
2013-12-01 04:39:02 +01:00
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/tracker/models"
)
var drivers = make(map[string]Driver)
2014-05-01 06:30:46 +02:00
// Driver represents an interface to a long-running connection with a
// consistent data store.
type Driver interface {
New(*config.DriverConfig) (Conn, error)
}
// 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 configuration.
func Open(cfg *config.DriverConfig) (Conn, error) {
driver, ok := drivers[cfg.Name]
if !ok {
return nil, fmt.Errorf(
"backend: unknown driver %q (forgotten import?)",
cfg.Name,
)
}
return driver.New(cfg)
}
// 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
2015-02-08 23:14:48 +01:00
// Ping just checks to see if the database is still alive. This is typically
// used for health checks.
Ping() error
// RecordAnnounce is called once per announce, and is passed the delta in
// statistics for the client peer since its last announce.
2014-06-24 04:47:43 +02:00
RecordAnnounce(delta *models.AnnounceDelta) error
// LoadTorrents fetches and returns the specified torrents.
2014-06-24 04:47:43 +02:00
LoadTorrents(ids []uint64) ([]*models.Torrent, error)
// LoadAllTorrents fetches and returns all torrents.
2014-06-24 04:47:43 +02:00
LoadAllTorrents() ([]*models.Torrent, error)
// LoadUsers fetches and returns the specified users.
2014-06-24 04:47:43 +02:00
LoadUsers(ids []uint64) ([]*models.User, error)
// LoadAllUsers fetches and returns all users.
LoadAllUsers() ([]*models.User, error)
}