mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-25 11:11:18 -08:00
Avoid taking Server.mu for repeated sends from a client to the same small set of local peers. Each sclient now keeps a bounded, goroutine-local LRU of destination public key to clientSet. To cap memory for idle clients, cache entries track a coarsely updated last-used time. The hot path refreshes that timestamp only when it is older than 30 seconds, and incoming ping frames trim entries idle for more than 10 minutes. This keeps cleanup on the client run goroutine without adding another mutex or background goroutine. cmd/derper gets new --peer-cache-max-entries and --peer-cache-max-idle flags. Their zero values use the automatic defaults, and --peer-cache-max-entries=-1 disables the cache. The peer_lookup_cache_misses counter tracks how often lookupDest falls back to the authoritative Server.mu lookup. We do not count hits on the hot path; when the cache is enabled, hits can be derived from packets_received minus peer_lookup_cache_misses. This optimization is pulled out of the larger #13510 DERP flow-tracking work from 2024, which did a bunch more. We can rebase that bigger PR later and discuss its stats and memory impact on its own merits without losing this standalone optimization. The benchmark compares the same code with TS_DEBUG_DERP_DISABLE_PEER_CACHE set true for the before run and the default cached path for the after run: TS_DEBUG_DERP_DISABLE_PEER_CACHE=true go test ./derp/derpserver -run '^$' -bench '^BenchmarkLookupDestPeerCache$' -benchtime=2s -count=10 > before go test ./derp/derpserver -run '^$' -bench '^BenchmarkLookupDestPeerCache$' -benchtime=2s -count=10 > after go run golang.org/x/perf/cmd/benchstat@latest before after goos: linux goarch: amd64 pkg: tailscale.com/derp/derpserver cpu: Intel(R) Xeon(R) 6975P-C │ before │ after │ │ sec/op │ sec/op vs base │ LookupDestPeerCache-16 180.400n ± 0% 5.720n ± 1% -96.83% (p=0.000 n=10) │ before │ after │ │ B/op │ B/op vs base │ LookupDestPeerCache-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal │ before │ after │ │ allocs/op │ allocs/op vs base │ LookupDestPeerCache-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal Updates #3560 Change-Id: Ie31b540447211fd9415eea6cc235b83a87930093 Signed-off-by: Brad Fitzpatrick <[email protected]>
188 lines
6.4 KiB
Go
188 lines
6.4 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package derpserver
|
|
|
|
import (
|
|
"time"
|
|
|
|
"tailscale.com/envknob"
|
|
"tailscale.com/types/key"
|
|
)
|
|
|
|
const (
|
|
// DefaultPeerCacheMaxEntries is the default maximum number of local
|
|
// destination peers cached per sclient.
|
|
DefaultPeerCacheMaxEntries = 100
|
|
|
|
// DefaultPeerCacheMaxIdle is the default duration a cached peer can go
|
|
// unused before it is trimmed during the next peer cache cleanup.
|
|
DefaultPeerCacheMaxIdle = 10 * time.Minute
|
|
|
|
// defaultPeerCacheLastUsedUpdateInterval is the minimum interval between
|
|
// lastUsed timestamp writes for a hot cached peer.
|
|
defaultPeerCacheLastUsedUpdateInterval = 30 * time.Second
|
|
)
|
|
|
|
var debugDisablePeerCache = envknob.RegisterBool("TS_DEBUG_DERP_DISABLE_PEER_CACHE")
|
|
|
|
// peerCacheConfig holds the configuration for the per-client peer lookup cache.
|
|
type peerCacheConfig struct {
|
|
maxEntries int // negative is disabled, zero means [DefaultPeerCacheMaxEntries]
|
|
maxIdle time.Duration // zeros means [DefaultPeerCacheMaxIdle]
|
|
}
|
|
|
|
// SetPeerCacheConfig configures the per-client peer lookup cache.
|
|
//
|
|
// maxEntries is the maximum number of local destination peers cached per
|
|
// sclient. maxIdle is how long a cached peer can go unused before it is trimmed
|
|
// during the next peer cache cleanup. Zero values mean to use defaults.
|
|
// A negative maxEntries disables the peer cache.
|
|
func (s *Server) SetPeerCacheConfig(maxEntries int, maxIdle time.Duration) {
|
|
s.peerCacheConfig = peerCacheConfig{
|
|
maxEntries: maxEntries,
|
|
maxIdle: maxIdle,
|
|
}
|
|
}
|
|
|
|
func (c peerCacheConfig) effectiveMaxEntries() int {
|
|
if c.maxEntries == 0 {
|
|
return DefaultPeerCacheMaxEntries
|
|
}
|
|
return c.maxEntries
|
|
}
|
|
|
|
func (c peerCacheConfig) effectiveMaxIdle() time.Duration {
|
|
if c.maxIdle <= 0 {
|
|
return DefaultPeerCacheMaxIdle
|
|
}
|
|
return c.maxIdle
|
|
}
|
|
|
|
func (c peerCacheConfig) effectiveLastUsedUpdateEvery() time.Duration {
|
|
maxIdle := c.effectiveMaxIdle()
|
|
if maxIdle < defaultPeerCacheLastUsedUpdateInterval {
|
|
return maxIdle
|
|
}
|
|
return defaultPeerCacheLastUsedUpdateInterval
|
|
}
|
|
|
|
func (s *Server) peerCacheEnabled() bool {
|
|
return s.peerCacheConfig.effectiveMaxEntries() > 0
|
|
}
|
|
|
|
// cachedPeer is the value type stored in [sclient.peerCache].
|
|
//
|
|
// The cs pointer itself is immutable for the life of the cachedPeer value. The
|
|
// pointed-to clientSet is still live state: its activeClient pointer is updated
|
|
// atomically, and its dup state is guarded by Server.mu.
|
|
//
|
|
// lastUsed is intentionally coarse. On a hot cache hit, lookupDestCached reads
|
|
// it every time but only writes it when it is older than
|
|
// Server.peerCacheConfig.effectiveLastUsedUpdateEvery. The pointer lets value
|
|
// copies returned from the LRU share a single timestamp without doing another
|
|
// cache Set on the hot path.
|
|
//
|
|
// A cached clientSet can outlive its entry in Server.clients. That is okay:
|
|
// unregisterClient clears activeClient before deleting the clientSet from the
|
|
// global map. If the same public key later gets a new clientSet in
|
|
// Server.clients, the old cached clientSet will have no active client, causing
|
|
// lookupDestCached to miss and lookupDest to replace the cache
|
|
// entry with the newer clientSet.
|
|
type cachedPeer struct {
|
|
cs *clientSet
|
|
lastUsed *time.Time
|
|
}
|
|
|
|
// lookupDest returns the local client, mesh forwarder, or duplicate-client
|
|
// count for dst.
|
|
//
|
|
// It must only be called from the [sclient.run] goroutine.
|
|
func (c *sclient) lookupDest(dst key.NodePublic) (_ *sclient, fwd PacketForwarder, dstLen int) {
|
|
if c.s.peerCacheEnabled() && !debugDisablePeerCache() {
|
|
if dstClient, ok := c.lookupDestCached(dst); ok {
|
|
// No metric in the hit path for performance; it's redundant with
|
|
// packets_received minus peer_lookup_cache_misses anyway.
|
|
return dstClient, nil, 0
|
|
}
|
|
c.s.peerLookupCacheMisses.Add(1)
|
|
}
|
|
|
|
dstClient, fwd, dstLen, set := c.lookupDestUncached(dst)
|
|
if dstClient != nil && c.s.peerCacheEnabled() && !debugDisablePeerCache() {
|
|
c.cacheDest(dst, set)
|
|
}
|
|
return dstClient, fwd, dstLen
|
|
}
|
|
|
|
// lookupDestCached is the hot-path destination lookup used by
|
|
// handleFrameSendPacket. It serves repeated sends to known-local peers without
|
|
// taking Server.mu by consulting c.peerCache and revalidating the cached
|
|
// clientSet through its atomic activeClient pointer.
|
|
//
|
|
// It returns ok false on cache misses and inactive cached clientSets. It does
|
|
// not perform authoritative lookup, duplicate-client accounting, mesh forwarder
|
|
// lookup, or cache population; lookupDest handles those on the fallback path.
|
|
func (c *sclient) lookupDestCached(dst key.NodePublic) (_ *sclient, ok bool) {
|
|
if peer, ok := c.peerCache.GetOk(dst); ok {
|
|
if dst := peer.cs.activeClient.Load(); dst != nil {
|
|
now := c.s.clock.Now()
|
|
if now.Sub(*peer.lastUsed) > c.s.peerCacheConfig.effectiveLastUsedUpdateEvery() {
|
|
*peer.lastUsed = now
|
|
}
|
|
// Common case for hot local flows: we know the clientSet and no
|
|
// server mutex is needed.
|
|
return dst, true
|
|
}
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func (c *sclient) cacheDest(dst key.NodePublic, set *clientSet) {
|
|
now := c.s.clock.Now()
|
|
c.peerCache.Set(dst, cachedPeer{
|
|
cs: set,
|
|
lastUsed: &now,
|
|
})
|
|
if c.peerCache.Len() > c.s.peerCacheConfig.effectiveMaxEntries() {
|
|
c.peerCache.DeleteOldest()
|
|
}
|
|
}
|
|
|
|
// lookupDestUncached is the authoritative destination lookup. It takes
|
|
// Server.mu to read Server.clients and Server.clientsMesh, returning the
|
|
// current local client, mesh forwarder, duplicate-client count, and clientSet
|
|
// found for dst. The returned clientSet is non-nil only when the returned local
|
|
// client is non-nil, and is suitable for cacheDest. At most one of the
|
|
// returned clientSet and PacketForwarder can be non-nil: local clients win over
|
|
// mesh forwarding, and mesh forwarding is considered only when there is no
|
|
// local clientSet.
|
|
func (c *sclient) lookupDestUncached(dst key.NodePublic) (_ *sclient, fwd PacketForwarder, dstLen int, cs *clientSet) {
|
|
s := c.s
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if set, ok := s.clients[dst]; ok {
|
|
dstLen = set.Len()
|
|
if dst := set.activeClient.Load(); dst != nil {
|
|
return dst, nil, dstLen, set
|
|
}
|
|
}
|
|
if dstLen < 1 {
|
|
fwd = s.clientsMesh[dst]
|
|
}
|
|
return nil, fwd, dstLen, nil
|
|
}
|
|
|
|
func (c *sclient) cleanPeerCache() {
|
|
now := c.s.clock.Now()
|
|
var old []key.NodePublic
|
|
c.peerCache.ForEach(func(k key.NodePublic, peer cachedPeer) {
|
|
if now.Sub(*peer.lastUsed) > c.s.peerCacheConfig.effectiveMaxIdle() {
|
|
old = append(old, k)
|
|
}
|
|
})
|
|
for _, k := range old {
|
|
c.peerCache.Delete(k)
|
|
}
|
|
}
|