Skip to content

Commit 9e469e6

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

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

tailnet/conn.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,29 +260,38 @@ 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+
for {
267+
select {
268+
case <-c.closed:
269+
return
270+
case node := <-queue:
271+
callback(node)
272+
}
273+
}
274+
}()
263275
c.wireguardEngine.SetNetInfoCallback(func(ni *tailcfg.NetInfo) {
264276
c.lastMutex.Lock()
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 {
273285
return
274286
}
275-
endpoints := make([]string, 0, len(s.LocalAddrs))
287+
c.lastMutex.Lock()
288+
c.lastEndpoints = make([]string, 0, len(s.LocalAddrs))
276289
for _, addr := range s.LocalAddrs {
277-
endpoints = append(endpoints, addr.Addr.String())
290+
c.lastEndpoints = append(c.lastEndpoints, addr.Addr.String())
278291
}
279-
go func() {
280-
c.lastMutex.Lock()
281-
c.lastEndpoints = endpoints
282-
node := makeNode()
283-
c.lastMutex.Unlock()
284-
callback(node)
285-
}()
292+
node := makeNode()
293+
queue <- node
294+
c.lastMutex.Unlock()
286295
})
287296
}
288297

0 commit comments

Comments
 (0)