Skip to content

Commit

Permalink
Add optimistic device lookup on nearest gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicletz committed Sep 23, 2024
1 parent 6089102 commit 9837e96
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ func (client *Client) newTicket() (*edge.DeviceTicket, error) {
LocalAddr: []byte{},
}

prim, secd := client.clientMan.PeekNearestClients()
prim, secd := client.clientMan.PeekNearestAddresses()

if prim != nil {
if *prim == serverID {
Expand Down
21 changes: 17 additions & 4 deletions rpc/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,27 @@ func (cm *ClientManager) GetNearestClient() (client *Client) {
return
}

// PeekNearestClients is a non-blocking version of GetNearestClient but can return nil
func (cm *ClientManager) PeekNearestClients() (prim *util.Address, secd *util.Address) {
// PeekNearestAddresses is a non-blocking version of GetNearestClient but can return nil
func (cm *ClientManager) PeekNearestAddresses() (prim *util.Address, secd *util.Address) {
primClient, secdClient := cm.PeekNearestClients()

if primClient != nil {
prim = &primClient.serverID
}

if secdClient != nil {
secd = &secdClient.serverID
}
return
}

func (cm *ClientManager) PeekNearestClients() (prim *Client, secd *Client) {
cm.srv.Call(func() {
if primary := cm.topClient(0); primary != nil {
prim = &primary.serverID
prim = primary
}
if secondary := cm.topClient(1); secondary != nil {
secd = &secondary.serverID
secd = secondary
}
})
return
Expand Down
9 changes: 9 additions & 0 deletions rpc/socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ func (socksServer *Server) doConnectDevice(requestId int64, deviceName string, p
serverID util.Address
}

nearestClient, _ := socksServer.clientManager.PeekNearestClients()

candidates := make([]candidate, 0)
for _, device := range devices {
var deviceID Address
Expand All @@ -341,8 +343,15 @@ func (socksServer *Server) doConnectDevice(requestId int64, deviceName string, p
continue
}

if nearestClient != nil {
candidates = append(candidates, candidate{deviceID, nearestClient.serverID})
}

for _, serverID := range device.GetServerIDs() {
socksServer.logger.Debug("Found device %s on server %s", deviceID.HexString(), serverID.HexString())
if nearestClient != nil && serverID == nearestClient.serverID {
continue
}
candidates = append(candidates, candidate{deviceID, serverID})
}
}
Expand Down

0 comments on commit 9837e96

Please sign in to comment.