diff --git a/chihaya.go b/chihaya.go index e697626..b6b445a 100644 --- a/chihaya.go +++ b/chihaya.go @@ -14,6 +14,7 @@ import ( "github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/http" + "github.com/chihaya/chihaya/stats" "github.com/chihaya/chihaya/tracker" // See the README for how to import custom drivers. @@ -70,6 +71,8 @@ func Boot() { glog.V(1).Infof("Loaded config file: %s", configPath) } + stats.DefaultBufferSize = cfg.StatsBufferSize + tkr, err := tracker.New(cfg) if err != nil { glog.Fatal("New: ", err) diff --git a/config/config.go b/config/config.go index beb1bfa..e3c9557 100644 --- a/config/config.go +++ b/config/config.go @@ -66,6 +66,7 @@ type Config struct { MinAnnounce Duration `json:"min_announce"` RequestTimeout Duration `json:"request_timeout"` NumWantFallback int `json:"default_num_want"` + StatsBufferSize int `json:"stats_buffer_size"` NetConfig } @@ -91,6 +92,7 @@ var DefaultConfig = Config{ MinAnnounce: Duration{15 * time.Minute}, RequestTimeout: Duration{10 * time.Second}, NumWantFallback: 50, + StatsBufferSize: 0, NetConfig: NetConfig{ AllowIPSpoofing: true, @@ -120,10 +122,7 @@ func Open(path string) (*Config, error) { // Decode casts an io.Reader into a JSONDecoder and decodes it into a *Config. func Decode(r io.Reader) (*Config, error) { - conf := &Config{} - err := json.NewDecoder(r).Decode(conf) - if err != nil { - return nil, err - } - return conf, nil + conf := DefaultConfig + err := json.NewDecoder(r).Decode(&conf) + return &conf, err } diff --git a/stats/stats.go b/stats/stats.go index 2ea809a..47cb4df 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -6,10 +6,7 @@ // BitTorrent tracker. package stats -import ( - "flag" - "time" -) +import "time" const ( Announce = iota @@ -40,14 +37,10 @@ const ( // channel for broadcasting events unless specified otherwise via a command // line flag. var ( - DefaultStats *Stats - DefaultChanSizes int + DefaultStats *Stats + DefaultBufferSize int ) -func init() { - flag.IntVar(&DefaultChanSizes, "stats_chan_sizes", 0, "specifies the size of chans used to collect stats") -} - type PeerStats struct { // Stats for all peers. Current uint64 `json:"current"` // Current total peer count. @@ -246,7 +239,7 @@ func (s *Stats) handlePeerEvent(ps *PeerStats, event int) { // RecordEvent broadcasts an event to the default stats queue. func RecordEvent(event int) { if DefaultStats == nil { - DefaultStats = New(DefaultChanSizes) + DefaultStats = New(DefaultBufferSize) } DefaultStats.RecordEvent(event) @@ -255,7 +248,7 @@ func RecordEvent(event int) { // RecordPeerEvent broadcasts a peer event to the default stats queue. func RecordPeerEvent(event int, ipv6 bool) { if DefaultStats == nil { - DefaultStats = New(DefaultChanSizes) + DefaultStats = New(DefaultBufferSize) } DefaultStats.RecordPeerEvent(event, ipv6) @@ -264,7 +257,7 @@ func RecordPeerEvent(event int, ipv6 bool) { // RecordTiming broadcasts a timing event to the default stats queue. func RecordTiming(event int, duration time.Duration) { if DefaultStats == nil { - DefaultStats = New(DefaultChanSizes) + DefaultStats = New(DefaultBufferSize) } DefaultStats.RecordTiming(event, duration)