Skip to content

Commit 882720d

Browse files
committed
Fix concurent map write for listen port #30
1 parent 9d59a3b commit 882720d

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

transport/layer.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net"
1010
"strconv"
1111
"strings"
12+
"sync"
1213
"time"
1314

1415
"github.com/emiago/sipgo/sip"
@@ -32,9 +33,11 @@ type Layer struct {
3233
tls *TLSTransport
3334
ws *WSTransport
3435

35-
transports map[string]Transport
36-
listenPorts map[string][]int
37-
dnsResolver *net.Resolver
36+
transports map[string]Transport
37+
38+
listenPorts map[string][]int
39+
listenPortsMu sync.Mutex
40+
dnsResolver *net.Resolver
3841

3942
handlers []sip.MessageHandler
4043

@@ -221,6 +224,9 @@ func (l *Layer) ListenAndServeTLS(ctx context.Context, network string, addr stri
221224
}
222225

223226
func (l *Layer) addListenPort(network string, port int) {
227+
l.listenPortsMu.Lock()
228+
defer l.listenPortsMu.Unlock()
229+
224230
if _, ok := l.listenPorts[network]; !ok {
225231
if l.listenPorts[network] == nil {
226232
l.listenPorts[network] = make([]int, 0)

0 commit comments

Comments
 (0)