Skip to content

Commit 36594a8

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

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

tailnet/conn.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,29 +260,39 @@ func (c *Conn) SetNodeCallback(callback func(node *Node)) {
260260
DERPLatency: c.lastDERPLatency,
261261
}
262262
}
263+
// A send queue must be used so nodes are sent in order.
264+
queue := make(chan *Node, 16)
265+
go func() {
266+
defer close(queue)
267+
for {
268+
select {
269+
case <-c.closed:
270+
return
271+
case node := <-queue:
272+
callback(node)
273+
}
274+
}
275+
}()
263276
c.wireguardEngine.SetNetInfoCallback(func(ni *tailcfg.NetInfo) {
264277
c.lastMutex.Lock()
265278
c.lastPreferredDERP = ni.PreferredDERP
266279
c.lastDERPLatency = ni.DERPLatency
267280
node := makeNode()
281+
queue <- node
268282
c.lastMutex.Unlock()
269-
callback(node)
270283
})
271284
c.wireguardEngine.SetStatusCallback(func(s *wgengine.Status, err error) {
272285
if err != nil {
273286
return
274287
}
275-
endpoints := make([]string, 0, len(s.LocalAddrs))
288+
c.lastMutex.Lock()
289+
c.lastEndpoints = make([]string, 0, len(s.LocalAddrs))
276290
for _, addr := range s.LocalAddrs {
277-
endpoints = append(endpoints, addr.Addr.String())
291+
c.lastEndpoints = append(c.lastEndpoints, addr.Addr.String())
278292
}
279-
go func() {
280-
c.lastMutex.Lock()
281-
c.lastEndpoints = endpoints
282-
node := makeNode()
283-
c.lastMutex.Unlock()
284-
callback(node)
285-
}()
293+
node := makeNode()
294+
queue <- node
295+
c.lastMutex.Unlock()
286296
})
287297
}
288298

0 commit comments

Comments
 (0)