From 080a24c7be20b1537fa5a30e6253cc8da405924e Mon Sep 17 00:00:00 2001 From: Justin Li Date: Fri, 6 Sep 2013 18:39:14 -0400 Subject: [PATCH 1/7] Set up initial outbound synchronization structure --- cache/cache.go | 4 +- cache/redis/redis.go | 2 +- cache/redis/redis_test.go | 2 +- config/config.go | 4 +- config/example.json | 2 +- main.go | 4 +- models/models.go | 10 +++++ storage/gazelle/flush.go | 62 +++++++++++++++++++++++++++ storage/gazelle/gazelle.go | 88 ++++++++++++++++++++++++++++++++++++++ storage/storage.go | 5 ++- 10 files changed, 173 insertions(+), 10 deletions(-) create mode 100644 storage/gazelle/flush.go create mode 100644 storage/gazelle/gazelle.go diff --git a/cache/cache.go b/cache/cache.go index 5f3ef69..569d9c7 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -15,8 +15,8 @@ import ( ) var ( - drivers = make(map[string]Driver) - ErrTxDone = errors.New("cache: Transaction has already been committed or rolled back") + drivers = make(map[string]Driver) + ErrTxDone = errors.New("cache: Transaction has already been committed or rolled back") ErrTxConflict = errors.New("cache: Commit interrupted, update transaction and repeat") ) diff --git a/cache/redis/redis.go b/cache/redis/redis.go index a1b194e..21ae0ea 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -31,7 +31,7 @@ func (d *driver) New(conf *config.DataStore) cache.Pool { return &Pool{ conf: conf, pool: redis.Pool{ - MaxIdle: conf.MaxIdleConn, + MaxIdle: conf.MaxIdleConns, IdleTimeout: conf.IdleTimeout.Duration, Dial: makeDialFunc(conf), TestOnBorrow: testOnBorrow, diff --git a/cache/redis/redis_test.go b/cache/redis/redis_test.go index 6f45ff3..f1a3cd4 100644 --- a/cache/redis/redis_test.go +++ b/cache/redis/redis_test.go @@ -47,7 +47,7 @@ func createTestTxObj(t TestReporter) *Tx { testPool := &Pool{ conf: conf, pool: redis.Pool{ - MaxIdle: conf.MaxIdleConn, + MaxIdle: conf.MaxIdleConns, IdleTimeout: conf.IdleTimeout.Duration, Dial: makeDialFunc(conf), TestOnBorrow: testOnBorrow, diff --git a/config/config.go b/config/config.go index c751909..c5907da 100644 --- a/config/config.go +++ b/config/config.go @@ -39,8 +39,8 @@ type DataStore struct { Encoding string `json:"encoding,omitempty"` Prefix string `json:"prefix,omitempty"` - MaxIdleConn int `json:"max_idle_conn,omitempty"` - IdleTimeout *Duration `json:"idle_timeout,omitempty"` + MaxIdleConns int `json:"max_idle_conns,omitempty"` + IdleTimeout *Duration `json:"idle_timeout,omitempty"` } // Config represents a configuration for a server.Server. diff --git a/config/example.json b/config/example.json index 90b5111..44a1ba9 100644 --- a/config/example.json +++ b/config/example.json @@ -12,7 +12,7 @@ "pass": "", "prefix": "test:", - "max_idle_conn": 3, + "max_idle_conns": 3, "idle_timeout": "240s" }, diff --git a/main.go b/main.go index f1ed81d..77831a8 100644 --- a/main.go +++ b/main.go @@ -12,9 +12,11 @@ import ( "runtime" "runtime/pprof" - _ "github.com/pushrax/chihaya/cache/redis" "github.com/pushrax/chihaya/config" "github.com/pushrax/chihaya/server" + + _ "github.com/pushrax/chihaya/cache/redis" + _ "github.com/pushrax/chihaya/storage/gazelle" ) var ( diff --git a/models/models.go b/models/models.go index 727c0a4..36cfd62 100644 --- a/models/models.go +++ b/models/models.go @@ -36,3 +36,13 @@ type User struct { Slots int64 `json:"slots"` SlotsUsed int64 `json:"slots_used"` } + +type AnnounceDelta struct { + Peer *Peer + Torrent *Torrent + User *User + + Uploaded uint64 + Downloaded uint64 + Timestamp float64 +} diff --git a/storage/gazelle/flush.go b/storage/gazelle/flush.go new file mode 100644 index 0000000..d9fd9f7 --- /dev/null +++ b/storage/gazelle/flush.go @@ -0,0 +1,62 @@ +// 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 gazelle + +import ( + "bytes" + "log" + "time" +) + +func (c *Conn) flushTorrents() { + var query bytes.Buffer + c.waitGroup.Add(1) + defer c.waitGroup.Done() + var count int + + for { + length := len(c.torrentChannel) + query.Reset() + + query.WriteString("INSERT INTO torrents (ID, Snatched, Seeders, Leechers, last_action) VALUES\n") + + for count = 0; count < length; count++ { + b := <-c.torrentChannel + if b == nil { + break + } + query.Write(b.Bytes()) + + if count != length-1 { + query.WriteRune(',') + } + } + + if !c.terminate { + log.Printf("[torrents] Flushing %d\n", count) + } + + if count > 0 { + query.WriteString("\nON DUPLICATE KEY UPDATE Snatched = Snatched + VALUES(Snatched), " + + "Seeders = VALUES(Seeders), Leechers = VALUES(Leechers), " + + "last_action = IF(last_action < VALUES(last_action), VALUES(last_action), last_action);") + + c.db.Exec(query.String()) + + if length < cap(c.torrentChannel)/2 { + time.Sleep(200 * time.Millisecond) + } + } else if c.terminate { + break + } else { + time.Sleep(time.Second) + } + } +} + +func (c *Conn) flushUsers() {} +func (c *Conn) flushTransferHistory() {} +func (c *Conn) flushTransferIps() {} +func (c *Conn) flushSnatches() {} diff --git a/storage/gazelle/gazelle.go b/storage/gazelle/gazelle.go new file mode 100644 index 0000000..7f50387 --- /dev/null +++ b/storage/gazelle/gazelle.go @@ -0,0 +1,88 @@ +// 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 gazelle provides a driver for a BitTorrent tracker to interface +// with the MySQL database used by Gazelle (github.com/WhatCD/Gazelle). +package gazelle + +import ( + "bytes" + "database/sql" + "fmt" + "sync" + + "github.com/pushrax/chihaya/config" + "github.com/pushrax/chihaya/models" + "github.com/pushrax/chihaya/storage" + + _ "github.com/go-sql-driver/mysql" +) + +type driver struct{} + +func (d *driver) New(conf *config.DataStore) storage.Conn { + dsn := fmt.Sprintf( + "%s:%s@%s:%s/%s?charset=utf8mb4,utf8", + conf.Username, + conf.Password, + conf.Host, + conf.Port, + conf.Schema, + ) + db, err := sql.Open("mysql", dsn) + if err != nil { + panic("gazelle: failed to open connection to MySQL") + } + db.SetMaxIdleConns(conf.MaxIdleConns) + + conn := &Conn{db: db} + + // TODO Buffer sizes + conn.torrentChannel = make(chan *bytes.Buffer, 1000) + conn.userChannel = make(chan *bytes.Buffer, 1000) + conn.transferHistoryChannel = make(chan *bytes.Buffer, 1000) + conn.transferIpsChannel = make(chan *bytes.Buffer, 1000) + conn.snatchChannel = make(chan *bytes.Buffer, 100) + + return conn +} + +type Conn struct { + db *sql.DB + waitGroup sync.WaitGroup + terminate bool + + torrentChannel chan *bytes.Buffer + userChannel chan *bytes.Buffer + transferHistoryChannel chan *bytes.Buffer + transferIpsChannel chan *bytes.Buffer + snatchChannel chan *bytes.Buffer +} + +func (c *Conn) Start() error { + go c.flushTorrents() + go c.flushUsers() + go c.flushTransferHistory() + go c.flushTransferIps() + go c.flushSnatches() + return nil +} + +func (c *Conn) Close() error { + c.terminate = true + c.waitGroup.Wait() + return c.db.Close() +} + +func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { + return nil +} + +func (c *Conn) RecordSnatch(peer *models.Peer) error { + return nil +} + +func init() { + storage.Register("gazelle", &driver{}) +} diff --git a/storage/storage.go b/storage/storage.go index 10109c0..e80a3d5 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -49,7 +49,8 @@ func Open(conf *config.DataStore) (Conn, error) { // Conn represents a connection to the data store. type Conn interface { + Start() error Close() error - UpdateTorrents(t []models.Torrent) error - UpdateUsers(u []models.User) error + RecordAnnounce(delta *models.AnnounceDelta) error + RecordSnatch(peer *models.Peer) error } From 4687ddde6f1aeb1b558581729c24b4241fd1461b Mon Sep 17 00:00:00 2001 From: Justin Li Date: Fri, 6 Sep 2013 18:51:15 -0400 Subject: [PATCH 2/7] Use strings for the database channels and add handling for torrents --- storage/gazelle/flush.go | 6 +++--- storage/gazelle/gazelle.go | 28 +++++++++++++++++----------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/storage/gazelle/flush.go b/storage/gazelle/flush.go index d9fd9f7..e0753fd 100644 --- a/storage/gazelle/flush.go +++ b/storage/gazelle/flush.go @@ -23,11 +23,11 @@ func (c *Conn) flushTorrents() { query.WriteString("INSERT INTO torrents (ID, Snatched, Seeders, Leechers, last_action) VALUES\n") for count = 0; count < length; count++ { - b := <-c.torrentChannel - if b == nil { + s := <-c.torrentChannel + if s == "" { break } - query.Write(b.Bytes()) + query.WriteString(s) if count != length-1 { query.WriteRune(',') diff --git a/storage/gazelle/gazelle.go b/storage/gazelle/gazelle.go index 7f50387..face4dc 100644 --- a/storage/gazelle/gazelle.go +++ b/storage/gazelle/gazelle.go @@ -7,7 +7,6 @@ package gazelle import ( - "bytes" "database/sql" "fmt" "sync" @@ -39,11 +38,11 @@ func (d *driver) New(conf *config.DataStore) storage.Conn { conn := &Conn{db: db} // TODO Buffer sizes - conn.torrentChannel = make(chan *bytes.Buffer, 1000) - conn.userChannel = make(chan *bytes.Buffer, 1000) - conn.transferHistoryChannel = make(chan *bytes.Buffer, 1000) - conn.transferIpsChannel = make(chan *bytes.Buffer, 1000) - conn.snatchChannel = make(chan *bytes.Buffer, 100) + conn.torrentChannel = make(chan string, 1000) + conn.userChannel = make(chan string, 1000) + conn.transferHistoryChannel = make(chan string, 1000) + conn.transferIpsChannel = make(chan string, 1000) + conn.snatchChannel = make(chan string, 100) return conn } @@ -53,11 +52,11 @@ type Conn struct { waitGroup sync.WaitGroup terminate bool - torrentChannel chan *bytes.Buffer - userChannel chan *bytes.Buffer - transferHistoryChannel chan *bytes.Buffer - transferIpsChannel chan *bytes.Buffer - snatchChannel chan *bytes.Buffer + torrentChannel chan string + userChannel chan string + transferHistoryChannel chan string + transferIpsChannel chan string + snatchChannel chan string } func (c *Conn) Start() error { @@ -76,6 +75,13 @@ func (c *Conn) Close() error { } func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { + c.torrentChannel <- fmt.Sprintf( + "('%s','%s','%s','%s','%s')", + delta.Torrent.ID, + len(delta.Torrent.Seeders), + len(delta.Torrent.Leechers), + delta.Torrent.LastAction, + ) return nil } From afa8abb9abb2f9a6b138ee257e4d8e85f11feed4 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Fri, 6 Sep 2013 19:01:02 -0400 Subject: [PATCH 3/7] Remove the unimplemented batter driver --- models/models.go | 1 + storage/batter/batter.go | 52 -------------------------------------- storage/gazelle/gazelle.go | 8 +++++- 3 files changed, 8 insertions(+), 53 deletions(-) delete mode 100644 storage/batter/batter.go diff --git a/models/models.go b/models/models.go index 36cfd62..2ea8527 100644 --- a/models/models.go +++ b/models/models.go @@ -45,4 +45,5 @@ type AnnounceDelta struct { Uploaded uint64 Downloaded uint64 Timestamp float64 + Snatched bool } diff --git a/storage/batter/batter.go b/storage/batter/batter.go deleted file mode 100644 index 4badba2..0000000 --- a/storage/batter/batter.go +++ /dev/null @@ -1,52 +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 batter provides a driver for a BitTorrent tracker to interface -// with the postgres database used by batter (github.com/wafflesfm/batter). -package batter - -import ( - "database/sql" - "fmt" - - "github.com/pushrax/chihaya/config" - "github.com/pushrax/chihaya/models" - "github.com/pushrax/chihaya/storage" - - _ "github.com/bmizerany/pq" -) - -type driver struct{} - -func (d *driver) New(conf *config.DataStore) storage.Conn { - dsn := fmt.Sprintf( - "host=%s user=%s password=%s dbname=%s", - conf.Host, - conf.Port, - conf.Username, - conf.Password, - conf.Schema, - ) - db, err := sql.Open("postgres", dsn) - if err != nil { - panic("batter: failed to open connection to postgres") - } - return &Conn{db} -} - -type Conn struct { - *sql.DB -} - -func (c *Conn) UpdateTorrents(t []models.Torrent) error { - return nil -} - -func (c *Conn) UpdateUsers(u []models.User) error { - return nil -} - -func init() { - storage.Register("batter", &driver{}) -} diff --git a/storage/gazelle/gazelle.go b/storage/gazelle/gazelle.go index face4dc..664fbeb 100644 --- a/storage/gazelle/gazelle.go +++ b/storage/gazelle/gazelle.go @@ -75,9 +75,15 @@ func (c *Conn) Close() error { } func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { + snatchCount := 0 + if delta.Snatched { + snatchCount = 1 + } + c.torrentChannel <- fmt.Sprintf( - "('%s','%s','%s','%s','%s')", + "('%d','%d','%d','%d','%d')", delta.Torrent.ID, + snatchCount, len(delta.Torrent.Seeders), len(delta.Torrent.Leechers), delta.Torrent.LastAction, From 46280fd97b7827d90fcc524053ad0cc5515f0ecd Mon Sep 17 00:00:00 2001 From: Justin Li Date: Fri, 6 Sep 2013 19:08:06 -0400 Subject: [PATCH 4/7] Embed *db.Sql into storage/gazelle --- server/stats_test.go | 2 +- storage/gazelle/flush.go | 2 +- storage/gazelle/gazelle.go | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/server/stats_test.go b/server/stats_test.go index 2bf1542..20bbedc 100644 --- a/server/stats_test.go +++ b/server/stats_test.go @@ -14,7 +14,7 @@ import ( "github.com/pushrax/chihaya/config" _ "github.com/pushrax/chihaya/cache/redis" - _ "github.com/pushrax/chihaya/storage/batter" + _ "github.com/pushrax/chihaya/storage/gazelle" ) func newTestServer() (*Server, error) { diff --git a/storage/gazelle/flush.go b/storage/gazelle/flush.go index e0753fd..d847d2d 100644 --- a/storage/gazelle/flush.go +++ b/storage/gazelle/flush.go @@ -43,7 +43,7 @@ func (c *Conn) flushTorrents() { "Seeders = VALUES(Seeders), Leechers = VALUES(Leechers), " + "last_action = IF(last_action < VALUES(last_action), VALUES(last_action), last_action);") - c.db.Exec(query.String()) + c.Exec(query.String()) if length < cap(c.torrentChannel)/2 { time.Sleep(200 * time.Millisecond) diff --git a/storage/gazelle/gazelle.go b/storage/gazelle/gazelle.go index 664fbeb..c5e21c9 100644 --- a/storage/gazelle/gazelle.go +++ b/storage/gazelle/gazelle.go @@ -35,7 +35,7 @@ func (d *driver) New(conf *config.DataStore) storage.Conn { } db.SetMaxIdleConns(conf.MaxIdleConns) - conn := &Conn{db: db} + conn := &Conn{DB: db} // TODO Buffer sizes conn.torrentChannel = make(chan string, 1000) @@ -48,7 +48,6 @@ func (d *driver) New(conf *config.DataStore) storage.Conn { } type Conn struct { - db *sql.DB waitGroup sync.WaitGroup terminate bool @@ -57,6 +56,8 @@ type Conn struct { transferHistoryChannel chan string transferIpsChannel chan string snatchChannel chan string + + *sql.DB } func (c *Conn) Start() error { @@ -71,7 +72,7 @@ func (c *Conn) Start() error { func (c *Conn) Close() error { c.terminate = true c.waitGroup.Wait() - return c.db.Close() + return c.DB.Close() } func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { From eff8e70cde95c84d8f5418d169bc5fb2f9ec866a Mon Sep 17 00:00:00 2001 From: Justin Li Date: Fri, 6 Sep 2013 19:13:44 -0400 Subject: [PATCH 5/7] Make the batter driver follow the updated storage interface --- main.go | 1 + server/stats_test.go | 2 +- storage/batter/batter.go | 57 ++++++++++++++++++++++++++++++++++++++ storage/gazelle/gazelle.go | 9 +++--- storage/storage.go | 1 - 5 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 storage/batter/batter.go diff --git a/main.go b/main.go index 77831a8..ed13f83 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( "github.com/pushrax/chihaya/server" _ "github.com/pushrax/chihaya/cache/redis" + _ "github.com/pushrax/chihaya/storage/batter" _ "github.com/pushrax/chihaya/storage/gazelle" ) diff --git a/server/stats_test.go b/server/stats_test.go index 20bbedc..2bf1542 100644 --- a/server/stats_test.go +++ b/server/stats_test.go @@ -14,7 +14,7 @@ import ( "github.com/pushrax/chihaya/config" _ "github.com/pushrax/chihaya/cache/redis" - _ "github.com/pushrax/chihaya/storage/gazelle" + _ "github.com/pushrax/chihaya/storage/batter" ) func newTestServer() (*Server, error) { diff --git a/storage/batter/batter.go b/storage/batter/batter.go new file mode 100644 index 0000000..63e9114 --- /dev/null +++ b/storage/batter/batter.go @@ -0,0 +1,57 @@ +// 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 batter provides a driver for a BitTorrent tracker to interface +// with the postgres database used by batter (github.com/wafflesfm/batter). +package batter + +import ( + "database/sql" + "fmt" + + "github.com/pushrax/chihaya/config" + "github.com/pushrax/chihaya/models" + "github.com/pushrax/chihaya/storage" + + _ "github.com/bmizerany/pq" +) + +type driver struct{} + +func (d *driver) New(conf *config.DataStore) storage.Conn { + dsn := fmt.Sprintf( + "host=%s user=%s password=%s dbname=%s", + conf.Host, + conf.Port, + conf.Username, + conf.Password, + conf.Schema, + ) + db, err := sql.Open("postgres", dsn) + if err != nil { + panic("batter: failed to open connection to postgres") + } + + if conf.MaxIdleConns != 0 { + db.SetMaxIdleConns(conf.MaxIdleConns) + } + + return &Conn{db} +} + +type Conn struct { + *sql.DB +} + +func (c *Conn) Start() error { + return nil +} + +func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { + return nil +} + +func init() { + storage.Register("batter", &driver{}) +} diff --git a/storage/gazelle/gazelle.go b/storage/gazelle/gazelle.go index c5e21c9..237c916 100644 --- a/storage/gazelle/gazelle.go +++ b/storage/gazelle/gazelle.go @@ -33,7 +33,10 @@ func (d *driver) New(conf *config.DataStore) storage.Conn { if err != nil { panic("gazelle: failed to open connection to MySQL") } - db.SetMaxIdleConns(conf.MaxIdleConns) + + if conf.MaxIdleConns != 0 { + db.SetMaxIdleConns(conf.MaxIdleConns) + } conn := &Conn{DB: db} @@ -92,10 +95,6 @@ func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { return nil } -func (c *Conn) RecordSnatch(peer *models.Peer) error { - return nil -} - func init() { storage.Register("gazelle", &driver{}) } diff --git a/storage/storage.go b/storage/storage.go index e80a3d5..b452e76 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -52,5 +52,4 @@ type Conn interface { Start() error Close() error RecordAnnounce(delta *models.AnnounceDelta) error - RecordSnatch(peer *models.Peer) error } From 7e402b162fa5886ea11a742b633dcffdefc7b0ec Mon Sep 17 00:00:00 2001 From: Justin Li Date: Fri, 6 Sep 2013 21:38:56 -0400 Subject: [PATCH 6/7] Add interface for loading data from storage --- models/models.go | 48 ++++++++++++++++-------------------- storage/batter/batter.go | 3 +-- storage/batter/load.go | 25 +++++++++++++++++++ storage/gazelle/gazelle.go | 3 +-- storage/gazelle/load.go | 25 +++++++++++++++++++ storage/storage.go | 50 +++++++++++++++++++++++++++++++++++--- 6 files changed, 119 insertions(+), 35 deletions(-) create mode 100644 storage/batter/load.go create mode 100644 storage/gazelle/load.go diff --git a/models/models.go b/models/models.go index 2ea8527..15b3a9a 100644 --- a/models/models.go +++ b/models/models.go @@ -8,42 +8,36 @@ 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"` + + 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"` } 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 uint `json:"snatches"` - UpMultiplier float64 `json:"up_multiplier"` - DownMultiplier float64 `json:"down_multiplier"` - LastAction int64 `json:"last_action"` + 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 uint `json:"snatches"` + UpMultiplier float64 `json:"up_multiplier"` + DownMultiplier float64 `json:"down_multiplier"` + LastAction int64 `json:"last_action"` } type User struct { - ID uint64 `json:"id"` - Passkey string `json:"passkey"` + ID uint64 `json:"id"` + Passkey string `json:"passkey"` + UpMultiplier float64 `json:"up_multiplier"` DownMultiplier float64 `json:"down_multiplier"` Slots int64 `json:"slots"` SlotsUsed int64 `json:"slots_used"` } - -type AnnounceDelta struct { - Peer *Peer - Torrent *Torrent - User *User - - Uploaded uint64 - Downloaded uint64 - Timestamp float64 - Snatched bool -} diff --git a/storage/batter/batter.go b/storage/batter/batter.go index 63e9114..311436e 100644 --- a/storage/batter/batter.go +++ b/storage/batter/batter.go @@ -11,7 +11,6 @@ import ( "fmt" "github.com/pushrax/chihaya/config" - "github.com/pushrax/chihaya/models" "github.com/pushrax/chihaya/storage" _ "github.com/bmizerany/pq" @@ -48,7 +47,7 @@ func (c *Conn) Start() error { return nil } -func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { +func (c *Conn) RecordAnnounce(delta *storage.AnnounceDelta) error { return nil } diff --git a/storage/batter/load.go b/storage/batter/load.go new file mode 100644 index 0000000..9e35bd2 --- /dev/null +++ b/storage/batter/load.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 batter + +import ( + "github.com/pushrax/chihaya/models" +) + +func (c *Conn) LoadTorrents(ids []uint64) ([]*models.Torrent, error) { + return nil, nil +} + +func (c *Conn) LoadAllTorrents() ([]*models.Torrent, error) { + return nil, nil +} + +func (c *Conn) LoadUsers(ids []uint64) ([]*models.User, error) { + return nil, nil +} + +func (c *Conn) LoadAllUsers(ids []uint64) ([]*models.User, error) { + return nil, nil +} diff --git a/storage/gazelle/gazelle.go b/storage/gazelle/gazelle.go index 237c916..484409b 100644 --- a/storage/gazelle/gazelle.go +++ b/storage/gazelle/gazelle.go @@ -12,7 +12,6 @@ import ( "sync" "github.com/pushrax/chihaya/config" - "github.com/pushrax/chihaya/models" "github.com/pushrax/chihaya/storage" _ "github.com/go-sql-driver/mysql" @@ -78,7 +77,7 @@ func (c *Conn) Close() error { return c.DB.Close() } -func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { +func (c *Conn) RecordAnnounce(delta *storage.AnnounceDelta) error { snatchCount := 0 if delta.Snatched { snatchCount = 1 diff --git a/storage/gazelle/load.go b/storage/gazelle/load.go new file mode 100644 index 0000000..6b5320e --- /dev/null +++ b/storage/gazelle/load.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 gazelle + +import ( + "github.com/pushrax/chihaya/models" +) + +func (c *Conn) LoadTorrents(ids []uint64) ([]*models.Torrent, error) { + return nil, nil +} + +func (c *Conn) LoadAllTorrents() ([]*models.Torrent, error) { + return nil, nil +} + +func (c *Conn) LoadUsers(ids []uint64) ([]*models.User, error) { + return nil, nil +} + +func (c *Conn) LoadAllUsers(ids []uint64) ([]*models.User, error) { + return nil, nil +} diff --git a/storage/storage.go b/storage/storage.go index b452e76..9949fe2 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -13,9 +13,7 @@ import ( "github.com/pushrax/chihaya/models" ) -var ( - drivers = make(map[string]Driver) -) +var drivers = make(map[string]Driver) type Driver interface { New(*config.DataStore) Conn @@ -49,7 +47,51 @@ func Open(conf *config.DataStore) (Conn, error) { // 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(delta *models.AnnounceDelta) 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) ([]*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) +} + +// AnnounceDelta contains a difference in statistics for a peer. +// It is used for communicating changes to be recorded by the storage driver. +type AnnounceDelta struct { + Peer *models.Peer + Torrent *models.Torrent + User *models.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 float64 + + // Snatched is true if this announce completed the download + Snatched bool } From 5685a504fc0be56539a0dc9dabd5193934fb7367 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Fri, 6 Sep 2013 21:43:58 -0400 Subject: [PATCH 7/7] Properly go fmt that time [ci skip] --- models/models.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/models/models.go b/models/models.go index 15b3a9a..abfeeb6 100644 --- a/models/models.go +++ b/models/models.go @@ -5,23 +5,23 @@ package models type Peer struct { - ID string `json:"id"` - UserID uint64 `json:"user_id"` - TorrentID uint64 `json:"torrent_id"` + 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"` + Uploaded uint64 `json:"uploaded"` + Downloaded uint64 `json:"downloaded` + Left uint64 `json:"left"` LastAnnounce int64 `json:"last_announce"` } type Torrent struct { - ID uint64 `json:"id"` - Infohash string `json:"infohash"` - Active bool `json:"active"` + ID uint64 `json:"id"` + Infohash string `json:"infohash"` + Active bool `json:"active"` Seeders map[string]Peer `json:"seeders"` Leechers map[string]Peer `json:"leechers"` @@ -29,7 +29,7 @@ type Torrent struct { Snatches uint `json:"snatches"` UpMultiplier float64 `json:"up_multiplier"` DownMultiplier float64 `json:"down_multiplier"` - LastAction int64 `json:"last_action"` + LastAction int64 `json:"last_action"` } type User struct {