]> Sergey Matveev's repositories - btrtrc.git/blobdiff - tracker/udp/client.go
Fix race in concurrent connects in UDP tracker
[btrtrc.git] / tracker / udp / client.go
index 59714c09c20d979d2aa87a5089bca16e55a558eb..aea3ec91fe69f5c552f4ba5259a30dc77778918a 100644 (file)
@@ -7,10 +7,12 @@ import (
        "errors"
        "fmt"
        "io"
+       "sync"
        "time"
 )
 
 type Client struct {
+       mu           sync.Mutex
        connId       ConnectionId
        connIdIssued time.Time
        Dispatcher   *Dispatcher
@@ -66,6 +68,10 @@ func (cl *Client) Scrape(
 }
 
 func (cl *Client) connect(ctx context.Context) (err error) {
+       // We could get fancier here and use RWMutex, and even fire off the connection asynchronously
+       // and provide a grace period while it resolves.
+       cl.mu.Lock()
+       defer cl.mu.Unlock()
        if !cl.connIdIssued.IsZero() && time.Since(cl.connIdIssued) < time.Minute {
                return nil
        }