logging: add error to udp logs

This also makes the format of UDP and HTTP logs a little more similar.
This commit is contained in:
Jimmy Zelinskie 2015-06-26 21:07:29 -04:00
parent 9a0d86610c
commit 0741df3575
3 changed files with 26 additions and 16 deletions

View file

@ -34,12 +34,11 @@ type Server struct {
// stats, logging, and handling errors. // stats, logging, and handling errors.
func makeHandler(handler ResponseHandler) httprouter.Handle { func makeHandler(handler ResponseHandler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var msg string
start := time.Now() start := time.Now()
httpCode, err := handler(w, r, p) httpCode, err := handler(w, r, p)
duration := time.Since(start) duration := time.Since(start)
var msg string
if err != nil { if err != nil {
msg = err.Error() msg = err.Error()
} else if httpCode != http.StatusOK { } else if httpCode != http.StatusOK {
@ -58,9 +57,9 @@ func makeHandler(handler ResponseHandler) httprouter.Handle {
} }
if len(msg) > 0 { if len(msg) > 0 {
glog.Errorf("[%d - %9s] %s (%s)", httpCode, duration, reqString, msg) glog.Errorf("[HTTP - %9s] %s (%d - %s)", duration, reqString, httpCode, msg)
} else { } else {
glog.Infof("[%d - %9s] %s", httpCode, duration, reqString) glog.Infof("[HTTP - %9s] %s (%d)", duration, reqString, httpCode)
} }
} }

View file

@ -62,9 +62,12 @@ func handleTorrentError(err error, w *Writer) {
} }
// handlePacket decodes and processes one UDP request, returning the response. // handlePacket decodes and processes one UDP request, returning the response.
func (s *Server) handlePacket(packet []byte, addr *net.UDPAddr) (response []byte, actionName string) { func (s *Server) handlePacket(packet []byte, addr *net.UDPAddr) (response []byte, actionName string, err error) {
if len(packet) < 16 { if len(packet) < 16 {
return // Malformed, no client packets are less than 16 bytes. // Malformed, no client packets are less than 16 bytes.
// We explicitly return nothing in case this is a DoS attempt.
err = errMalformedPacket
return
} }
connID := packet[0:8] connID := packet[0:8]
@ -80,15 +83,18 @@ func (s *Server) handlePacket(packet []byte, addr *net.UDPAddr) (response []byte
defer func() { response = writer.buf.Bytes() }() defer func() { response = writer.buf.Bytes() }()
if action != 0 && !s.connIDGen.Matches(connID, addr.IP) { if action != 0 && !s.connIDGen.Matches(connID, addr.IP) {
writer.WriteError(errBadConnectionID) err = errBadConnectionID
writer.WriteError(err)
return return
} }
switch action { switch action {
case connectActionID: case connectActionID:
actionName = "connect" actionName = "connect"
if !bytes.Equal(connID, initialConnectionID) { if !bytes.Equal(connID, initialConnectionID) {
return // Malformed packet. err = errMalformedPacket
return
} }
writer.writeHeader(0) writer.writeHeader(0)
@ -96,8 +102,9 @@ func (s *Server) handlePacket(packet []byte, addr *net.UDPAddr) (response []byte
case announceActionID: case announceActionID:
actionName = "announce" actionName = "announce"
ann, err := s.newAnnounce(packet, addr.IP)
var ann *models.Announce
ann, err = s.newAnnounce(packet, addr.IP)
if err == nil { if err == nil {
err = s.tracker.HandleAnnounce(ann, writer) err = s.tracker.HandleAnnounce(ann, writer)
} }
@ -106,8 +113,9 @@ func (s *Server) handlePacket(packet []byte, addr *net.UDPAddr) (response []byte
case scrapeActionID: case scrapeActionID:
actionName = "scrape" actionName = "scrape"
scrape, err := s.newScrape(packet)
var scrape *models.Scrape
scrape, err = s.newScrape(packet)
if err == nil { if err == nil {
err = s.tracker.HandleScrape(scrape, writer) err = s.tracker.HandleScrape(scrape, writer)
} }

View file

@ -68,20 +68,23 @@ func (s *Server) serve(listenAddr string) error {
return err return err
} }
start := time.Now()
go func() { go func() {
response, action := s.handlePacket(buffer[:n], addr) start := time.Now()
pool.GiveSlice(buffer) response, action, err := s.handlePacket(buffer[:n], addr)
defer pool.GiveSlice(buffer)
duration := time.Since(start)
if len(response) > 0 { if len(response) > 0 {
sock.WriteToUDP(response, addr) sock.WriteToUDP(response, addr)
} }
if glog.V(2) { if glog.V(2) {
duration := time.Since(start) if err != nil {
glog.Infof("[UDP - %9s] %s %s (%s)", duration, action, addr, err)
} else {
glog.Infof("[UDP - %9s] %s %s", duration, action, addr) glog.Infof("[UDP - %9s] %s %s", duration, action, addr)
} }
}
}() }()
} }