udp: clean up

This commit is contained in:
Leo Balduf 2016-08-17 19:04:53 -04:00
parent 8ebe57a602
commit a4639a1aac
2 changed files with 3 additions and 8 deletions

View file

@ -6,7 +6,6 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/binary" "encoding/binary"
"log"
"net" "net"
"sync" "sync"
"time" "time"
@ -94,13 +93,12 @@ func (t *Frontend) ListenAndServe() error {
} }
defer t.socket.Close() defer t.socket.Close()
pool := bytepool.New(256, 2048) pool := bytepool.New(2048, 2048)
for { for {
// Check to see if we need to shutdown. // Check to see if we need to shutdown.
select { select {
case <-t.closing: case <-t.closing:
t.wg.Wait()
return nil return nil
default: default:
} }
@ -124,7 +122,6 @@ func (t *Frontend) ListenAndServe() error {
continue continue
} }
log.Println("Got UDP Request")
t.wg.Add(1) t.wg.Add(1)
go func() { go func() {
defer t.wg.Done() defer t.wg.Done()
@ -132,11 +129,10 @@ func (t *Frontend) ListenAndServe() error {
// Handle the request. // Handle the request.
start := time.Now() start := time.Now()
response, action, err := t.handleRequest( action, err := t.handleRequest(
Request{buffer[:n], addr.IP}, Request{buffer[:n], addr.IP},
ResponseWriter{t.socket, addr}, ResponseWriter{t.socket, addr},
) )
log.Printf("Handled UDP Request: %s, %s, %s\n", response, action, err)
recordResponseDuration(action, err, time.Since(start)) recordResponseDuration(action, err, time.Since(start))
}() }()
} }
@ -162,7 +158,7 @@ func (w ResponseWriter) Write(b []byte) (int, error) {
} }
// handleRequest parses and responds to a UDP Request. // handleRequest parses and responds to a UDP Request.
func (t *Frontend) handleRequest(r Request, w ResponseWriter) (response []byte, actionName string, err error) { func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string, err error) {
if len(r.Packet) < 16 { if len(r.Packet) < 16 {
// 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. // We explicitly return nothing in case this is a DoS attempt.

View file

@ -12,7 +12,6 @@ const (
announceActionID announceActionID
scrapeActionID scrapeActionID
errorActionID errorActionID
announceDualStackActionID
) )
// Option-Types as described in BEP 41 and BEP 45. // Option-Types as described in BEP 41 and BEP 45.