monitor/polling: reduce Cloud Run polling interval.
Cloud Run's routes never change at runtime. Don't poll it for
route changes very often.
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()
