diff --git a/.travis.yml b/.travis.yml index 217ac68..016da69 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,6 @@ language: go go: 1.2 -env: - - TESTCONFIGPATH=/home/travis/gopath/src/github.com/chihaya/chihaya/config/example.json - services: - redis-server diff --git a/README.md b/README.md index b945609..cecc926 100644 --- a/README.md +++ b/README.md @@ -42,12 +42,8 @@ flag. An example configuration file can be found ### Running the tests -The tests require a running redis server to function correctly, and a config to use for the tests. -The `$TESTCONFIGPATH` environment variable is required for the tests to function: - ```sh $ cd $GOPATH/src/github.com/chihaya/chihaya -$ export TESTCONFIGPATH=$GOPATH/src/github.com/chihaya/chihaya/config/example.json $ go test -v ./... ``` @@ -59,11 +55,12 @@ are a number of drivers that will be directly supported: Tracker: +* mock (memory) * [redis](https://github.com/chihaya/chihaya-redis) -* memory Backend: +* mock (memory) * [gazelle (mysql)](https://github.com/chihaya/chihaya-gazelle) [implement a new driver]: https://github.com/chihaya/chihaya/wiki/Implementing-a-driver diff --git a/config/config.go b/config/config.go index 537d6f8..d7a0dd7 100644 --- a/config/config.go +++ b/config/config.go @@ -46,13 +46,11 @@ type DataStore struct { // Config represents a configuration for a server.Server. type Config struct { Addr string `json:"addr"` - PubAddr string `json:"pub_addr"` - Cache DataStore `json:"cache"` - Storage DataStore `json:"storage"` + Tracker DataStore `json:"tracker"` + Backend DataStore `json:"backend"` Private bool `json:"private"` Freeleech bool `json:"freeleech"` - Slots bool `json:"slots"` Announce Duration `json:"announce"` MinAnnounce Duration `json:"min_announce"` diff --git a/config/config_test.go b/config/config_test.go deleted file mode 100644 index 8ee6884..0000000 --- a/config/config_test.go +++ /dev/null @@ -1,29 +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 config - -import ( - "bytes" - "io/ioutil" - "os" - "testing" -) - -func TestOpenConfig(t *testing.T) { - if _, err := Open(os.Getenv("TESTCONFIGPATH")); err != nil { - t.Error(err) - } -} - -func TestNewConfig(t *testing.T) { - contents, err := ioutil.ReadFile(os.Getenv("TESTCONFIGPATH")) - if err != nil { - t.Error(err) - } - buff := bytes.NewBuffer(contents) - if _, err := newConfig(buff); err != nil { - t.Error(err) - } -} diff --git a/config/example.json b/config/example.json deleted file mode 100644 index 90c098f..0000000 --- a/config/example.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - - "network": "tcp", - "addr": ":80", - - "cache": { - "driver": "mock", - "network": "tcp", - "host": "127.0.0.1", - "port": "6379", - "user": "root", - "pass": "", - "prefix": "test:", - - "max_idle_conns": 3, - "idle_timeout": "240s" - }, - - "storage": { - "driver": "gazelle", - "host": "127.0.0.1", - "port": "5432", - "user": "postgres", - "pass": "" - }, - - "private": true, - "freeleech": false, - - "announce": "30m", - "min_announce": "15m", - "read_timeout": "20s", - "default_num_want": 50 - -} diff --git a/config/mock_config.go b/config/mock_config.go new file mode 100644 index 0000000..77abff3 --- /dev/null +++ b/config/mock_config.go @@ -0,0 +1,25 @@ +// 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 config + +import ( + "time" +) + +var MockConfig = Config{ + Addr: ":80", + Tracker: DataStore{ + Driver: "mock", + }, + Backend: DataStore{ + Driver: "mock", + }, + Private: true, + Freeleech: false, + Announce: Duration{30 * time.Minute}, + MinAnnounce: Duration{15 * time.Minute}, + ReadTimeout: Duration{20 % time.Second}, + DefaultNumWant: 50, +} diff --git a/main.go b/main.go index c70daf2..eb0bc09 100644 --- a/main.go +++ b/main.go @@ -14,9 +14,6 @@ import ( "github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/server" - - _ "github.com/chihaya/chihaya-gazelle" - _ "github.com/chihaya/chihaya-redis" ) var ( diff --git a/server/announce.go b/server/announce.go index de96494..809e997 100644 --- a/server/announce.go +++ b/server/announce.go @@ -114,14 +114,6 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) { } default: - // Check the user's slots to see if they're allowed to leech - if s.conf.Slots && user.Slots != -1 && left != 0 { - if user.SlotsUsed >= user.Slots { - fail(errors.New("You've run out of download slots."), w, r) - return - } - } - if left == 0 { // Save the peer as a new seeder err := conn.AddSeeder(torrent, peer) @@ -129,11 +121,6 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) { log.Panicf("server: %s", err) } } else { - // Save the peer as a new leecher and increment the user's slots - err := conn.IncrementSlots(user) - if err != nil { - log.Panicf("server: %s", err) - } err = conn.AddLeecher(torrent, peer) if err != nil { log.Panicf("server: %s", err) @@ -155,10 +142,6 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) { if err != nil { log.Panicf("server: %s", err) } - err = conn.DecrementSlots(user) - if err != nil { - log.Panicf("server: %s", err) - } } case event == "completed": diff --git a/server/server.go b/server/server.go index e38841c..1b9e3ae 100644 --- a/server/server.go +++ b/server/server.go @@ -37,7 +37,7 @@ type Server struct { } func New(conf *config.Config) (*Server, error) { - pool, err := tracker.Open(&conf.Cache) + pool, err := tracker.Open(&conf.Tracker) if err != nil { return nil, err } diff --git a/server/stats_test.go b/server/stats_test.go index 7b6dd2f..0161270 100644 --- a/server/stats_test.go +++ b/server/stats_test.go @@ -8,22 +8,15 @@ import ( "errors" "net/http" "net/http/httptest" - "os" "testing" "github.com/chihaya/chihaya/config" - - _ "github.com/chihaya/chihaya-gazelle" + _ "github.com/chihaya/chihaya/storage/backend/mock" _ "github.com/chihaya/chihaya/storage/tracker/mock" ) func newTestServer() (*Server, error) { - testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH")) - if err != nil { - return nil, err - } - - s, err := New(testConfig) + s, err := New(&config.MockConfig) if err != nil { return nil, err } diff --git a/storage/backend/mock/driver.go b/storage/backend/mock/driver.go new file mode 100644 index 0000000..b8c7224 --- /dev/null +++ b/storage/backend/mock/driver.go @@ -0,0 +1,23 @@ +// 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 ( + "github.com/chihaya/chihaya/config" + "github.com/chihaya/chihaya/storage/backend" +) + +type driver struct{} + +func (d *driver) New(conf *config.DataStore) backend.Conn { + return nil +} + +func init() { + backend.Register("mock", &driver{}) +} diff --git a/storage/tracker/tracker.go b/storage/tracker/tracker.go index 11cd38f..18e3a66 100644 --- a/storage/tracker/tracker.go +++ b/storage/tracker/tracker.go @@ -67,15 +67,12 @@ type Conn interface { // Writes RecordSnatch(u *storage.User, t *storage.Torrent) error MarkActive(t *storage.Torrent) error - MarkInactive(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 - IncrementSlots(u *storage.User) error - DecrementSlots(u *storage.User) error LeecherFinished(t *storage.Torrent, p *storage.Peer) error // Priming / Testing