Skip to content

Commit

Permalink
Split out update devices part of fetcher into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
giodamelio committed Apr 26, 2023
1 parent 0fab3ce commit c99b485
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions server/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ func getDeviceName(rawDeviceName string) string {
return strings.Join(domainParts[:len(domainParts)-3], ".")
}

func updateDevices(writeDevices chan WriteDevicesOp, ts *tsapi.TSApi) {
devices, err := fetchDevices(ts)
if err != nil {
log.Warn().Err(err).Msg("Cannot fetch devices")
}

// Build a DeviceMap
var deviceMap = make(DeviceMap)
for _, device := range devices {
name := getDeviceName(device.Name)
deviceMap[name] = device
}

// Write the device map to the central store
write := WriteDevicesOp{
DeviceMap: deviceMap,
Response: make(chan bool),
}
writeDevices <- write
<-write.Response
}

// Fetch the devices on a regular basis
func SetupDeviceFetcher(
writeDevices chan WriteDevicesOp,
Expand All @@ -42,25 +64,7 @@ func SetupDeviceFetcher(
Msgf("Fetching tailnet devices every %s", duration)

for {
devices, err := fetchDevices(ts)
if err != nil {
log.Warn().Err(err).Msg("Cannot fetch devices")
}

// Build a DeviceMap
var deviceMap = make(DeviceMap)
for _, device := range devices {
name := getDeviceName(device.Name)
deviceMap[name] = device
}

// Write the device map to the central store
write := WriteDevicesOp{
DeviceMap: deviceMap,
Response: make(chan bool),
}
writeDevices <- write
<-write.Response
updateDevices(writeDevices, ts)

// Take a nap
time.Sleep(duration)
Expand Down

0 comments on commit c99b485

Please sign in to comment.