more work towards new architecture
This commit is contained in:
parent
4226ecc59d
commit
107ec1dd15
6 changed files with 88 additions and 24 deletions
14
cache/cache.go
vendored
14
cache/cache.go
vendored
|
@ -2,7 +2,7 @@
|
||||||
// Use of this source code is governed by the BSD 2-Clause license,
|
// Use of this source code is governed by the BSD 2-Clause license,
|
||||||
// which can be found in the LICENSE file.
|
// which can be found in the LICENSE file.
|
||||||
|
|
||||||
// Package storage provides a generic interface for manipulating a
|
// Package cache provides a generic interface for manipulating a
|
||||||
// BitTorrent tracker's fast moving data.
|
// BitTorrent tracker's fast moving data.
|
||||||
package cache
|
package cache
|
||||||
|
|
||||||
|
@ -16,11 +16,11 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
drivers = make(map[string]Driver)
|
drivers = make(map[string]Driver)
|
||||||
ErrTxDone = errors.New("storage: Transaction has already been committed or rolled back")
|
ErrTxDone = errors.New("cache: Transaction has already been committed or rolled back")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Driver interface {
|
type Driver interface {
|
||||||
New(*config.Cache) Pool
|
New(*config.DataStore) Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register makes a database driver available by the provided name.
|
// Register makes a database driver available by the provided name.
|
||||||
|
@ -28,20 +28,20 @@ type Driver interface {
|
||||||
// it panics.
|
// it panics.
|
||||||
func Register(name string, driver Driver) {
|
func Register(name string, driver Driver) {
|
||||||
if driver == nil {
|
if driver == nil {
|
||||||
panic("storage: Register driver is nil")
|
panic("cache: Register driver is nil")
|
||||||
}
|
}
|
||||||
if _, dup := drivers[name]; dup {
|
if _, dup := drivers[name]; dup {
|
||||||
panic("storage: Register called twice for driver " + name)
|
panic("cache: Register called twice for driver " + name)
|
||||||
}
|
}
|
||||||
drivers[name] = driver
|
drivers[name] = driver
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open creates a pool of data store connections specified by a storage configuration.
|
// Open creates a pool of data store connections specified by a storage configuration.
|
||||||
func Open(conf *config.Cache) (Pool, error) {
|
func Open(conf *config.DataStore) (Pool, error) {
|
||||||
driver, ok := drivers[conf.Driver]
|
driver, ok := drivers[conf.Driver]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"storage: unknown driver %q (forgotten import?)",
|
"cache: unknown driver %q (forgotten import?)",
|
||||||
conf.Driver,
|
conf.Driver,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
8
cache/redis/redis.go
vendored
8
cache/redis/redis.go
vendored
|
@ -27,7 +27,7 @@ import (
|
||||||
|
|
||||||
type driver struct{}
|
type driver struct{}
|
||||||
|
|
||||||
func (d *driver) New(conf *config.Cache) cache.Pool {
|
func (d *driver) New(conf *config.DataStore) cache.Pool {
|
||||||
return &Pool{
|
return &Pool{
|
||||||
conf: conf,
|
conf: conf,
|
||||||
pool: redis.Pool{
|
pool: redis.Pool{
|
||||||
|
@ -39,7 +39,7 @@ func (d *driver) New(conf *config.Cache) cache.Pool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeDialFunc(conf *config.Cache) func() (redis.Conn, error) {
|
func makeDialFunc(conf *config.DataStore) func() (redis.Conn, error) {
|
||||||
return func() (conn redis.Conn, err error) {
|
return func() (conn redis.Conn, err error) {
|
||||||
conn, err = redis.Dial(conf.Network, conf.Addr)
|
conn, err = redis.Dial(conf.Network, conf.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -55,7 +55,7 @@ func testOnBorrow(c redis.Conn, t time.Time) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pool struct {
|
type Pool struct {
|
||||||
conf *config.Cache
|
conf *config.DataStore
|
||||||
pool redis.Pool
|
pool redis.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ func (p *Pool) Get() (cache.Tx, error) {
|
||||||
// SET keyB
|
// SET keyB
|
||||||
// EXEC
|
// EXEC
|
||||||
type Tx struct {
|
type Tx struct {
|
||||||
conf *config.Cache
|
conf *config.DataStore
|
||||||
done bool
|
done bool
|
||||||
multi bool
|
multi bool
|
||||||
redis.Conn
|
redis.Conn
|
||||||
|
|
|
@ -27,8 +27,8 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache represents the configuration for any data store used as a cache.
|
// DataStore represents the configuration used to connect to a data store.
|
||||||
type Cache struct {
|
type DataStore struct {
|
||||||
Driver string `json:"driver"`
|
Driver string `json:"driver"`
|
||||||
Network string `json:"network`
|
Network string `json:"network`
|
||||||
Addr string `json:"addr"`
|
Addr string `json:"addr"`
|
||||||
|
@ -38,16 +38,16 @@ type Cache struct {
|
||||||
Encoding string `json:"encoding,omitempty"`
|
Encoding string `json:"encoding,omitempty"`
|
||||||
Prefix string `json:"prefix,omitempty"`
|
Prefix string `json:"prefix,omitempty"`
|
||||||
|
|
||||||
MaxIdleConn int `json:"max_idle_conn"`
|
MaxIdleConn int `json:"max_idle_conn,omitempty"`
|
||||||
IdleTimeout *Duration `json:"idle_timeout"`
|
IdleTimeout *Duration `json:"idle_timeout,omitempty"`
|
||||||
TxRetries int `json:"tx_retries"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config represents a configuration for a server.Server.
|
// Config represents a configuration for a server.Server.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Addr string `json:"addr"`
|
Addr string `json:"addr"`
|
||||||
PubAddr string `json:"pub_addr"`
|
PubAddr string `json:"pub_addr"`
|
||||||
Cache Cache `json:"cache"`
|
Cache DataStore `json:"cache"`
|
||||||
|
Storage DataStore `json:"storage"`
|
||||||
|
|
||||||
Private bool `json:"private"`
|
Private bool `json:"private"`
|
||||||
Freeleech bool `json:"freeleech"`
|
Freeleech bool `json:"freeleech"`
|
||||||
|
@ -57,6 +57,8 @@ type Config struct {
|
||||||
MinAnnounce Duration `json:"min_announce"`
|
MinAnnounce Duration `json:"min_announce"`
|
||||||
ReadTimeout Duration `json:"read_timeout"`
|
ReadTimeout Duration `json:"read_timeout"`
|
||||||
DefaultNumWant int `json:"default_num_want"`
|
DefaultNumWant int `json:"default_num_want"`
|
||||||
|
|
||||||
|
TxRetries int `json:"tx_retries"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open is a shortcut to open a file, read it, and generate a Config.
|
// Open is a shortcut to open a file, read it, and generate a Config.
|
||||||
|
|
|
@ -17,6 +17,7 @@ var exampleJson = `{
|
||||||
"network": "tcp",
|
"network": "tcp",
|
||||||
"addr": ":34000",
|
"addr": ":34000",
|
||||||
"pub_addr": "tcp://*:34001",
|
"pub_addr": "tcp://*:34001",
|
||||||
|
|
||||||
"cache": {
|
"cache": {
|
||||||
"driver": "redis",
|
"driver": "redis",
|
||||||
"addr": "127.0.0.1:6379",
|
"addr": "127.0.0.1:6379",
|
||||||
|
@ -25,9 +26,14 @@ var exampleJson = `{
|
||||||
"prefix": "test:",
|
"prefix": "test:",
|
||||||
|
|
||||||
"max_idle_conn": 3,
|
"max_idle_conn": 3,
|
||||||
"idle_timeout": "240s",
|
"idle_timeout": "240s"
|
||||||
"conn_timeout": "5s",
|
},
|
||||||
"tx_retries": 3
|
|
||||||
|
"storage": {
|
||||||
|
"driver": "batter",
|
||||||
|
"addr": "127.0.0.1:5432",
|
||||||
|
"user": "postgres",
|
||||||
|
"pass": ""
|
||||||
},
|
},
|
||||||
|
|
||||||
"private": true,
|
"private": true,
|
||||||
|
@ -36,8 +42,9 @@ var exampleJson = `{
|
||||||
"announce": "30m",
|
"announce": "30m",
|
||||||
"min_announce": "15m",
|
"min_announce": "15m",
|
||||||
"read_timeout": "20s",
|
"read_timeout": "20s",
|
||||||
"default_num_want": 50
|
"default_num_want": 50,
|
||||||
|
|
||||||
|
"tx_retries": 3
|
||||||
}`
|
}`
|
||||||
|
|
||||||
func TestNewConfig(t *testing.T) {
|
func TestNewConfig(t *testing.T) {
|
||||||
|
|
|
@ -24,7 +24,7 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retry failed transactions a specified number of times
|
// Retry failed transactions a specified number of times
|
||||||
for i := 0; i < s.conf.Cache.TxRetries; i++ {
|
for i := 0; i < s.conf.TxRetries; i++ {
|
||||||
|
|
||||||
// Start a transaction
|
// Start a transaction
|
||||||
tx, err := s.dbConnPool.Get()
|
tx, err := s.dbConnPool.Get()
|
||||||
|
|
55
storage/storage.go
Normal file
55
storage/storage.go
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// 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 provides a generic interface for manipulating a
|
||||||
|
// BitTorrent tracker's web application data.
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/pushrax/chihaya/config"
|
||||||
|
"github.com/pushrax/chihaya/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
drivers = make(map[string]Driver)
|
||||||
|
)
|
||||||
|
|
||||||
|
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("storage: Register driver is nil")
|
||||||
|
}
|
||||||
|
if _, dup := drivers[name]; dup {
|
||||||
|
panic("storage: 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(
|
||||||
|
"storage: 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() error
|
||||||
|
UpdateTorrents(t []models.Torrent) error
|
||||||
|
UpdateUsers(u []models.User) error
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue