fix: Flake on `TestReplica/TwentyConcurrent` (#4842)

This could actually cause connections to intermittently fail too
when a CPU is absolutely pegged. It just so happens that only
our runners have been that slow!

Fixes #4607.
This commit is contained in:
Kyle Carberry 2022-11-01 13:28:34 -07:00 committed by GitHub
parent a390b73386
commit 288e7d1045
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 0 deletions

View File

@ -216,7 +216,18 @@ func NewConn(options *Options) (*Conn, error) {
server.sendNode()
})
wireguardEngine.SetNetInfoCallback(func(ni *tailcfg.NetInfo) {
server.logger.Info(context.Background(), "netinfo callback", slog.F("netinfo", ni))
// If the lastMutex is blocked, it's possible that
// multiple NetInfo callbacks occur at the same time.
//
// We need to ensure only the latest is sent!
asOf := time.Now()
server.lastMutex.Lock()
if asOf.Before(server.lastNetInfo) {
server.lastMutex.Unlock()
return
}
server.lastNetInfo = asOf
server.lastPreferredDERP = ni.PreferredDERP
server.lastDERPLatency = ni.DERPLatency
server.lastMutex.Unlock()
@ -269,6 +280,7 @@ type Conn struct {
// It's only possible to store these values via status functions,
// so the values must be stored for retrieval later on.
lastStatus time.Time
lastNetInfo time.Time
lastEndpoints []string
lastPreferredDERP int
lastDERPLatency map[string]float64