mirror of
https://github.com/netbirdio/netbird.git
synced 2026-07-19 04:59:56 +00:00
Replace the mutex-guarded callback model of peer.Conn with a per-peer event loop that exclusively owns all mutable connection state. External callers and transport workers post typed events into a non-blocking, coalescing mailbox instead of contending on conn.mu: - offers/answers coalesce to the newest message, a new offer flushes queued candidates of the superseded session - candidates are applied in arrival order from a bounded FIFO - transport state changes are never dropped - the blocking relay dial runs on a helper goroutine with a single dial in flight; signaling I/O (offer/answer sends) runs off the loop conn.mu now only guards the open/close lifecycle. Close posts a close event and waits for the loop teardown; the loop also tears down on engine context cancellation and releases resources of unprocessed events. Delete the Handshaker listener machinery (Listen loop, unbuffered drop-on-busy channels, AsyncOfferListener with its double-processing of the first offer), the never-wired dispatcher package and the unused ICEMonitor.ReconnectCh. Fix a goroutine leak in the WG watcher test that raced with tests mutating the package-level check timing vars.
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package peer
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/pion/ice/v4"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/peer/conntype"
|
|
"github.com/netbirdio/netbird/route"
|
|
)
|
|
|
|
// event is a message processed by the Conn event loop. All mutable Conn state
|
|
// is owned by that loop; producers deliver events through the mailbox and
|
|
// never mutate Conn state directly.
|
|
type event any
|
|
|
|
// evClose asks the event loop to tear down the connection. done is closed
|
|
// once the teardown finished.
|
|
type evClose struct {
|
|
signalToRemote bool
|
|
done chan struct{}
|
|
}
|
|
|
|
type evRemoteOffer struct {
|
|
offer OfferAnswer
|
|
}
|
|
|
|
type evRemoteAnswer struct {
|
|
answer OfferAnswer
|
|
}
|
|
|
|
type evRemoteCandidate struct {
|
|
candidate ice.Candidate
|
|
haRoutes route.HAMap
|
|
}
|
|
|
|
type evICEReady struct {
|
|
priority conntype.ConnPriority
|
|
info ICEConnInfo
|
|
}
|
|
|
|
type evICEDown struct {
|
|
sessionChanged bool
|
|
}
|
|
|
|
type evRelayReady struct {
|
|
info RelayConnInfo
|
|
}
|
|
|
|
type evRelayDown struct{}
|
|
|
|
// evRelayDialDone reports that the relay dial helper goroutine finished,
|
|
// successfully or not, so the loop may dispatch a pending offer.
|
|
type evRelayDialDone struct{}
|
|
|
|
type evWGTimeout struct{}
|
|
|
|
// evWGHandshake reports the first WireGuard handshake of the current watcher run.
|
|
type evWGHandshake struct {
|
|
when time.Time
|
|
}
|
|
|
|
// evWGCheckOK reports a watcher check that observed a fresh handshake,
|
|
// including handshakes of connections that were already up.
|
|
type evWGCheckOK struct{}
|
|
|
|
// evGuardTick asks the loop to send a new offer to restore connectivity.
|
|
type evGuardTick struct{}
|