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
}
// HTTPConfig is the configuration for HTTP functionality.
type HTTPConfig struct {
HTTPListenAddr string `json:"http_listen_addr"`
HTTPRequestTimeout Duration `json:"http_request_timeout"`
HTTPReadTimeout Duration `json:"http_read_timeout"`
HTTPWriteTimeout Duration `json:"http_write_timeout"`
HTTPListenLimit int `json:"http_listen_limit"`
// APIConfig is the configuration for an HTTP JSON API server.
type APIConfig struct {
ListenAddr string `json:"api_listen_addr"`
RequestTimeout Duration `json:"api_request_timeout"`
ReadTimeout Duration `json:"api_read_timeout"`
WriteTimeout Duration `json:"api_write_timeout"`
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 {
UDPListenAddr string `json:"udp_listen_addr"`
UDPReadBufferSize int `json:"udp_read_buffer_size"`
ListenAddr string `json:"udp_listen_addr"`
ReadBufferSize int `json:"udp_read_buffer_size"`
}
// Config is the global configuration for an instance of Chihaya.
type Config struct {
TrackerConfig
APIConfig
HTTPConfig
UDPConfig
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{
HTTPListenAddr: ":6881",
HTTPRequestTimeout: Duration{10 * time.Second},
HTTPReadTimeout: Duration{10 * time.Second},
HTTPWriteTimeout: Duration{10 * time.Second},
ListenAddr: ":6881",
RequestTimeout: Duration{10 * time.Second},
ReadTimeout: Duration{10 * time.Second},
WriteTimeout: Duration{10 * time.Second},
},
UDPConfig: UDPConfig{
UDPListenAddr: ":6882",
ListenAddr: ":6882",
},
DriverConfig: DriverConfig{

View file

@ -15,6 +15,11 @@
"respect_af": false,
"client_whitelist_enabled": false,
"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",
"http_listen_addr": ":6881",
"http_request_timeout": "4s",

View file

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

View file

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