UpdateCache stores the DERP map cache back to disk. The caller must not mutate 'c' after calling this function.
(c *tailcfg.DERPMap, logf logger.Logf)
| 231 | // |
| 232 | // The caller must not mutate 'c' after calling this function. |
| 233 | func UpdateCache(c *tailcfg.DERPMap, logf logger.Logf) { |
| 234 | // Don't do anything if nothing changed. |
| 235 | curr := cachedDERPMap.Load() |
| 236 | if reflect.DeepEqual(curr, c) { |
| 237 | return |
| 238 | } |
| 239 | |
| 240 | d, err := json.Marshal(c) |
| 241 | if err != nil { |
| 242 | logf("[v1] dnsfallback: UpdateCache error marshaling: %v", err) |
| 243 | return |
| 244 | } |
| 245 | |
| 246 | // Only store after we're confident this is at least valid JSON |
| 247 | cachedDERPMap.Store(c) |
| 248 | |
| 249 | // Don't try writing if we don't have a cache path set; this can happen |
| 250 | // when we don't have a state path (e.g. /var/lib/tailscale) configured. |
| 251 | if cachePath != "" { |
| 252 | err = atomicfile.WriteFile(cachePath, d, 0600) |
| 253 | if err != nil { |
| 254 | logf("[v1] dnsfallback: UpdateCache error writing: %v", err) |
| 255 | return |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // SetCachePath sets the path to the on-disk DERP map cache that we store and |
| 261 | // update. Additionally, if a file at this path exists, we load it and merge it |
searching dependent graphs…