docs, signatures; glog for main; config.New()

This commit is contained in:
Jimmy Zelinskie
2014-06-24 03:59:30 -04:00
parent 3c01900c69
commit 42693e2a3b
11 changed files with 125 additions and 115 deletions
+43 -15
View File
@@ -10,6 +10,8 @@ import (
"io" "io"
"os" "os"
"time" "time"
log "github.com/golang/glog"
) )
// Duration wraps a time.Duration and adds JSON marshalling. // Duration wraps a time.Duration and adds JSON marshalling.
@@ -30,8 +32,9 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
return err return err
} }
// DataStore represents the configuration used to connect to a data store. // DriverConfig is the configuration used to connect to a tracker.Driver or
type DataStore struct { // a backend.Driver.
type DriverConfig struct {
Driver string `json:"driver"` Driver string `json:"driver"`
Network string `json:"network` Network string `json:"network`
Host string `json:"host"` Host string `json:"host"`
@@ -46,41 +49,66 @@ type DataStore struct {
IdleTimeout *Duration `json:"idle_timeout,omitempty"` IdleTimeout *Duration `json:"idle_timeout,omitempty"`
} }
// Config represents a configuration for a server.Server. // Config is a configuration for a Server.
type Config struct { type Config struct {
Addr string `json:"addr"` Addr string `json:"addr"`
Tracker DataStore `json:"tracker"` Tracker DriverConfig `json:"tracker"`
Backend DataStore `json:"backend"` Backend DriverConfig `json:"backend"`
Private bool `json:"private"` Private bool `json:"private"`
Freeleech bool `json:"freeleech"` Freeleech bool `json:"freeleech"`
Announce Duration `json:"announce"` Announce Duration `json:"announce"`
MinAnnounce Duration `json:"min_announce"` MinAnnounce Duration `json:"min_announce"`
ReadTimeout Duration `json:"read_timeout"` ReadTimeout Duration `json:"read_timeout"`
DefaultNumWant int `json:"default_num_want"` NumWantFallback int `json:"default_num_want"`
}
// New returns a default configuration.
func New() *Config {
return &Config{
Addr: ":6881",
Tracker: DriverConfig{
Driver: "mock",
},
Backend: DriverConfig{
Driver: "mock",
},
Private: true,
Freeleech: false,
Announce: Duration{30 * time.Minute},
MinAnnounce: Duration{15 * time.Minute},
ReadTimeout: Duration{20 % time.Second},
NumWantFallback: 50,
}
} }
// Open is a shortcut to open a file, read it, and generate a Config. // Open is a shortcut to open a file, read it, and generate a Config.
// It supports relative and absolute paths. // It supports relative and absolute paths. Given "", it returns the result of
// New.
func Open(path string) (*Config, error) { func Open(path string) (*Config, error) {
if path == "" {
log.Info("chihaya: using default configuration")
return New(), nil
}
f, err := os.Open(os.ExpandEnv(path)) f, err := os.Open(os.ExpandEnv(path))
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close() defer f.Close()
conf, err := decode(f) conf, err := Decode(f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return conf, nil return conf, nil
} }
// decode transforms Reader populated with JSON into a *Config. // Decode attempts to decode a JSON encoded reader into a *Config.
func decode(raw io.Reader) (*Config, error) { func Decode(r io.Reader) (*Config, error) {
conf := &Config{} conf := &Config{}
err := json.NewDecoder(raw).Decode(conf) err := json.NewDecoder(r).Decode(conf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
-26
View File
@@ -1,26 +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 config
import (
"time"
)
// MockConfig is a pre-initialized config that can be used for testing purposes.
var MockConfig = Config{
Addr: ":34000",
Tracker: DataStore{
Driver: "mock",
},
Backend: DataStore{
Driver: "mock",
},
Private: true,
Freeleech: false,
Announce: Duration{30 * time.Minute},
MinAnnounce: Duration{15 * time.Minute},
ReadTimeout: Duration{20 % time.Second},
DefaultNumWant: 50,
}
+2 -2
View File
@@ -19,7 +19,7 @@ var drivers = make(map[string]Driver)
// Driver represents an interface to a long-running connection with a // Driver represents an interface to a long-running connection with a
// consistent data store. // consistent data store.
type Driver interface { type Driver interface {
New(*config.DataStore) Conn New(*config.DriverConfig) Conn
} }
// Register makes a database driver available by the provided name. // Register makes a database driver available by the provided name.
@@ -36,7 +36,7 @@ func Register(name string, driver Driver) {
} }
// Open creates a connection specified by a models configuration. // Open creates a connection specified by a models configuration.
func Open(conf *config.DataStore) (Conn, error) { func Open(conf *config.DriverConfig) (Conn, error) {
driver, ok := drivers[conf.Driver] driver, ok := drivers[conf.Driver]
if !ok { if !ok {
return nil, fmt.Errorf( return nil, fmt.Errorf(
+1 -1
View File
@@ -24,7 +24,7 @@ type Mock struct {
deltaHistoryM sync.RWMutex deltaHistoryM sync.RWMutex
} }
func (d *driver) New(conf *config.DataStore) backend.Conn { func (d *driver) New(conf *config.DriverConfig) backend.Conn {
return &Mock{} return &Mock{}
} }
+1 -1
View File
@@ -15,7 +15,7 @@ import (
type driver struct{} type driver struct{}
func (d *driver) New(conf *config.DataStore) tracker.Pool { func (d *driver) New(conf *config.DriverConfig) tracker.Pool {
return &Pool{ return &Pool{
users: make(map[string]*models.User), users: make(map[string]*models.User),
torrents: make(map[string]*models.Torrent), torrents: make(map[string]*models.Torrent),
+2 -2
View File
@@ -30,7 +30,7 @@ var (
// Driver represents an interface to pool of connections to models used for // Driver represents an interface to pool of connections to models used for
// the tracker. // the tracker.
type Driver interface { type Driver interface {
New(*config.DataStore) Pool New(*config.DriverConfig) Pool
} }
// Register makes a database driver available by the provided name. // Register makes a database driver available by the provided name.
@@ -47,7 +47,7 @@ func Register(name string, driver Driver) {
} }
// Open creates a pool of data store connections specified by a models configuration. // Open creates a pool of data store connections specified by a models configuration.
func Open(conf *config.DataStore) (Pool, error) { func Open(conf *config.DriverConfig) (Pool, error) {
driver, ok := drivers[conf.Driver] driver, ok := drivers[conf.Driver]
if !ok { if !ok {
return nil, fmt.Errorf( return nil, fmt.Errorf(
+25 -21
View File
@@ -6,17 +6,17 @@ package main
import ( import (
"flag" "flag"
"log"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
"github.com/chihaya/chihaya/config" log "github.com/golang/glog"
"github.com/chihaya/chihaya/server"
"github.com/chihaya/chihaya/config"
_ "github.com/chihaya/chihaya/drivers/backend/mock" _ "github.com/chihaya/chihaya/drivers/backend/mock"
_ "github.com/chihaya/chihaya/drivers/tracker/mock" _ "github.com/chihaya/chihaya/drivers/tracker/mock"
"github.com/chihaya/chihaya/server"
) )
var ( var (
@@ -25,8 +25,8 @@ var (
) )
func init() { func init() {
flag.BoolVar(&profile, "profile", false, "Generate profiling data for pprof into chihaya.cpu") flag.BoolVar(&profile, "profile", false, "Generate profiling data for pprof into ./chihaya.cpu")
flag.StringVar(&configPath, "config", "", "The location of a valid configuration file.") flag.StringVar(&configPath, "config", "", "Provide the filesystem path of a valid configuration file.")
} }
func main() { func main() {
@@ -35,54 +35,58 @@ func main() {
// Enable the profile if flagged. // Enable the profile if flagged.
if profile { if profile {
log.Println("running with profiling enabled")
f, err := os.Create("chihaya.cpu") f, err := os.Create("chihaya.cpu")
if err != nil { if err != nil {
log.Fatalf("failed to create profile file: %s\n", err) log.Fatalf("chihaya: failed to create profile file: %s\n", err)
} }
defer f.Close() defer f.Close()
pprof.StartCPUProfile(f) pprof.StartCPUProfile(f)
log.Info("chihaya: started profiling")
} }
// Load the config file. // Load the config file.
if configPath == "" {
log.Fatalf("must specify a configuration file")
}
conf, err := config.Open(configPath) conf, err := config.Open(configPath)
if err != nil { if err != nil {
log.Fatalf("failed to parse configuration file: %s\n", err) log.Fatalf("chihaya: failed to parse configuration file: %s\n", err)
} }
log.Println("succesfully loaded config") log.Infoln("chihaya: succesfully loaded config")
// Create a new server. // Create a new server.
s, err := server.New(conf) s, err := server.New(conf)
if err != nil { if err != nil {
log.Fatalf("failed to create server: %s\n", err) log.Fatalf("chihaya: failed to create server: %s\n", err)
} }
// Spawn a goroutine to handle interrupts and safely shut down. // Spawn a goroutine to handle interrupts and safely shut down.
go func() { go func() {
c := make(chan os.Signal, 1) interrupts := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt) signal.Notify(interrupts, os.Interrupt)
<-c
<-interrupts
log.Info("chihaya: caught interrupt, shutting down...")
if profile { if profile {
pprof.StopCPUProfile() pprof.StopCPUProfile()
log.Info("chihaya: stopped profiling")
} }
log.Println("caught interrupt, shutting down.")
err := s.Stop() err := s.Stop()
if err != nil { if err != nil {
panic("failed to shutdown cleanly") log.Fatalf("chihaya: failed to shutdown cleanly: %s", err)
} }
log.Println("shutdown successfully")
<-c log.Info("chihaya: shutdown cleanly")
<-interrupts
log.Flush()
os.Exit(0) os.Exit(0)
}() }()
// Start the server listening and handling requests. // Start the server listening and handling requests.
err = s.ListenAndServe() err = s.ListenAndServe()
if err != nil { if err != nil {
log.Fatalf("failed to start server: %s\n", err) log.Fatalf("chihaya: failed to start server: %s\n", err)
} }
} }
+29 -21
View File
@@ -18,12 +18,12 @@ import (
) )
var ( var (
// ErrMalformedRequest is returned when a request does no have the required // ErrMalformedRequest is returned when an http.Request does no have the
// parameters. // required parameters to create a model.
ErrMalformedRequest = errors.New("malformed request") ErrMalformedRequest = errors.New("malformed request")
) )
// Peer is the internal representation of a participant in a swarm. // Peer is a participant in a swarm.
type Peer struct { type Peer struct {
ID string `json:"id"` ID string `json:"id"`
UserID uint64 `json:"user_id"` UserID uint64 `json:"user_id"`
@@ -38,13 +38,13 @@ type Peer struct {
LastAnnounce int64 `json:"last_announce"` LastAnnounce int64 `json:"last_announce"`
} }
// Key is a helper that returns the proper format for keys used for maps // Key returns the unique key used to look-up a peer in a swarm (i.e
// of peers (i.e. torrent.Seeders & torrent.Leechers). // Torrent.Seeders & Torrent.Leechers).
func (p Peer) Key() string { func (p Peer) Key() string {
return p.ID + ":" + strconv.FormatUint(p.UserID, 36) return p.ID + ":" + strconv.FormatUint(p.UserID, 36)
} }
// Torrent is the internal representation of a swarm for a given torrent file. // Torrent is a swarm for a given torrent file.
type Torrent struct { type Torrent struct {
ID uint64 `json:"id"` ID uint64 `json:"id"`
Infohash string `json:"infohash"` Infohash string `json:"infohash"`
@@ -59,20 +59,20 @@ type Torrent struct {
LastAction int64 `json:"last_action"` LastAction int64 `json:"last_action"`
} }
// InSeederPool returns true if a peer is within a torrent's pool of seeders. // InSeederPool returns true if a peer is within a Torrent's pool of seeders.
func (t *Torrent) InSeederPool(p *Peer) bool { func (t *Torrent) InSeederPool(p *Peer) bool {
_, exists := t.Seeders[p.Key()] _, exists := t.Seeders[p.Key()]
return exists return exists
} }
// InLeecherPool returns true if a peer is within a torrent's pool of leechers. // InLeecherPool returns true if a peer is within a Torrent's pool of leechers.
func (t *Torrent) InLeecherPool(p *Peer) bool { func (t *Torrent) InLeecherPool(p *Peer) bool {
_, exists := t.Leechers[p.Key()] _, exists := t.Leechers[p.Key()]
return exists return exists
} }
// NewPeer creates a new peer using the information provided by an announce. // NewPeer returns the Peer representation of an Announce.
func NewPeer(t *Torrent, u *User, a *Announce) *Peer { func NewPeer(a *Announce, u *User, t *Torrent) *Peer {
return &Peer{ return &Peer{
ID: a.PeerID, ID: a.PeerID,
UserID: u.ID, UserID: u.ID,
@@ -86,7 +86,7 @@ func NewPeer(t *Torrent, u *User, a *Announce) *Peer {
} }
} }
// User is the internal representation of registered user for private trackers. // User is a registered user for private trackers.
type User struct { type User struct {
ID uint64 `json:"id"` ID uint64 `json:"id"`
Passkey string `json:"passkey"` Passkey string `json:"passkey"`
@@ -96,7 +96,7 @@ type User struct {
Snatches uint64 `json:"snatches"` Snatches uint64 `json:"snatches"`
} }
// Announce represents all of the data from an announce request. // Announce is an Announce by a Peer.
type Announce struct { type Announce struct {
Config *config.Config `json:"config"` Config *config.Config `json:"config"`
Request *http.Request `json:"request"` Request *http.Request `json:"request"`
@@ -127,7 +127,7 @@ func NewAnnounce(r *http.Request, conf *config.Config) (*Announce, error) {
infohash, _ := q.Params["info_hash"] infohash, _ := q.Params["info_hash"]
ip, _ := q.RequestedIP(r) ip, _ := q.RequestedIP(r)
left, leftErr := q.Uint64("left") left, leftErr := q.Uint64("left")
numWant := q.RequestedPeerCount(conf.DefaultNumWant) numWant := q.RequestedPeerCount(conf.NumWantFallback)
dir, _ := path.Split(r.URL.Path) dir, _ := path.Split(r.URL.Path)
peerID, _ := q.Params["peer_id"] peerID, _ := q.Params["peer_id"]
port, portErr := q.Uint64("port") port, portErr := q.Uint64("port")
@@ -161,7 +161,8 @@ func NewAnnounce(r *http.Request, conf *config.Config) (*Announce, error) {
}, nil }, nil
} }
// ClientID returns the part of a PeerID that identifies the client software. // ClientID returns the part of a PeerID that identifies a Peer's client
// software.
func (a Announce) ClientID() (clientID string) { func (a Announce) ClientID() (clientID string) {
length := len(a.PeerID) length := len(a.PeerID)
if length >= 6 { if length >= 6 {
@@ -177,8 +178,8 @@ func (a Announce) ClientID() (clientID string) {
return return
} }
// AnnounceDelta contains a difference in statistics for a peer. // AnnounceDelta contains the changes to a Peer's state. These changes are
// It is used for communicating changes to be recorded by the driver. // recorded by the backend driver.
type AnnounceDelta struct { type AnnounceDelta struct {
Peer *Peer Peer *Peer
Torrent *Torrent Torrent *Torrent
@@ -196,10 +197,17 @@ type AnnounceDelta struct {
Downloaded uint64 Downloaded uint64
} }
// NewAnnounceDelta does stuff // NewAnnounceDelta calculates a Peer's download and upload deltas between
func NewAnnounceDelta(p *Peer, u *User, a *Announce, t *Torrent, created, snatched bool) *AnnounceDelta { // Announces and generates an AnnounceDelta.
rawDeltaUp := p.Uploaded - a.Uploaded func NewAnnounceDelta(a *Announce, p *Peer, u *User, t *Torrent, created, snatched bool) *AnnounceDelta {
rawDeltaDown := p.Downloaded - a.Downloaded var (
rawDeltaUp = p.Uploaded - a.Uploaded
rawDeltaDown uint64
)
if !a.Config.Freeleech {
rawDeltaDown = p.Downloaded - a.Downloaded
}
// Restarting a torrent may cause a delta to be negative. // Restarting a torrent may cause a delta to be negative.
if rawDeltaUp < 0 { if rawDeltaUp < 0 {
@@ -222,7 +230,7 @@ func NewAnnounceDelta(p *Peer, u *User, a *Announce, t *Torrent, created, snatch
} }
} }
// Scrape represents all of the data from an scrape request. // Scrape is a Scrape by a Peer.
type Scrape struct { type Scrape struct {
Config *config.Config `json:"config"` Config *config.Config `json:"config"`
Request *http.Request `json:"request"` Request *http.Request `json:"request"`
+17 -17
View File
@@ -51,15 +51,15 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
return return
} }
peer := models.NewPeer(torrent, user, announce) peer := models.NewPeer(announce, user, torrent)
created, err := updateTorrent(peer, torrent, conn, announce) created, err := updateTorrent(conn, announce, peer, torrent)
if err != nil { if err != nil {
fail(err, w, r) fail(err, w, r)
return return
} }
snatched, err := handleEvent(announce, user, torrent, peer, conn) snatched, err := handleEvent(conn, announce, peer, user, torrent)
if err != nil { if err != nil {
fail(err, w, r) fail(err, w, r)
return return
@@ -67,15 +67,15 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
writeAnnounceResponse(w, announce, user, torrent) writeAnnounceResponse(w, announce, user, torrent)
delta := models.NewAnnounceDelta(peer, user, announce, torrent, created, snatched) delta := models.NewAnnounceDelta(announce, peer, user, torrent, created, snatched)
s.backendConn.RecordAnnounce(delta) s.backendConn.RecordAnnounce(delta)
log.V(3).Infof("chihaya: handled announce from %s", announce.IP) log.Infof("chihaya: handled announce from %s", announce.IP)
} }
func updateTorrent(p *models.Peer, t *models.Torrent, conn tracker.Conn, a *models.Announce) (created bool, err error) { func updateTorrent(c tracker.Conn, a *models.Announce, p *models.Peer, t *models.Torrent) (created bool, err error) {
if !t.Active && a.Left == 0 { if !t.Active && a.Left == 0 {
err = conn.MarkActive(t) err = c.MarkActive(t)
if err != nil { if err != nil {
return return
} }
@@ -83,25 +83,25 @@ func updateTorrent(p *models.Peer, t *models.Torrent, conn tracker.Conn, a *mode
switch { switch {
case t.InSeederPool(p): case t.InSeederPool(p):
err = conn.SetSeeder(t, p) err = c.SetSeeder(t, p)
if err != nil { if err != nil {
return return
} }
case t.InLeecherPool(p): case t.InLeecherPool(p):
err = conn.SetLeecher(t, p) err = c.SetLeecher(t, p)
if err != nil { if err != nil {
return return
} }
default: default:
if a.Left == 0 { if a.Left == 0 {
err = conn.AddSeeder(t, p) err = c.AddSeeder(t, p)
if err != nil { if err != nil {
return return
} }
} else { } else {
err = conn.AddLeecher(t, p) err = c.AddLeecher(t, p)
if err != nil { if err != nil {
return return
} }
@@ -112,31 +112,31 @@ func updateTorrent(p *models.Peer, t *models.Torrent, conn tracker.Conn, a *mode
return return
} }
func handleEvent(a *models.Announce, u *models.User, t *models.Torrent, p *models.Peer, conn tracker.Conn) (snatched bool, err error) { func handleEvent(c tracker.Conn, a *models.Announce, p *models.Peer, u *models.User, t *models.Torrent) (snatched bool, err error) {
switch { switch {
case a.Event == "stopped" || a.Event == "paused": case a.Event == "stopped" || a.Event == "paused":
if t.InSeederPool(p) { if t.InSeederPool(p) {
err = conn.RemoveSeeder(t, p) err = c.RemoveSeeder(t, p)
if err != nil { if err != nil {
return return
} }
} }
if t.InLeecherPool(p) { if t.InLeecherPool(p) {
err = conn.RemoveLeecher(t, p) err = c.RemoveLeecher(t, p)
if err != nil { if err != nil {
return return
} }
} }
case a.Event == "completed": case a.Event == "completed":
err = conn.IncrementSnatches(t) err = c.IncrementSnatches(t)
if err != nil { if err != nil {
return return
} }
snatched = true snatched = true
if t.InLeecherPool(p) { if t.InLeecherPool(p) {
err = tracker.LeecherFinished(conn, t, p) err = tracker.LeecherFinished(c, t, p)
if err != nil { if err != nil {
return return
} }
@@ -144,7 +144,7 @@ func handleEvent(a *models.Announce, u *models.User, t *models.Torrent, p *model
case t.InLeecherPool(p) && a.Left == 0: case t.InLeecherPool(p) && a.Left == 0:
// A leecher completed but the event was never received // A leecher completed but the event was never received
err = tracker.LeecherFinished(conn, t, p) err = tracker.LeecherFinished(c, t, p)
if err != nil { if err != nil {
return return
} }
+4 -4
View File
@@ -9,16 +9,16 @@ import (
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/drivers/backend" "github.com/chihaya/chihaya/drivers/backend"
"github.com/chihaya/chihaya/drivers/tracker"
"github.com/chihaya/chihaya/models"
_ "github.com/chihaya/chihaya/drivers/backend/mock" _ "github.com/chihaya/chihaya/drivers/backend/mock"
"github.com/chihaya/chihaya/drivers/tracker"
_ "github.com/chihaya/chihaya/drivers/tracker/mock" _ "github.com/chihaya/chihaya/drivers/tracker/mock"
"github.com/chihaya/chihaya/models"
) )
func TestAnnounce(t *testing.T) { func TestAnnounce(t *testing.T) {
s, err := newTestServer() s, err := New(config.New())
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
+1 -5
View File
@@ -14,12 +14,8 @@ import (
_ "github.com/chihaya/chihaya/drivers/tracker/mock" _ "github.com/chihaya/chihaya/drivers/tracker/mock"
) )
func newTestServer() (*Server, error) {
return New(&config.MockConfig)
}
func TestStats(t *testing.T) { func TestStats(t *testing.T) {
s, err := newTestServer() s, err := New(config.New())
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }