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:
parent
9a0d86610c
commit
0741df3575
3 changed files with 26 additions and 16 deletions
|
@ -34,12 +34,11 @@ type Server struct {
|
|||
// stats, logging, and handling errors.
|
||||
func makeHandler(handler ResponseHandler) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
var msg string
|
||||
|
||||
start := time.Now()
|
||||
httpCode, err := handler(w, r, p)
|
||||
duration := time.Since(start)
|
||||
|
||||
var msg string
|
||||
if err != nil {
|
||||
msg = err.Error()
|
||||
} else if httpCode != http.StatusOK {
|
||||
|
@ -58,9 +57,9 @@ func makeHandler(handler ResponseHandler) httprouter.Handle {
|
|||
}
|
||||
|
||||
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 {
|
||||
glog.Infof("[%d - %9s] %s", httpCode, duration, reqString)
|
||||
glog.Infof("[HTTP - %9s] %s (%d)", duration, reqString, httpCode)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -62,9 +62,12 @@ func handleTorrentError(err error, w *Writer) {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
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]
|
||||
|
@ -80,15 +83,18 @@ func (s *Server) handlePacket(packet []byte, addr *net.UDPAddr) (response []byte
|
|||
defer func() { response = writer.buf.Bytes() }()
|
||||
|
||||
if action != 0 && !s.connIDGen.Matches(connID, addr.IP) {
|
||||
writer.WriteError(errBadConnectionID)
|
||||
err = errBadConnectionID
|
||||
writer.WriteError(err)
|
||||
return
|
||||
}
|
||||
|
||||
switch action {
|
||||
case connectActionID:
|
||||
actionName = "connect"
|
||||
|
||||
if !bytes.Equal(connID, initialConnectionID) {
|
||||
return // Malformed packet.
|
||||
err = errMalformedPacket
|
||||
return
|
||||
}
|
||||
|
||||
writer.writeHeader(0)
|
||||
|
@ -96,8 +102,9 @@ func (s *Server) handlePacket(packet []byte, addr *net.UDPAddr) (response []byte
|
|||
|
||||
case announceActionID:
|
||||
actionName = "announce"
|
||||
ann, err := s.newAnnounce(packet, addr.IP)
|
||||
|
||||
var ann *models.Announce
|
||||
ann, err = s.newAnnounce(packet, addr.IP)
|
||||
if err == nil {
|
||||
err = s.tracker.HandleAnnounce(ann, writer)
|
||||
}
|
||||
|
@ -106,8 +113,9 @@ func (s *Server) handlePacket(packet []byte, addr *net.UDPAddr) (response []byte
|
|||
|
||||
case scrapeActionID:
|
||||
actionName = "scrape"
|
||||
scrape, err := s.newScrape(packet)
|
||||
|
||||
var scrape *models.Scrape
|
||||
scrape, err = s.newScrape(packet)
|
||||
if err == nil {
|
||||
err = s.tracker.HandleScrape(scrape, writer)
|
||||
}
|
||||
|
|
15
udp/udp.go
15
udp/udp.go
|
@ -68,19 +68,22 @@ func (s *Server) serve(listenAddr string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
|
||||
go func() {
|
||||
response, action := s.handlePacket(buffer[:n], addr)
|
||||
pool.GiveSlice(buffer)
|
||||
start := time.Now()
|
||||
response, action, err := s.handlePacket(buffer[:n], addr)
|
||||
defer pool.GiveSlice(buffer)
|
||||
duration := time.Since(start)
|
||||
|
||||
if len(response) > 0 {
|
||||
sock.WriteToUDP(response, addr)
|
||||
}
|
||||
|
||||
if glog.V(2) {
|
||||
duration := time.Since(start)
|
||||
glog.Infof("[UDP - %9s] %s %s", duration, action, addr)
|
||||
if err != nil {
|
||||
glog.Infof("[UDP - %9s] %s %s (%s)", duration, action, addr, err)
|
||||
} else {
|
||||
glog.Infof("[UDP - %9s] %s %s", duration, action, addr)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue