From 3089081349c9cf670dbac6bb733bc4c74f2f1dd9 Mon Sep 17 00:00:00 2001 From: Denton Gentry Date: Fri, 2 Apr 2021 00:24:29 -0700 Subject: [PATCH] monitor/polling: reduce Cloud Run polling interval. Cloud Run's routes never change at runtime. Don't poll it for route changes very often. Signed-off-by: Denton Gentry --- wgengine/monitor/polling.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/wgengine/monitor/polling.go b/wgengine/monitor/polling.go index 4b8f81291..c2c4b6cf0 100644 --- a/wgengine/monitor/polling.go +++ b/wgengine/monitor/polling.go @@ -7,7 +7,9 @@ package monitor import ( + "bytes" "errors" + "os" "runtime" "sync" "time" @@ -42,6 +44,20 @@ func (pm *pollingMon) Close() error { return nil } +func (pm *pollingMon) isCloudRun() bool { + // https: //cloud.google.com/run/docs/reference/container-contract#env-vars + if os.Getenv("K_REVISION") == "" || os.Getenv("K_CONFIGURATION") == "" || + os.Getenv("K_SERVICE") == "" || os.Getenv("PORT") == "" { + return false + } + vers, err := os.ReadFile("/proc/version") + if err != nil { + pm.logf("Failed to read /proc/version: %v", err) + return false + } + return string(bytes.TrimSpace(vers)) == "Linux version 4.4.0 #1 SMP Sun Jan 10 15:06:54 PST 2016" +} + func (pm *pollingMon) Receive() (message, error) { d := 10 * time.Second if runtime.GOOS == "android" { @@ -50,8 +66,12 @@ func (pm *pollingMon) Receive() (message, error) { // https://github.com/tailscale/tailscale/issues/1427 d = 10 * time.Minute } - // TODO: detect if we're running in Cloud Run, and reduce frequency of - // polling as its routes never change. + if pm.isCloudRun() { + // Cloud Run routes never change at runtime. the containers are killed within + // 15 minutes by default, set the interval long enough to be effectively infinite. + pm.logf("monitor polling: Cloud Run detected, reduce polling interval to 24h") + d = 24 * time.Hour + } ticker := time.NewTicker(d) defer ticker.Stop() base := pm.m.InterfaceState()