Limit udp tracker server request concurrency

This commit is contained in:
Matt Joiner 2023-01-03 00:14:21 +11:00
parent f38629d354
commit 6baf8dcb99
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
1 changed files with 11 additions and 0 deletions

View File

@ -206,13 +206,24 @@ func RunSimple(ctx context.Context, s *Server, pc net.PacketConn, family udp.Add
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var b [1500]byte
// Limit concurrent handled requests.
sem := make(chan struct{}, 1000)
for {
n, addr, err := pc.ReadFrom(b[:])
if err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
default:
log.Printf("dropping request from %v: concurrency limit reached", addr)
continue
case sem <- struct{}{}:
}
b := append([]byte(nil), b[:n]...)
go func() {
defer func() { <-sem }()
err := s.HandleRequest(ctx, family, addr, b)
if err != nil {
log.Printf("error handling %v byte request from %v: %v", n, addr, err)