Begin refactor to better encapsulate configuration

This also updates the example_config.json which did not previously work.
Config.Config is now composed of embedded structs of other configs that
can later be used to clearly have a separation of concerns.
This commit is contained in:
Jimmy Zelinskie 2014-11-01 20:12:40 -04:00
parent d754b5b376
commit ddd10d9732
5 changed files with 69 additions and 84 deletions

View file

@ -40,6 +40,8 @@ type DriverConfig struct {
Params map[string]string `json:"params,omitempty"` Params map[string]string `json:"params,omitempty"`
} }
// SubnetConfig is the configuration used to specify if local peers should be
// given a preference when responding to an announce.
type SubnetConfig struct { type SubnetConfig struct {
PreferredSubnet bool `json:"preferred_subnet,omitempty"` PreferredSubnet bool `json:"preferred_subnet,omitempty"`
PreferredIPv4Subnet int `json:"preferred_ipv4_subnet,omitempty"` PreferredIPv4Subnet int `json:"preferred_ipv4_subnet,omitempty"`
@ -55,6 +57,7 @@ type NetConfig struct {
SubnetConfig SubnetConfig
} }
// StatsConfig is the configuration used to record runtime statistics.
type StatsConfig struct { type StatsConfig struct {
BufferSize int `json:"stats_buffer_size"` BufferSize int `json:"stats_buffer_size"`
IncludeMem bool `json:"include_mem_stats"` IncludeMem bool `json:"include_mem_stats"`
@ -63,59 +66,77 @@ type StatsConfig struct {
MemUpdateInterval Duration `json:"mem_stats_interval"` MemUpdateInterval Duration `json:"mem_stats_interval"`
} }
type ShardConfig struct { // WhitelistConfig is the configuration used enable and store a whitelist of
TorrentMapShards int `json:"torrent_map_shards"` // acceptable torrent client peer ID prefixes.
type WhitelistConfig struct {
ClientWhitelistEnabled bool `json:"client_whitelist_enabled"`
ClientWhitelist []string `json:"client_whitelist,omitempty"`
} }
// Config is a configuration for a Server. // TrackerConfig is the configuration for tracker functionality.
type Config struct { type TrackerConfig struct {
Addr string `json:"addr"` PrivateEnabled bool `json:"private_enabled"`
Tracker DriverConfig `json:"tracker"` FreeleechEnabled bool `json:"freeleech_enabled"`
Backend DriverConfig `json:"backend"` PurgeInactiveTorrents bool `json:"purge_inactive_torrents"`
Announce Duration `json:"announce"`
MinAnnounce Duration `json:"min_announce"`
NumWantFallback int `json:"default_num_want"`
TorrentMapShards int `json:"torrent_map_shards"`
PrivateEnabled bool `json:"private_enabled"` NetConfig
FreeleechEnabled bool `json:"freeleech_enabled"` WhitelistConfig
PurgeInactiveTorrents bool `json:"purge_inactive_torrents"` }
Announce Duration `json:"announce"` // HTTPConfig is the configuration for HTTP functionality.
MinAnnounce Duration `json:"min_announce"` type HTTPConfig struct {
RequestTimeout Duration `json:"request_timeout"` ListenAddr string `json:"http_listen_addr"`
RequestTimeout Duration `json:"http_request_timeout"`
HttpReadTimeout Duration `json:"http_read_timeout"` HttpReadTimeout Duration `json:"http_read_timeout"`
HttpWriteTimeout Duration `json:"http_write_timeout"` HttpWriteTimeout Duration `json:"http_write_timeout"`
HttpListenLimit int `json:"http_listen_limit"` HttpListenLimit int `json:"http_listen_limit"`
NumWantFallback int `json:"default_num_want"` }
ClientWhitelistEnabled bool `json:"client_whitelist_enabled"`
ClientWhitelist []string `json:"client_whitelist,omitempty"`
// Config is the global configuration for an instance of Chihaya.
type Config struct {
TrackerConfig
HTTPConfig
DriverConfig
StatsConfig StatsConfig
NetConfig
ShardConfig
} }
// DefaultConfig is a configuration that can be used as a fallback value. // DefaultConfig is a configuration that can be used as a fallback value.
var DefaultConfig = Config{ var DefaultConfig = Config{
Addr: ":6881", TrackerConfig: TrackerConfig{
PrivateEnabled: false,
FreeleechEnabled: false,
PurgeInactiveTorrents: true,
Announce: Duration{30 * time.Minute},
MinAnnounce: Duration{15 * time.Minute},
NumWantFallback: 50,
TorrentMapShards: 1,
Tracker: DriverConfig{ NetConfig: NetConfig{
Name: "memory", AllowIPSpoofing: true,
DualStackedPeers: true,
RespectAF: false,
},
WhitelistConfig: WhitelistConfig{
ClientWhitelistEnabled: false,
},
}, },
Backend: DriverConfig{ HTTPConfig: HTTPConfig{
ListenAddr: ":6881",
RequestTimeout: Duration{10 * time.Second},
HttpReadTimeout: Duration{10 * time.Second},
HttpWriteTimeout: Duration{10 * time.Second},
},
DriverConfig: DriverConfig{
Name: "noop", Name: "noop",
}, },
PrivateEnabled: false,
FreeleechEnabled: false,
PurgeInactiveTorrents: true,
Announce: Duration{30 * time.Minute},
MinAnnounce: Duration{15 * time.Minute},
RequestTimeout: Duration{10 * time.Second},
HttpReadTimeout: Duration{10 * time.Second},
HttpWriteTimeout: Duration{10 * time.Second},
NumWantFallback: 50,
StatsConfig: StatsConfig{ StatsConfig: StatsConfig{
BufferSize: 0, BufferSize: 0,
IncludeMem: true, IncludeMem: true,
@ -123,18 +144,6 @@ var DefaultConfig = Config{
MemUpdateInterval: Duration{5 * time.Second}, MemUpdateInterval: Duration{5 * time.Second},
}, },
NetConfig: NetConfig{
AllowIPSpoofing: true,
DualStackedPeers: true,
RespectAF: false,
},
ShardConfig: ShardConfig{
TorrentMapShards: 1,
},
ClientWhitelistEnabled: false,
} }
// 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.

View file

@ -1,48 +1,24 @@
{ {
"addr": ":6881",
"tracker": {
"driver": "memory"
},
"backend": {
"driver": "noop"
},
"private_enabled": false, "private_enabled": false,
"freeleech_enabled": false, "freeleech_enabled": false,
"purge_inactive_torrents": true, "purge_inactive_torrents": true,
"announce": "30m", "announce": "30m",
"min_announce": "15m", "min_announce": "15m",
"request_timeout": "10s",
"default_num_want": 50, "default_num_want": 50,
"torrent_map_shards": 1,
"allow_ip_spoofing": true, "allow_ip_spoofing": true,
"dual_stacked_peers": true, "dual_stacked_peers": true,
"real_ip_header": "", "real_ip_header": "",
"preferred_subnet": false, "respect_af": false,
"preferred_ipv4_subnet": 0, "client_whitelist_enabled": false,
"preferred_ipv6_subnet": 0, "http_listen_addr": ":6881",
"http_request_timeout": "10s",
"http_read_timeout": "10s",
"http_write_timeout": "10s",
"http_listen_limit": 0,
"driver": "noop",
"stats_buffer_size": 0, "stats_buffer_size": 0,
"include_mem_stats": true, "include_mem_stats": true,
"verbose_mem_stats": false, "verbose_mem_stats": false,
"mem_stats_interval": "5s", "mem_stats_interval": "5s"
"client_whitelist_enabled": false,
"client_whitelist": [
"UT340-",
"AZ2500",
"exbc0J",
"FUTB0L",
"XBT054",
"OP1011",
"ML2.7.",
"BOWA0C",
"Q1-0-0",
"Q1-10-",
"346---",
"QVOD00"
]
} }

View file

@ -116,7 +116,7 @@ func Serve(cfg *config.Config, tkr *tracker.Tracker) {
tracker: tkr, tracker: tkr,
} }
glog.V(0).Info("Starting on ", cfg.Addr) glog.V(0).Info("Starting on ", cfg.ListenAddr)
if cfg.HttpListenLimit != 0 { if cfg.HttpListenLimit != 0 {
glog.V(0).Info("Limiting connections to ", cfg.HttpListenLimit) glog.V(0).Info("Limiting connections to ", cfg.HttpListenLimit)
} }
@ -126,7 +126,7 @@ func Serve(cfg *config.Config, tkr *tracker.Tracker) {
ConnState: srv.connState, ConnState: srv.connState,
ListenLimit: cfg.HttpListenLimit, ListenLimit: cfg.HttpListenLimit,
Server: &http.Server{ Server: &http.Server{
Addr: cfg.Addr, Addr: cfg.ListenAddr,
Handler: newRouter(srv), Handler: newRouter(srv),
ReadTimeout: cfg.HttpReadTimeout.Duration, ReadTimeout: cfg.HttpReadTimeout.Duration,
WriteTimeout: cfg.HttpWriteTimeout.Duration, WriteTimeout: cfg.HttpWriteTimeout.Duration,

View file

@ -35,7 +35,7 @@ type Storage struct {
func NewStorage(cfg *config.Config) *Storage { func NewStorage(cfg *config.Config) *Storage {
s := &Storage{ s := &Storage{
users: make(map[string]*models.User), users: make(map[string]*models.User),
shards: make([]Torrents, cfg.ShardConfig.TorrentMapShards), shards: make([]Torrents, cfg.TorrentMapShards),
clients: make(map[string]bool), clients: make(map[string]bool),
} }
for i := range s.shards { for i := range s.shards {

View file

@ -27,7 +27,7 @@ type Tracker struct {
// New creates a new Tracker, and opens any necessary connections. // New creates a new Tracker, and opens any necessary connections.
// Maintenance routines are automatically spawned in the background. // Maintenance routines are automatically spawned in the background.
func New(cfg *config.Config) (*Tracker, error) { func New(cfg *config.Config) (*Tracker, error) {
bc, err := backend.Open(&cfg.Backend) bc, err := backend.Open(&cfg.DriverConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }