From eff8e70cde95c84d8f5418d169bc5fb2f9ec866a Mon Sep 17 00:00:00 2001 From: Justin Li Date: Fri, 6 Sep 2013 19:13:44 -0400 Subject: [PATCH] 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 }