Skip to content

Commit

Permalink
Fixed UDP buffer size for loopback
Browse files Browse the repository at this point in the history
  • Loading branch information
alpinskiy committed Jan 21, 2025
1 parent 632bada commit 28e2956
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *Client) ConfigureEx(args ConfigureArgs) {
c.conn = nil
}
// update packet size
if maxSize := maxPacketSize(args.Network); maxSize != c.packet.maxSize {
if maxSize := maxPacketSize(args.Network, args.StatsHouseAddr); maxSize != c.packet.maxSize {
c.packet = packet{
buf: make([]byte, batchHeaderLen, maxSize),
maxSize: maxSize,
Expand Down
15 changes: 11 additions & 4 deletions client_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package statshouse
import (
"encoding/binary"
"math"
"net"

"github.com/vkcom/statshouse-go/internal/basictl"
)
Expand Down Expand Up @@ -144,13 +145,19 @@ func fillTag(k *metricKeyTransport, tagName string, tagValue string) {
k.hasEnv = k.hasEnv || tagName == "0" || tagName == "env" || tagName == "key0" // TODO - keep only "0", rest are legacy
}

func maxPacketSize(network string) int {
func maxPacketSize(network, addr string) int {
switch network {
case "tcp":
return math.MaxUint16
default:
// "udp" or "unixgram"
// IPv6 mandated minimum MTU size of 1280 (minus 40 byte IPv6 header and 8 byte UDP header)
case "unixgram":
return 1232
default:
addr, err := net.ResolveUDPAddr("udp", addr)
switch {
case err == nil && addr.IP.IsLoopback():
return 65507 // https://stackoverflow.com/questions/42609561/udp-maximum-packet-size/42610200
default:
return 1232 // IPv6 mandated minimum MTU size of 1280 (minus 40 byte IPv6 header and 8 byte UDP header)
}
}
}

0 comments on commit 28e2956

Please sign in to comment.