Skip to content

Commit

Permalink
Prevent heap allocations in FiveTuple Fingerprint
Browse files Browse the repository at this point in the history
  • Loading branch information
paulwe committed Apr 20, 2024
1 parent cf40c33 commit a9f47d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/allocation/allocation_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Manager struct {
lock sync.RWMutex
log logging.LeveledLogger

allocations map[string]*Allocation
allocations map[FiveTupleFingerprint]*Allocation
reservations []*reservation

allocatePacketConn func(network string, requestedPort int) (net.PacketConn, net.Addr, error)
Expand All @@ -51,7 +51,7 @@ func NewManager(config ManagerConfig) (*Manager, error) {

return &Manager{
log: config.LeveledLogger,
allocations: make(map[string]*Allocation, 64),
allocations: make(map[FiveTupleFingerprint]*Allocation, 64),

Check warning on line 54 in internal/allocation/allocation_manager.go

View check run for this annotation

Codecov / codecov/patch

internal/allocation/allocation_manager.go#L54

Added line #L54 was not covered by tests
allocatePacketConn: config.AllocatePacketConn,
allocateConn: config.AllocateConn,
permissionHandler: config.PermissionHandler,
Expand Down
30 changes: 28 additions & 2 deletions internal/allocation/five_tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,33 @@ func (f *FiveTuple) Equal(b *FiveTuple) bool {
return f.Fingerprint() == b.Fingerprint()
}

// FiveTupleFingerprint is a comparable representation of a FiveTuple
type FiveTupleFingerprint struct {
srcIP, dstIP [16]byte
srcPort, dstPort uint16
protocol Protocol
}

// Fingerprint is the identity of a FiveTuple
func (f *FiveTuple) Fingerprint() string {
return fmt.Sprintf("%d_%s_%s", f.Protocol, f.SrcAddr.String(), f.DstAddr.String())
func (f *FiveTuple) Fingerprint() (fp FiveTupleFingerprint) {
srcIP, srcPort := netAddrIPAndPort(f.SrcAddr)
copy(fp.srcIP[:], srcIP)
fp.srcPort = srcPort
dstIP, dstPort := netAddrIPAndPort(f.DstAddr)
copy(fp.dstIP[:], dstIP)
fp.dstPort = dstPort
fp.protocol = f.Protocol
return
}

func netAddrIPAndPort(addr net.Addr) (net.IP, uint16) {
switch a := addr.(type) {
case *net.UDPAddr:
return a.IP, uint16(a.Port)
case *net.TCPAddr:
return a.IP, uint16(a.Port)
default:
panic(fmt.Sprintf("unexpected type %t", a))

Check failure on line 62 in internal/allocation/five_tuple.go

View workflow job for this annotation

GitHub Actions / lint / Go

use of `panic` forbidden by pattern `^panic$` (forbidigo)
return nil, 0

Check failure on line 63 in internal/allocation/five_tuple.go

View workflow job for this annotation

GitHub Actions / lint / Go

unreachable: unreachable code (govet)

Check warning on line 63 in internal/allocation/five_tuple.go

View check run for this annotation

Codecov / codecov/patch

internal/allocation/five_tuple.go#L59-L63

Added lines #L59 - L63 were not covered by tests
}
}

0 comments on commit a9f47d0

Please sign in to comment.