frontend: make request timing optional

This commit is contained in:
Leo Balduf 2017-05-12 13:12:35 +02:00
parent 48abc6048e
commit f61e7a9281
3 changed files with 65 additions and 28 deletions

View file

@ -40,6 +40,10 @@ chihaya:
read_timeout: 5s read_timeout: 5s
write_timeout: 5s write_timeout: 5s
# Whether to time requests.
# Disabling this should increase performance/decrease load.
enable_request_timing: false
# This block defines configuration for the tracker's UDP interface. # This block defines configuration for the tracker's UDP interface.
# If you do not wish to run this, delete this section. # If you do not wish to run this, delete this section.
udp: udp:
@ -57,6 +61,10 @@ chihaya:
# The key used to encrypt connection IDs. # The key used to encrypt connection IDs.
private_key: "paste a random string here that will be used to hmac connection IDs" private_key: "paste a random string here that will be used to hmac connection IDs"
# Whether to time requests.
# Disabling this should increase performance/decrease load.
enable_request_timing: false
# This block defines configuration used for the storage of peer data. # This block defines configuration used for the storage of peer data.
storage: storage:
# The frequency which stale peers are removed. # The frequency which stale peers are removed.

View file

@ -69,6 +69,7 @@ type Config struct {
RealIPHeader string `yaml:"real_ip_header"` RealIPHeader string `yaml:"real_ip_header"`
TLSCertPath string `yaml:"tls_cert_path"` TLSCertPath string `yaml:"tls_cert_path"`
TLSKeyPath string `yaml:"tls_key_path"` TLSKeyPath string `yaml:"tls_key_path"`
EnableRequestTiming bool `yaml:"enable_request_timing"`
} }
// LogFields renders the current config as a set of Logrus fields. // LogFields renders the current config as a set of Logrus fields.
@ -81,6 +82,7 @@ func (cfg Config) LogFields() log.Fields {
"realIPHeader": cfg.RealIPHeader, "realIPHeader": cfg.RealIPHeader,
"tlsCertPath": cfg.TLSCertPath, "tlsCertPath": cfg.TLSCertPath,
"tlsKeyPath": cfg.TLSKeyPath, "tlsKeyPath": cfg.TLSKeyPath,
"enableRequestTiming": cfg.EnableRequestTiming,
} }
} }
@ -168,9 +170,18 @@ func (f *Frontend) listenAndServe() error {
// announceRoute parses and responds to an Announce. // announceRoute parses and responds to an Announce.
func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var err error var err error
start := time.Now() var start time.Time
if f.EnableRequestTiming {
start = time.Now()
}
var af *bittorrent.AddressFamily var af *bittorrent.AddressFamily
defer func() { recordResponseDuration("announce", af, err, time.Since(start)) }() defer func() {
if f.EnableRequestTiming {
recordResponseDuration("announce", af, err, time.Since(start))
} else {
recordResponseDuration("announce", af, err, time.Duration(0))
}
}()
req, err := ParseAnnounce(r, f.RealIPHeader, f.AllowIPSpoofing) req, err := ParseAnnounce(r, f.RealIPHeader, f.AllowIPSpoofing)
if err != nil { if err != nil {
@ -198,9 +209,18 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httpr
// scrapeRoute parses and responds to a Scrape. // scrapeRoute parses and responds to a Scrape.
func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var err error var err error
start := time.Now() var start time.Time
if f.EnableRequestTiming {
start = time.Now()
}
var af *bittorrent.AddressFamily var af *bittorrent.AddressFamily
defer func() { recordResponseDuration("scrape", af, err, time.Since(start)) }() defer func() {
if f.EnableRequestTiming {
recordResponseDuration("scrape", af, err, time.Since(start))
} else {
recordResponseDuration("scrape", af, err, time.Duration(0))
}
}()
req, err := ParseScrape(r) req, err := ParseScrape(r)
if err != nil { if err != nil {

View file

@ -71,6 +71,7 @@ type Config struct {
PrivateKey string `yaml:"private_key"` PrivateKey string `yaml:"private_key"`
MaxClockSkew time.Duration `yaml:"max_clock_skew"` MaxClockSkew time.Duration `yaml:"max_clock_skew"`
AllowIPSpoofing bool `yaml:"allow_ip_spoofing"` AllowIPSpoofing bool `yaml:"allow_ip_spoofing"`
EnableRequestTiming bool `yaml:"enable_request_timing"`
} }
// LogFields renders the current config as a set of Logrus fields. // LogFields renders the current config as a set of Logrus fields.
@ -80,6 +81,7 @@ func (cfg Config) LogFields() log.Fields {
"privateKey": cfg.PrivateKey, "privateKey": cfg.PrivateKey,
"maxClockSkew": cfg.MaxClockSkew, "maxClockSkew": cfg.MaxClockSkew,
"allowIPSpoofing": cfg.AllowIPSpoofing, "allowIPSpoofing": cfg.AllowIPSpoofing,
"enableRequestTiming": cfg.EnableRequestTiming,
} }
} }
@ -201,13 +203,20 @@ func (t *Frontend) listenAndServe() error {
} }
// Handle the request. // Handle the request.
start := time.Now() var start time.Time
if t.EnableRequestTiming {
start = time.Now()
}
action, af, err := t.handleRequest( action, af, err := t.handleRequest(
// Make sure the IP is copied, not referenced. // Make sure the IP is copied, not referenced.
Request{buffer[:n], append([]byte{}, addr.IP...)}, Request{buffer[:n], append([]byte{}, addr.IP...)},
ResponseWriter{t.socket, addr}, ResponseWriter{t.socket, addr},
) )
if t.EnableRequestTiming {
recordResponseDuration(action, af, err, time.Since(start)) recordResponseDuration(action, af, err, time.Since(start))
} else {
recordResponseDuration(action, af, err, time.Duration(0))
}
}() }()
} }
} }