magicsock: rate limit send error log messages

The x/time/rate dependency adds 24kb to tailscaled binary size.

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
pull/122/head
David Crawshaw 5 years ago committed by David Crawshaw
parent 9cdaeac00a
commit cc4afa775f

@ -24,6 +24,7 @@ import (
"github.com/tailscale/wireguard-go/conn" "github.com/tailscale/wireguard-go/conn"
"github.com/tailscale/wireguard-go/device" "github.com/tailscale/wireguard-go/device"
"github.com/tailscale/wireguard-go/wgcfg" "github.com/tailscale/wireguard-go/wgcfg"
"golang.org/x/time/rate"
"tailscale.com/derp" "tailscale.com/derp"
"tailscale.com/derp/derphttp" "tailscale.com/derp/derphttp"
"tailscale.com/stun" "tailscale.com/stun"
@ -41,6 +42,7 @@ type Conn struct {
epFunc func(endpoints []string) epFunc func(endpoints []string)
logf func(format string, args ...interface{}) logf func(format string, args ...interface{})
donec chan struct{} // closed on Conn.Close donec chan struct{} // closed on Conn.Close
sendLogLimit *rate.Limiter
epUpdateCtx context.Context // endpoint updater context epUpdateCtx context.Context // endpoint updater context
epUpdateCancel func() // the func to cancel epUpdateCtx epUpdateCancel func() // the func to cancel epUpdateCtx
@ -138,6 +140,7 @@ func Listen(opts Options) (*Conn, error) {
pconn: new(RebindingUDPConn), pconn: new(RebindingUDPConn),
pconnPort: opts.Port, pconnPort: opts.Port,
donec: make(chan struct{}), donec: make(chan struct{}),
sendLogLimit: rate.NewLimiter(rate.Every(1*time.Minute), 1),
stunServers: append([]string{}, opts.STUN...), stunServers: append([]string{}, opts.STUN...),
startEpUpdate: make(chan struct{}, 1), startEpUpdate: make(chan struct{}, 1),
epUpdateCtx: epUpdateCtx, epUpdateCtx: epUpdateCtx,
@ -473,7 +476,7 @@ func (c *Conn) Send(b []byte, ep conn.Endpoint) error {
} else if ret == nil { } else if ret == nil {
ret = err ret = err
} }
if err != nil && addr != roamAddr { if err != nil && addr != roamAddr && c.sendLogLimit.Allow() {
log.Printf("magicsock: Conn.Send(%v): %v", addr, err) log.Printf("magicsock: Conn.Send(%v): %v", addr, err)
} }
} }

Loading…
Cancel
Save