Merge pull request #136 from mrd0ll4r/middleware-config
middleware: made middleware configurable
This commit is contained in:
commit
1874584eae
4 changed files with 86 additions and 37 deletions
19
config.go
19
config.go
|
@ -18,8 +18,8 @@ var DefaultConfig = Config{
|
||||||
Tracker: TrackerConfig{
|
Tracker: TrackerConfig{
|
||||||
AnnounceInterval: 30 * time.Minute,
|
AnnounceInterval: 30 * time.Minute,
|
||||||
MinAnnounceInterval: 20 * time.Minute,
|
MinAnnounceInterval: 20 * time.Minute,
|
||||||
AnnounceMiddleware: []string{},
|
AnnounceMiddleware: []MiddlewareConfig{},
|
||||||
ScrapeMiddleware: []string{},
|
ScrapeMiddleware: []MiddlewareConfig{},
|
||||||
},
|
},
|
||||||
Servers: []ServerConfig{},
|
Servers: []ServerConfig{},
|
||||||
}
|
}
|
||||||
|
@ -33,10 +33,17 @@ type Config struct {
|
||||||
// TrackerConfig represents the configuration of protocol-agnostic BitTorrent
|
// TrackerConfig represents the configuration of protocol-agnostic BitTorrent
|
||||||
// Tracker used by Servers started by chihaya.
|
// Tracker used by Servers started by chihaya.
|
||||||
type TrackerConfig struct {
|
type TrackerConfig struct {
|
||||||
AnnounceInterval time.Duration `yaml:"announce"`
|
AnnounceInterval time.Duration `yaml:"announce"`
|
||||||
MinAnnounceInterval time.Duration `yaml:"min_announce"`
|
MinAnnounceInterval time.Duration `yaml:"min_announce"`
|
||||||
AnnounceMiddleware []string `yaml:"announce_middleware"`
|
AnnounceMiddleware []MiddlewareConfig `yaml:"announce_middleware"`
|
||||||
ScrapeMiddleware []string `yaml:"scrape_middleware"`
|
ScrapeMiddleware []MiddlewareConfig `yaml:"scrape_middleware"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// MiddlewareConfig represents the configuration of a middleware used by
|
||||||
|
// the tracker.
|
||||||
|
type MiddlewareConfig struct {
|
||||||
|
Name string `yaml:"name"`
|
||||||
|
Config interface{} `yaml:"config"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServerConfig represents the configuration of the Servers started by chihaya.
|
// ServerConfig represents the configuration of the Servers started by chihaya.
|
||||||
|
|
|
@ -8,13 +8,15 @@ chihaya:
|
||||||
min_announce: 5m
|
min_announce: 5m
|
||||||
announce_middleware:
|
announce_middleware:
|
||||||
# These are currently fake values
|
# These are currently fake values
|
||||||
- prometheus
|
- name: prometheus
|
||||||
- store_client_validation
|
- name: store_client_validation
|
||||||
- store_create_on_announce
|
config:
|
||||||
|
be_clever: true
|
||||||
|
- name: store_create_on_announce
|
||||||
scrape_middleware:
|
scrape_middleware:
|
||||||
# These are currently fake values
|
# These are currently fake values
|
||||||
- prometheus
|
- name: prometheus
|
||||||
- store_client_validation
|
- name: store_client_validation
|
||||||
|
|
||||||
servers:
|
servers:
|
||||||
- name: store
|
- name: store
|
||||||
|
|
|
@ -14,6 +14,10 @@ type AnnounceHandler func(*chihaya.TrackerConfig, *chihaya.AnnounceRequest, *chi
|
||||||
// of AnnounceHandlers.
|
// of AnnounceHandlers.
|
||||||
type AnnounceMiddleware func(AnnounceHandler) AnnounceHandler
|
type AnnounceMiddleware func(AnnounceHandler) AnnounceHandler
|
||||||
|
|
||||||
|
// AnnounceMiddlewareConstructor is a function that creates a new
|
||||||
|
// AnnounceMiddleware from a MiddlewareConfig.
|
||||||
|
type AnnounceMiddlewareConstructor func(chihaya.MiddlewareConfig) AnnounceMiddleware
|
||||||
|
|
||||||
type announceChain struct{ mw []AnnounceMiddleware }
|
type announceChain struct{ mw []AnnounceMiddleware }
|
||||||
|
|
||||||
func (c *announceChain) Append(mw ...AnnounceMiddleware) {
|
func (c *announceChain) Append(mw ...AnnounceMiddleware) {
|
||||||
|
@ -31,23 +35,39 @@ func (c *announceChain) Handler() AnnounceHandler {
|
||||||
return final
|
return final
|
||||||
}
|
}
|
||||||
|
|
||||||
var announceMiddleware = make(map[string]AnnounceMiddleware)
|
var announceMiddlewareConstructors = make(map[string]AnnounceMiddlewareConstructor)
|
||||||
|
|
||||||
|
// RegisterAnnounceMiddlewareConstructor makes a configurable middleware
|
||||||
|
// globally available under the provided name.
|
||||||
|
//
|
||||||
|
// If this function is called twice with the same name or if the constructor is
|
||||||
|
// nil, it panics.
|
||||||
|
func RegisterAnnounceMiddlewareConstructor(name string, mw AnnounceMiddlewareConstructor) {
|
||||||
|
if mw == nil {
|
||||||
|
panic("tracker: could not register nil AnnounceMiddlewareConstructor")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, dup := announceMiddlewareConstructors[name]; dup {
|
||||||
|
panic("tracker: could not register duplicate AnnounceMiddleware: " + name)
|
||||||
|
}
|
||||||
|
|
||||||
|
announceMiddlewareConstructors[name] = mw
|
||||||
|
}
|
||||||
|
|
||||||
// RegisterAnnounceMiddleware makes a middleware globally available under the
|
// RegisterAnnounceMiddleware makes a middleware globally available under the
|
||||||
// provided named.
|
// provided name.
|
||||||
//
|
//
|
||||||
// If this function is called twice with the same name or if the handler is nil,
|
// This function is intended to register middleware that has no configuration.
|
||||||
// it panics.
|
// If this function is called twice with the same name or if the middleware is
|
||||||
|
// nil, it panics.
|
||||||
func RegisterAnnounceMiddleware(name string, mw AnnounceMiddleware) {
|
func RegisterAnnounceMiddleware(name string, mw AnnounceMiddleware) {
|
||||||
if mw == nil {
|
if mw == nil {
|
||||||
panic("tracker: could not register nil AnnounceMiddleware")
|
panic("tracker: could not register nil AnnounceMiddleware")
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, dup := announceMiddleware[name]; dup {
|
RegisterAnnounceMiddlewareConstructor(name, func(_ chihaya.MiddlewareConfig) AnnounceMiddleware {
|
||||||
panic("tracker: could not register duplicate AnnounceMiddleware: " + name)
|
return mw
|
||||||
}
|
})
|
||||||
|
|
||||||
announceMiddleware[name] = mw
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScrapeHandler is a function that operates on a ScrapeResponse before it has
|
// ScrapeHandler is a function that operates on a ScrapeResponse before it has
|
||||||
|
@ -58,6 +78,10 @@ type ScrapeHandler func(*chihaya.TrackerConfig, *chihaya.ScrapeRequest, *chihaya
|
||||||
// ScrapeHandlers.
|
// ScrapeHandlers.
|
||||||
type ScrapeMiddleware func(ScrapeHandler) ScrapeHandler
|
type ScrapeMiddleware func(ScrapeHandler) ScrapeHandler
|
||||||
|
|
||||||
|
// ScrapeMiddlewareConstructor is a function that creates a new
|
||||||
|
// ScrapeMiddleware from a MiddlewareConfig.
|
||||||
|
type ScrapeMiddlewareConstructor func(chihaya.MiddlewareConfig) ScrapeMiddleware
|
||||||
|
|
||||||
type scrapeChain struct{ mw []ScrapeMiddleware }
|
type scrapeChain struct{ mw []ScrapeMiddleware }
|
||||||
|
|
||||||
func (c *scrapeChain) Append(mw ...ScrapeMiddleware) {
|
func (c *scrapeChain) Append(mw ...ScrapeMiddleware) {
|
||||||
|
@ -74,21 +98,37 @@ func (c *scrapeChain) Handler() ScrapeHandler {
|
||||||
return final
|
return final
|
||||||
}
|
}
|
||||||
|
|
||||||
var scrapeMiddleware = make(map[string]ScrapeMiddleware)
|
var scrapeMiddlewareConstructors = make(map[string]ScrapeMiddlewareConstructor)
|
||||||
|
|
||||||
|
// RegisterScrapeMiddlewareConstructor makes a configurable middleware globally
|
||||||
|
// available under the provided name.
|
||||||
|
//
|
||||||
|
// If this function is called twice with the same name or if the constructor is
|
||||||
|
// nil, it panics.
|
||||||
|
func RegisterScrapeMiddlewareConstructor(name string, mw ScrapeMiddlewareConstructor) {
|
||||||
|
if mw == nil {
|
||||||
|
panic("tracker: could not register nil ScrapeMiddlewareConstructor")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, dup := scrapeMiddlewareConstructors[name]; dup {
|
||||||
|
panic("tracker: could not register duplicate ScrapeMiddleware: " + name)
|
||||||
|
}
|
||||||
|
|
||||||
|
scrapeMiddlewareConstructors[name] = mw
|
||||||
|
}
|
||||||
|
|
||||||
// RegisterScrapeMiddleware makes a middleware globally available under the
|
// RegisterScrapeMiddleware makes a middleware globally available under the
|
||||||
// provided named.
|
// provided name.
|
||||||
//
|
//
|
||||||
// If this function is called twice with the same name or if the handler is nil,
|
// This function is intended to register middleware that has no configuration.
|
||||||
// it panics.
|
// If this function is called twice with the same name or if the middleware is
|
||||||
|
// nil, it panics.
|
||||||
func RegisterScrapeMiddleware(name string, mw ScrapeMiddleware) {
|
func RegisterScrapeMiddleware(name string, mw ScrapeMiddleware) {
|
||||||
if mw == nil {
|
if mw == nil {
|
||||||
panic("tracker: could not register nil ScrapeMiddleware")
|
panic("tracker: could not register nil ScrapeMiddleware")
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, dup := scrapeMiddleware[name]; dup {
|
RegisterScrapeMiddlewareConstructor(name, func(_ chihaya.MiddlewareConfig) ScrapeMiddleware {
|
||||||
panic("tracker: could not register duplicate ScrapeMiddleware: " + name)
|
return mw
|
||||||
}
|
})
|
||||||
|
|
||||||
scrapeMiddleware[name] = mw
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,21 +31,21 @@ type Tracker struct {
|
||||||
// in the provided configuration.
|
// in the provided configuration.
|
||||||
func NewTracker(cfg *chihaya.TrackerConfig) (*Tracker, error) {
|
func NewTracker(cfg *chihaya.TrackerConfig) (*Tracker, error) {
|
||||||
var achain announceChain
|
var achain announceChain
|
||||||
for _, mwName := range cfg.AnnounceMiddleware {
|
for _, mwConfig := range cfg.AnnounceMiddleware {
|
||||||
mw, ok := announceMiddleware[mwName]
|
mw, ok := announceMiddlewareConstructors[mwConfig.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("failed to find announce middleware: " + mwName)
|
return nil, errors.New("failed to find announce middleware: " + mwConfig.Name)
|
||||||
}
|
}
|
||||||
achain.Append(mw)
|
achain.Append(mw(mwConfig))
|
||||||
}
|
}
|
||||||
|
|
||||||
var schain scrapeChain
|
var schain scrapeChain
|
||||||
for _, mwName := range cfg.ScrapeMiddleware {
|
for _, mwConfig := range cfg.ScrapeMiddleware {
|
||||||
mw, ok := scrapeMiddleware[mwName]
|
mw, ok := scrapeMiddlewareConstructors[mwConfig.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("failed to find scrape middleware: " + mwName)
|
return nil, errors.New("failed to find scrape middleware: " + mwConfig.Name)
|
||||||
}
|
}
|
||||||
schain.Append(mw)
|
schain.Append(mw(mwConfig))
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Tracker{
|
return &Tracker{
|
||||||
|
|
Loading…
Add table
Reference in a new issue