add api to config & propogate config changes

This commit is contained in:
Jimmy Zelinskie 2015-10-11 02:32:24 -04:00
parent 5f413e6e2d
commit 1764fa4a2d
4 changed files with 52 additions and 28 deletions

View file

@ -90,24 +90,34 @@ type TrackerConfig struct {
WhitelistConfig WhitelistConfig
} }
// HTTPConfig is the configuration for HTTP functionality. // APIConfig is the configuration for an HTTP JSON API server.
type HTTPConfig struct { type APIConfig struct {
HTTPListenAddr string `json:"http_listen_addr"` ListenAddr string `json:"api_listen_addr"`
HTTPRequestTimeout Duration `json:"http_request_timeout"` RequestTimeout Duration `json:"api_request_timeout"`
HTTPReadTimeout Duration `json:"http_read_timeout"` ReadTimeout Duration `json:"api_read_timeout"`
HTTPWriteTimeout Duration `json:"http_write_timeout"` WriteTimeout Duration `json:"api_write_timeout"`
HTTPListenLimit int `json:"http_listen_limit"` ListenLimit int `json:"api_listen_limit"`
} }
// UDPConfig is the configuration for HTTP functionality. // HTTPConfig is the configuration for the HTTP protocol.
type HTTPConfig struct {
ListenAddr string `json:"http_listen_addr"`
RequestTimeout Duration `json:"http_request_timeout"`
ReadTimeout Duration `json:"http_read_timeout"`
WriteTimeout Duration `json:"http_write_timeout"`
ListenLimit int `json:"http_listen_limit"`
}
// UDPConfig is the configuration for the UDP protocol.
type UDPConfig struct { type UDPConfig struct {
UDPListenAddr string `json:"udp_listen_addr"` ListenAddr string `json:"udp_listen_addr"`
UDPReadBufferSize int `json:"udp_read_buffer_size"` ReadBufferSize int `json:"udp_read_buffer_size"`
} }
// Config is the global configuration for an instance of Chihaya. // Config is the global configuration for an instance of Chihaya.
type Config struct { type Config struct {
TrackerConfig TrackerConfig
APIConfig
HTTPConfig HTTPConfig
UDPConfig UDPConfig
DriverConfig DriverConfig
@ -139,15 +149,22 @@ var DefaultConfig = Config{
}, },
}, },
APIConfig: APIConfig{
ListenAddr: ":6880",
RequestTimeout: Duration{10 * time.Second},
ReadTimeout: Duration{10 * time.Second},
WriteTimeout: Duration{10 * time.Second},
},
HTTPConfig: HTTPConfig{ HTTPConfig: HTTPConfig{
HTTPListenAddr: ":6881", ListenAddr: ":6881",
HTTPRequestTimeout: Duration{10 * time.Second}, RequestTimeout: Duration{10 * time.Second},
HTTPReadTimeout: Duration{10 * time.Second}, ReadTimeout: Duration{10 * time.Second},
HTTPWriteTimeout: Duration{10 * time.Second}, WriteTimeout: Duration{10 * time.Second},
}, },
UDPConfig: UDPConfig{ UDPConfig: UDPConfig{
UDPListenAddr: ":6882", ListenAddr: ":6882",
}, },
DriverConfig: DriverConfig{ DriverConfig: DriverConfig{

View file

@ -15,6 +15,11 @@
"respect_af": false, "respect_af": false,
"client_whitelist_enabled": false, "client_whitelist_enabled": false,
"client_whitelist": ["OP1011"], "client_whitelist": ["OP1011"],
"api_listen_addr": ":6880",
"api_request_timeout": "4s",
"api_read_timeout": "4s",
"api_write_timeout": "4s",
"api_listen_limit": 0,
"udp_listen_addr": ":6881", "udp_listen_addr": ":6881",
"http_listen_addr": ":6881", "http_listen_addr": ":6881",
"http_request_timeout": "4s", "http_request_timeout": "4s",

View file

@ -2,8 +2,8 @@
// Use of this source code is governed by the BSD 2-Clause license, // Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file. // which can be found in the LICENSE file.
// Package udp implements a UDP BitTorrent tracker per BEP 15. // Package udp implements a BitTorrent tracker over the UDP protocol as per
// IPv6 is currently unsupported as there is no widely-implemented standard. // BEP 15.
package udp package udp
import ( import (
@ -29,12 +29,12 @@ type Server struct {
connIDGen *ConnectionIDGenerator connIDGen *ConnectionIDGenerator
} }
func (s *Server) serve(listenAddr string) error { func (s *Server) serve() error {
if s.sock != nil { if s.sock != nil {
return errors.New("server already booted") return errors.New("server already booted")
} }
udpAddr, err := net.ResolveUDPAddr("udp", listenAddr) udpAddr, err := net.ResolveUDPAddr("udp", s.config.UDPConfig.ListenAddr)
if err != nil { if err != nil {
close(s.booting) close(s.booting)
return err return err
@ -47,8 +47,8 @@ func (s *Server) serve(listenAddr string) error {
} }
defer sock.Close() defer sock.Close()
if s.config.UDPReadBufferSize > 0 { if s.config.UDPConfig.ReadBufferSize > 0 {
sock.SetReadBuffer(s.config.UDPReadBufferSize) sock.SetReadBuffer(s.config.UDPConfig.ReadBufferSize)
} }
pool := bufferpool.New(1000, 2048) pool := bufferpool.New(1000, 2048)
@ -92,17 +92,20 @@ func (s *Server) serve(listenAddr string) error {
} }
// Serve runs a UDP server, blocking until the server has shut down. // Serve runs a UDP server, blocking until the server has shut down.
func (s *Server) Serve(addr string) { func (s *Server) Serve() {
glog.V(0).Info("Starting UDP on ", addr) glog.V(0).Info("Starting UDP on ", s.config.UDPConfig.ListenAddr)
go func() { go func() {
// Generate a new IV every hour. // Generate a new IV every hour.
for range time.Tick(time.Hour) { for range time.Tick(time.Hour) {
if s.done {
return
}
s.connIDGen.NewIV() s.connIDGen.NewIV()
} }
}() }()
if err := s.serve(addr); err != nil { if err := s.serve(); err != nil {
glog.Errorf("Failed to run UDP server: %s", err.Error()) glog.Errorf("Failed to run UDP server: %s", err.Error())
} else { } else {
glog.Info("UDP server shut down cleanly") glog.Info("UDP server shut down cleanly")

View file

@ -19,7 +19,6 @@ import (
) )
var ( var (
testPort = "34137"
connectAction = []byte{0, 0, 0, byte(connectActionID)} connectAction = []byte{0, 0, 0, byte(connectActionID)}
announceAction = []byte{0, 0, 0, byte(announceActionID)} announceAction = []byte{0, 0, 0, byte(announceActionID)}
scrapeAction = []byte{0, 0, 0, byte(scrapeActionID)} scrapeAction = []byte{0, 0, 0, byte(scrapeActionID)}
@ -36,7 +35,7 @@ func setupTracker(cfg *config.Config) (*Server, chan struct{}, error) {
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
if err := srv.serve(":" + testPort); err != nil { if err := srv.serve(); err != nil {
panic(err) panic(err)
} }
close(done) close(done)
@ -47,7 +46,7 @@ func setupTracker(cfg *config.Config) (*Server, chan struct{}, error) {
} }
func setupSocket() (*net.UDPAddr, *net.UDPConn, error) { func setupSocket() (*net.UDPAddr, *net.UDPConn, error) {
srvAddr, err := net.ResolveUDPAddr("udp", "localhost:"+testPort) srvAddr, err := net.ResolveUDPAddr("udp", config.DefaultConfig.UDPConfig.ListenAddr)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -57,7 +56,7 @@ func setupSocket() (*net.UDPAddr, *net.UDPConn, error) {
return nil, nil, err return nil, nil, err
} }
return srvAddr, sock, err return srvAddr, sock, nil
} }
func makeTransactionID() []byte { func makeTransactionID() []byte {