Skip to content

Commit cbede6c

Browse files
committed
Add a queue for sending order
1 parent 42ad0d7 commit cbede6c

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

tailnet/conn.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ func (c *Conn) SetForwardTCPCallback(callback func(conn net.Conn, listenerExists
248248
// renegotiation may be required. Clients should constantly be emitting
249249
// node changes.
250250
func (c *Conn) SetNodeCallback(callback func(node *Node)) {
251+
queue := make(chan *Node, 16)
252+
go func() {
253+
defer close(queue)
254+
for {
255+
select {
256+
case <-c.closed:
257+
return
258+
case node := <-queue:
259+
callback(node)
260+
}
261+
}
262+
}()
251263
makeNode := func() *Node {
252264
return &Node{
253265
ID: c.netMap.SelfNode.ID,
@@ -265,8 +277,8 @@ func (c *Conn) SetNodeCallback(callback func(node *Node)) {
265277
c.lastPreferredDERP = ni.PreferredDERP
266278
c.lastDERPLatency = ni.DERPLatency
267279
node := makeNode()
280+
queue <- node
268281
c.lastMutex.Unlock()
269-
callback(node)
270282
})
271283
c.wireguardEngine.SetStatusCallback(func(s *wgengine.Status, err error) {
272284
if err != nil {
@@ -276,13 +288,11 @@ func (c *Conn) SetNodeCallback(callback func(node *Node)) {
276288
for _, addr := range s.LocalAddrs {
277289
endpoints = append(endpoints, addr.Addr.String())
278290
}
279-
go func() {
280-
c.lastMutex.Lock()
281-
c.lastEndpoints = endpoints
282-
node := makeNode()
283-
c.lastMutex.Unlock()
284-
callback(node)
285-
}()
291+
c.lastMutex.Lock()
292+
c.lastEndpoints = endpoints
293+
node := makeNode()
294+
queue <- node
295+
c.lastMutex.Unlock()
286296
})
287297
}
288298

0 commit comments

Comments
 (0)