Skip to content

Commit b054ff3

Browse files
committed
concurrent status API requests
1 parent e876323 commit b054ff3

File tree

1 file changed

+52
-33
lines changed

1 file changed

+52
-33
lines changed

src/cmd/status.go

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ type statusCmdConfig struct {
1919
configFileE2EE string
2020
}
2121

22+
// Represents one Server or Client in tree
23+
type Node struct {
24+
peerConfig peer.PeerConfig
25+
relayConfig peer.Config
26+
e2eeConfig peer.Config
27+
children []*Node
28+
interfaces []api.HostInterface
29+
error string
30+
}
31+
2232
// Defaults for status command.
2333
// See root command for shared defaults.
2434
var statusCmd = statusCmdConfig{
@@ -50,16 +60,6 @@ func init() {
5060

5161
// Run attempts to parse config files into a network diagram.
5262
func (cc statusCmdConfig) Run() {
53-
// Start building tree.
54-
type Node struct {
55-
peerConfig peer.PeerConfig
56-
relayConfig peer.Config
57-
e2eeConfig peer.Config
58-
children []*Node
59-
interfaces []api.HostInterface
60-
error string
61-
}
62-
6363
var err error
6464

6565
// Parse the relay and e2ee config files
@@ -81,32 +81,21 @@ func (cc statusCmdConfig) Run() {
8181
nodes := make(map[string]Node)
8282
var errorNodes []Node
8383
e2ee_peer_list := client.e2eeConfig.GetPeers()
84+
nodeChannel := make(chan Node)
8485
for _, ep := range e2ee_peer_list {
85-
relayConfig, e2eeConfig, err := api.ServerInfo(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort)))
86-
if err != nil {
87-
errorNodes = append(errorNodes, Node{
88-
peerConfig: ep,
89-
error: err.Error(),
90-
})
86+
// Make all the API requests concurrently to speed things up
87+
go cc.makeAPIRequests(nodeChannel, ep)
88+
}
9189

92-
} else {
93-
var interfaces []api.HostInterface
94-
if cc.networkInfo {
95-
interfaces, err = api.ServerInterfaces(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort)))
96-
if err != nil {
97-
interfaces = append(interfaces, api.HostInterface{
98-
Name: "ERROR: " + err.Error(),
99-
})
100-
}
101-
}
90+
// Don't need to do anything with values, just need to loop the same number of times
91+
for range e2ee_peer_list {
92+
responseNode := <- nodeChannel
10293

103-
nodes[relayConfig.GetPublicKey()] = Node{
104-
peerConfig: ep,
105-
relayConfig: relayConfig,
106-
e2eeConfig: e2eeConfig,
107-
interfaces: interfaces,
108-
}
109-
}
94+
if responseNode.error == "" {
95+
nodes[responseNode.relayConfig.GetPublicKey()] = responseNode
96+
} else {
97+
errorNodes = append(errorNodes, responseNode)
98+
}
11099
}
111100

112101
// Build tree by adding each relay node as a child.
@@ -237,6 +226,36 @@ Network Interfaces:
237226
}
238227
}
239228

229+
func (cc statusCmdConfig) makeAPIRequests(ch chan<- Node, ep peer.PeerConfig) {
230+
relayConfig, e2eeConfig, err := api.ServerInfo(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort)))
231+
if err != nil {
232+
ch <- Node{
233+
peerConfig: ep,
234+
error: err.Error(),
235+
}
236+
return
237+
238+
} else {
239+
var interfaces []api.HostInterface
240+
if cc.networkInfo {
241+
interfaces, err = api.ServerInterfaces(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort)))
242+
if err != nil {
243+
interfaces = append(interfaces, api.HostInterface{
244+
Name: "ERROR: " + err.Error(),
245+
})
246+
}
247+
}
248+
249+
ch <- Node{
250+
peerConfig: ep,
251+
relayConfig: relayConfig,
252+
e2eeConfig: e2eeConfig,
253+
interfaces: interfaces,
254+
}
255+
return
256+
}
257+
}
258+
240259
func errorWrap(text string, lineWidth int) string {
241260
words := strings.Fields(strings.TrimSpace(text))
242261
if len(words) == 0 {

0 commit comments

Comments
 (0)