Make the batter driver follow the updated storage interface

This commit is contained in:
Justin Li 2013-09-06 19:13:44 -04:00
parent 46280fd97b
commit eff8e70cde
5 changed files with 63 additions and 7 deletions

View file

@ -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"
)

View file

@ -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) {

57
storage/batter/batter.go Normal file
View file

@ -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{})
}

View file

@ -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{})
}

View file

@ -52,5 +52,4 @@ type Conn interface {
Start() error
Close() error
RecordAnnounce(delta *models.AnnounceDelta) error
RecordSnatch(peer *models.Peer) error
}