From c99b485b71f16ef98acf88543f2cbf69e56bf167 Mon Sep 17 00:00:00 2001 From: Giovanni d'Amelio Date: Wed, 26 Apr 2023 11:01:00 -0700 Subject: [PATCH] Split out update devices part of fetcher into a function --- server/fetcher.go | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/server/fetcher.go b/server/fetcher.go index ea56cfc..38149b3 100644 --- a/server/fetcher.go +++ b/server/fetcher.go @@ -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, @@ -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)