Skip to content

Commit

Permalink
clean: remove unused sync.WaitGroup (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens authored Nov 16, 2024
1 parent 09785e9 commit e1fd23b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
11 changes: 3 additions & 8 deletions service/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ func (h *packetHandler) SetTargetIPValidator(targetIPValidator onet.TargetIPVali
// Listen on addr for encrypted packets and basically do UDP NAT.
// We take the ciphers as a pointer because it gets replaced on config updates.
func (h *packetHandler) Handle(clientConn net.PacketConn) {
var running sync.WaitGroup

nm := newNATmap(h.natTimeout, h.m, &running, h.logger)
nm := newNATmap(h.natTimeout, h.m, h.logger)
defer nm.Close()
cipherBuf := make([]byte, serverUDPBufferSize)
textBuf := make([]byte, serverUDPBufferSize)
Expand Down Expand Up @@ -320,11 +318,10 @@ type natmap struct {
logger *slog.Logger
timeout time.Duration
metrics UDPMetrics
running *sync.WaitGroup
}

func newNATmap(timeout time.Duration, sm UDPMetrics, running *sync.WaitGroup, l *slog.Logger) *natmap {
m := &natmap{logger: l, metrics: sm, running: running}
func newNATmap(timeout time.Duration, sm UDPMetrics, l *slog.Logger) *natmap {
m := &natmap{logger: l, metrics: sm}
m.keyConn = make(map[string]*natconn)
m.timeout = timeout
return m
Expand Down Expand Up @@ -368,14 +365,12 @@ func (m *natmap) Add(clientAddr net.Addr, clientConn net.PacketConn, cryptoKey *
connMetrics := m.metrics.AddUDPNatEntry(clientAddr, keyID)
entry := m.set(clientAddr.String(), targetConn, cryptoKey, keyID, connMetrics)

m.running.Add(1)
go func() {
timedCopy(clientAddr, clientConn, entry, keyID, m.logger)
connMetrics.RemoveNatEntry()
if pc := m.del(clientAddr.String()); pc != nil {
pc.Close()
}
m.running.Done()
}()
return entry
}
Expand Down
5 changes: 2 additions & 3 deletions service/udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"net"
"net/netip"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -207,14 +206,14 @@ func assertAlmostEqual(t *testing.T, a, b time.Time) {
}

func TestNATEmpty(t *testing.T) {
nat := newNATmap(timeout, &natTestMetrics{}, &sync.WaitGroup{}, noopLogger())
nat := newNATmap(timeout, &natTestMetrics{}, noopLogger())
if nat.Get("foo") != nil {
t.Error("Expected nil value from empty NAT map")
}
}

func setupNAT() (*fakePacketConn, *fakePacketConn, *natconn) {
nat := newNATmap(timeout, &natTestMetrics{}, &sync.WaitGroup{}, noopLogger())
nat := newNATmap(timeout, &natTestMetrics{}, noopLogger())
clientConn := makePacketConn()
targetConn := makePacketConn()
nat.Add(&clientAddr, clientConn, natCryptoKey, targetConn, "key id")
Expand Down

0 comments on commit e1fd23b

Please sign in to comment.