2013-09-07 00:39:14 +02:00
|
|
|
// 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"
|
2013-09-07 00:39:14 +02:00
|
|
|
|
|
|
|
_ "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 {
|
2013-09-07 00:39:14 +02:00
|
|
|
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")
|
|
|
|
}
|
2013-09-07 01:13:44 +02:00
|
|
|
|
|
|
|
if conf.MaxIdleConns != 0 {
|
|
|
|
db.SetMaxIdleConns(conf.MaxIdleConns)
|
|
|
|
}
|
2013-09-07 00:39:14 +02:00
|
|
|
|
2013-09-07 01:08:06 +02:00
|
|
|
conn := &Conn{DB: db}
|
2013-09-07 00:39:14 +02:00
|
|
|
|
|
|
|
// TODO Buffer sizes
|
2013-09-07 00:51:15 +02:00
|
|
|
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)
|
2013-09-07 00:39:14 +02:00
|
|
|
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
|
|
|
type Conn struct {
|
|
|
|
waitGroup sync.WaitGroup
|
|
|
|
terminate bool
|
|
|
|
|
2013-09-07 00:51:15 +02:00
|
|
|
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
|
2013-09-07 00:39:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-07 00:39:14 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-09-07 00:51:15 +02:00
|
|
|
c.torrentChannel <- fmt.Sprintf(
|
2013-09-07 01:01:02 +02:00
|
|
|
"('%d','%d','%d','%d','%d')",
|
2013-09-07 00:51:15 +02:00
|
|
|
delta.Torrent.ID,
|
2013-09-07 01:01:02 +02:00
|
|
|
snatchCount,
|
2013-09-07 00:51:15 +02:00
|
|
|
len(delta.Torrent.Seeders),
|
|
|
|
len(delta.Torrent.Leechers),
|
|
|
|
delta.Torrent.LastAction,
|
|
|
|
)
|
2013-09-07 00:39:14 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2013-10-21 09:29:35 +02:00
|
|
|
backend.Register("gazelle", &driver{})
|
2013-09-07 00:39:14 +02:00
|
|
|
}
|