tracker/storage/backend/gazelle/gazelle.go

100 lines
2.1 KiB
Go
Raw Normal View History

// 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 (
"database/sql"
"fmt"
"sync"
"github.com/pushrax/chihaya/config"
2013-10-21 09:29:35 +02:00
"github.com/pushrax/chihaya/storage/backend"
_ "github.com/go-sql-driver/mysql"
)
type driver struct{}
2013-10-21 09:29:35 +02:00
func (d *driver) New(conf *config.DataStore) backend.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")
}
if conf.MaxIdleConns != 0 {
db.SetMaxIdleConns(conf.MaxIdleConns)
}
2013-09-07 01:08:06 +02:00
conn := &Conn{DB: db}
// TODO Buffer sizes
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
}
type Conn struct {
waitGroup sync.WaitGroup
terminate bool
torrentChannel chan string
userChannel chan string
transferHistoryChannel chan string
transferIpsChannel chan string
snatchChannel chan string
2013-09-07 01:08:06 +02:00
*sql.DB
}
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()
2013-09-07 01:08:06 +02:00
return c.DB.Close()
}
2013-10-21 09:29:35 +02:00
func (c *Conn) RecordAnnounce(delta *backend.AnnounceDelta) error {
2013-09-07 01:01:02 +02:00
snatchCount := 0
if delta.Snatched {
snatchCount = 1
}
c.torrentChannel <- fmt.Sprintf(
2013-09-07 01:01:02 +02:00
"('%d','%d','%d','%d','%d')",
delta.Torrent.ID,
2013-09-07 01:01:02 +02:00
snatchCount,
len(delta.Torrent.Seeders),
len(delta.Torrent.Leechers),
delta.Torrent.LastAction,
)
return nil
}
func init() {
2013-10-21 09:29:35 +02:00
backend.Register("gazelle", &driver{})
}