derp: account for increased size of peerPresent messages in mesh updates

sendMeshUpdates tries to write as much as possible without blocking,
being careful to check the bufio.Writer.Available size before writes.

Except that regressed in 6c791f7d60 which made those messages larger, which
meants we were doing network I/O with the Server mutex held.

Updates tailscale/corp#13945

Change-Id: Ic327071d2e37de262931b9b390cae32084811919
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/12597/head
Brad Fitzpatrick 5 months ago committed by Brad Fitzpatrick
parent 200d92121f
commit ded7734c36

@ -1630,6 +1630,11 @@ func (c *sclient) sendPong(data [8]byte) error {
return err return err
} }
const (
peerGoneFrameLen = keyLen + 1
peerPresentFrameLen = keyLen + 16 + 2 + 1 // 16 byte IP + 2 byte port + 1 byte flags
)
// sendPeerGone sends a peerGone frame, without flushing. // sendPeerGone sends a peerGone frame, without flushing.
func (c *sclient) sendPeerGone(peer key.NodePublic, reason PeerGoneReasonType) error { func (c *sclient) sendPeerGone(peer key.NodePublic, reason PeerGoneReasonType) error {
switch reason { switch reason {
@ -1639,7 +1644,7 @@ func (c *sclient) sendPeerGone(peer key.NodePublic, reason PeerGoneReasonType) e
c.s.peerGoneNotHereFrames.Add(1) c.s.peerGoneNotHereFrames.Add(1)
} }
c.setWriteDeadline() c.setWriteDeadline()
data := make([]byte, 0, keyLen+1) data := make([]byte, 0, peerGoneFrameLen)
data = peer.AppendTo(data) data = peer.AppendTo(data)
data = append(data, byte(reason)) data = append(data, byte(reason))
if err := writeFrameHeader(c.bw.bw(), framePeerGone, uint32(len(data))); err != nil { if err := writeFrameHeader(c.bw.bw(), framePeerGone, uint32(len(data))); err != nil {
@ -1653,11 +1658,10 @@ func (c *sclient) sendPeerGone(peer key.NodePublic, reason PeerGoneReasonType) e
// sendPeerPresent sends a peerPresent frame, without flushing. // sendPeerPresent sends a peerPresent frame, without flushing.
func (c *sclient) sendPeerPresent(peer key.NodePublic, ipPort netip.AddrPort, flags PeerPresentFlags) error { func (c *sclient) sendPeerPresent(peer key.NodePublic, ipPort netip.AddrPort, flags PeerPresentFlags) error {
c.setWriteDeadline() c.setWriteDeadline()
const frameLen = keyLen + 16 + 2 + 1 // 16 byte IP + 2 byte port + 1 byte flags if err := writeFrameHeader(c.bw.bw(), framePeerPresent, peerPresentFrameLen); err != nil {
if err := writeFrameHeader(c.bw.bw(), framePeerPresent, frameLen); err != nil {
return err return err
} }
payload := make([]byte, frameLen) payload := make([]byte, peerPresentFrameLen)
_ = peer.AppendTo(payload[:0]) _ = peer.AppendTo(payload[:0])
a16 := ipPort.Addr().As16() a16 := ipPort.Addr().As16()
copy(payload[keyLen:], a16[:]) copy(payload[keyLen:], a16[:])
@ -1688,13 +1692,17 @@ drainUpdates:
writes := 0 writes := 0
for _, pcs := range c.peerStateChange { for _, pcs := range c.peerStateChange {
if c.bw.Available() <= frameHeaderLen+keyLen { avail := c.bw.Available()
break
}
var err error var err error
if pcs.present { if pcs.present {
if avail <= frameHeaderLen+peerPresentFrameLen {
break
}
err = c.sendPeerPresent(pcs.peer, pcs.ipPort, pcs.flags) err = c.sendPeerPresent(pcs.peer, pcs.ipPort, pcs.flags)
} else { } else {
if avail <= frameHeaderLen+peerGoneFrameLen {
break
}
err = c.sendPeerGone(pcs.peer, PeerGoneReasonDisconnected) err = c.sendPeerGone(pcs.peer, PeerGoneReasonDisconnected)
} }
if err != nil { if err != nil {

Loading…
Cancel
Save