Skip to content

Commit 36d4b27

Browse files
committed
Simplify the natmap a little.
1 parent f80294c commit 36d4b27

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

service/udp.go

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func PacketServe(clientConn net.PacketConn, handle AssocationHandleFunc, metrics
179179
}
180180
metrics.AddNATEntry()
181181
deleteEntry := nm.Add(addr, conn)
182-
go func(conn *natconn) {
182+
go func(conn net.Conn) {
183183
defer func() {
184184
conn.Close()
185185
deleteEntry()
@@ -239,6 +239,7 @@ func (c *natconn) Close() error {
239239
close(c.bytesReadCh)
240240
return c.PacketConn.Close()
241241
}
242+
242243
func (c *natconn) RemoteAddr() net.Addr {
243244
return c.raddr
244245
}
@@ -437,33 +438,18 @@ func (m *natmap) Get(key string) *natconn {
437438
return m.keyConn[key]
438439
}
439440

440-
func (m *natmap) set(key string, pc *natconn) {
441-
m.Lock()
442-
defer m.Unlock()
443-
444-
m.keyConn[key] = pc
445-
return
446-
}
447-
448-
func (m *natmap) del(key string) *natconn {
449-
m.Lock()
450-
defer m.Unlock()
451-
452-
entry, ok := m.keyConn[key]
453-
if ok {
454-
delete(m.keyConn, key)
455-
return entry
456-
}
457-
return nil
458-
}
459-
460441
// Add adds a new UDP NAT entry to the natmap and returns a closure to delete
461442
// the entry.
462443
func (m *natmap) Add(addr net.Addr, pc *natconn) func() {
444+
m.Lock()
445+
defer m.Unlock()
446+
463447
key := addr.String()
464-
m.set(key, pc)
448+
m.keyConn[key] = pc
465449
return func() {
466-
m.del(key)
450+
m.Lock()
451+
defer m.Unlock()
452+
delete(m.keyConn, key)
467453
}
468454
}
469455

0 commit comments

Comments
 (0)