From 405daf2c0562c38366a0c413f6c92a04380d33a1 Mon Sep 17 00:00:00 2001 From: Dan Fuhry Date: Fri, 14 Nov 2025 12:29:15 -0500 Subject: [PATCH] bulk replace all signal.NotifyContext with utils/context.Interruptible() --- attestation/rpc_client/BUILD.bazel | 1 + attestation/rpc_client/main.go | 7 +++---- attestation/rpc_server/BUILD.bazel | 1 + attestation/rpc_server/main.go | 7 +++---- automation/bryston_ctl/client/BUILD.bazel | 1 + automation/bryston_ctl/client/main.go | 7 +++---- automation/bryston_ctl/server/BUILD.bazel | 1 + automation/bryston_ctl/server/main.go | 7 +++---- cmd/apcups_exporter/BUILD.bazel | 1 + cmd/apcups_exporter/main.go | 7 +++---- cmd/echo_client/BUILD.bazel | 1 + cmd/echo_client/main.go | 7 +++---- cmd/echo_server/BUILD.bazel | 1 + cmd/echo_server/main.go | 7 +++---- cmd/ephs_client/BUILD.bazel | 1 + cmd/ephs_client/main.go | 6 ++---- cmd/ephs_server/BUILD.bazel | 1 + cmd/ephs_server/main.go | 7 +++---- cmd/grpc_health_probe/BUILD.bazel | 1 + cmd/grpc_health_probe/main.go | 6 ++---- cmd/http_proxy/BUILD.bazel | 1 + cmd/http_proxy/main.go | 6 ++---- cmd/machines_event_monitor/BUILD.bazel | 1 + cmd/machines_event_monitor/main.go | 7 +++---- cmd/metricbus_server/BUILD.bazel | 1 + cmd/metricbus_server/main.go | 7 +++---- cmd/mtls_exporter/BUILD.bazel | 1 + cmd/mtls_exporter/main.go | 7 +++---- cmd/mtls_supervisor/BUILD.bazel | 1 + cmd/mtls_supervisor/main.go | 4 ++-- cmd/prometheus_http_discovery/BUILD.bazel | 1 + cmd/prometheus_http_discovery/main.go | 7 +++---- cmd/sase_ws_tcp_proxy/BUILD.bazel | 1 + cmd/sase_ws_tcp_proxy/main.go | 7 +++---- cmd/sd_health_exporter/BUILD.bazel | 1 + cmd/sd_health_exporter/main.go | 7 +++---- cmd/sd_publish/BUILD.bazel | 1 + cmd/sd_publish/main.go | 7 +++---- cmd/sd_register/BUILD.bazel | 1 + cmd/sd_register/main.go | 7 +++---- cmd/sd_watcher/BUILD.bazel | 1 + cmd/sd_watcher/main.go | 7 +++---- metrics/metricbus/mbclient/example/BUILD.bazel | 5 ++++- metrics/metricbus/mbclient/example/main.go | 7 +++---- sd/BUILD.bazel | 1 + sd/etcd_factory.go | 11 +++++++++-- thirdparty/registry/BUILD.bazel | 1 + thirdparty/registry/main.go | 7 +++---- 48 files changed, 101 insertions(+), 93 deletions(-) diff --git a/attestation/rpc_client/BUILD.bazel b/attestation/rpc_client/BUILD.bazel index 6ba0d80..71bcdad 100644 --- a/attestation/rpc_client/BUILD.bazel +++ b/attestation/rpc_client/BUILD.bazel @@ -10,6 +10,7 @@ go_library( "//grpc", "//mtls", "//proto/service/attest", + "//utils/context", ], ) diff --git a/attestation/rpc_client/main.go b/attestation/rpc_client/main.go index aad5985..d82dd98 100644 --- a/attestation/rpc_client/main.go +++ b/attestation/rpc_client/main.go @@ -1,20 +1,19 @@ package main import ( - "context" "flag" "fmt" - "os/signal" - "syscall" "go.fuhry.dev/runtime/attestation/internal/attestation" "go.fuhry.dev/runtime/grpc" "go.fuhry.dev/runtime/mtls" attest_pb "go.fuhry.dev/runtime/proto/service/attest" + "go.fuhry.dev/runtime/utils/context" ) func main() { - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() flag.Parse() diff --git a/attestation/rpc_server/BUILD.bazel b/attestation/rpc_server/BUILD.bazel index 4b6aabf..4043c16 100644 --- a/attestation/rpc_server/BUILD.bazel +++ b/attestation/rpc_server/BUILD.bazel @@ -10,6 +10,7 @@ go_library( "//grpc", "//mtls", "//proto/service/attest", + "//utils/context", "@org_golang_google_grpc//:grpc", ], ) diff --git a/attestation/rpc_server/main.go b/attestation/rpc_server/main.go index 5945a5a..e62da24 100644 --- a/attestation/rpc_server/main.go +++ b/attestation/rpc_server/main.go @@ -1,15 +1,13 @@ package main import ( - "context" "flag" - "os/signal" - "syscall" "go.fuhry.dev/runtime/attestation/internal/attestation" "go.fuhry.dev/runtime/grpc" "go.fuhry.dev/runtime/mtls" attest_pb "go.fuhry.dev/runtime/proto/service/attest" + "go.fuhry.dev/runtime/utils/context" google_grpc "google.golang.org/grpc" ) @@ -25,7 +23,8 @@ func main() { panic(err) } - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() err = s.PublishAndServe(ctx, func(s *google_grpc.Server) { attest_pb.RegisterAttestServer(s, attestation.NewAttestationServer()) diff --git a/automation/bryston_ctl/client/BUILD.bazel b/automation/bryston_ctl/client/BUILD.bazel index 29d2dc4..be8018a 100644 --- a/automation/bryston_ctl/client/BUILD.bazel +++ b/automation/bryston_ctl/client/BUILD.bazel @@ -10,6 +10,7 @@ go_library( "//grpc", "//mtls", "//proto/service/bryston_ctl", + "//utils/context", "//utils/log", ], ) diff --git a/automation/bryston_ctl/client/main.go b/automation/bryston_ctl/client/main.go index 723d8f6..5695ac0 100644 --- a/automation/bryston_ctl/client/main.go +++ b/automation/bryston_ctl/client/main.go @@ -1,20 +1,19 @@ package main import ( - "context" "flag" - "os/signal" - "syscall" "go.fuhry.dev/runtime/automation/bryston_ctl" "go.fuhry.dev/runtime/grpc" "go.fuhry.dev/runtime/mtls" bryston_ctl_pb "go.fuhry.dev/runtime/proto/service/bryston_ctl" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" ) func main() { - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() model, modelFF := bryston_ctl.NewBrystonModelFlag() flag.Func("model", "Bryston device model to control", modelFF) diff --git a/automation/bryston_ctl/server/BUILD.bazel b/automation/bryston_ctl/server/BUILD.bazel index a360b33..f176641 100644 --- a/automation/bryston_ctl/server/BUILD.bazel +++ b/automation/bryston_ctl/server/BUILD.bazel @@ -10,6 +10,7 @@ go_library( "//grpc", "//mtls", "//proto/service/bryston_ctl", + "//utils/context", "//utils/log", "@org_golang_google_grpc//:grpc", ], diff --git a/automation/bryston_ctl/server/main.go b/automation/bryston_ctl/server/main.go index d93fe73..6099f5f 100644 --- a/automation/bryston_ctl/server/main.go +++ b/automation/bryston_ctl/server/main.go @@ -1,10 +1,7 @@ package main import ( - "context" "flag" - "os/signal" - "syscall" google_grpc "google.golang.org/grpc" @@ -12,6 +9,7 @@ import ( "go.fuhry.dev/runtime/grpc" "go.fuhry.dev/runtime/mtls" bryston_ctl_pb "go.fuhry.dev/runtime/proto/service/bryston_ctl" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" ) @@ -26,7 +24,8 @@ func main() { log.Panic(err) } - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() ctl, err := bryston_ctl.NewController() if err != nil { diff --git a/cmd/apcups_exporter/BUILD.bazel b/cmd/apcups_exporter/BUILD.bazel index fc8d960..5357380 100644 --- a/cmd/apcups_exporter/BUILD.bazel +++ b/cmd/apcups_exporter/BUILD.bazel @@ -7,6 +7,7 @@ go_library( visibility = ["//visibility:private"], deps = [ "//metrics/metricbus/mbclient", + "//utils/context", "//utils/log", "@com_github_coreos_go_systemd//daemon", "@com_github_mdlayher_apcupsd//:apcupsd", diff --git a/cmd/apcups_exporter/main.go b/cmd/apcups_exporter/main.go index 54e7b71..e7d69ba 100644 --- a/cmd/apcups_exporter/main.go +++ b/cmd/apcups_exporter/main.go @@ -1,17 +1,15 @@ package main import ( - "context" "flag" - "os/signal" "sync" - "syscall" "time" "github.com/coreos/go-systemd/daemon" "github.com/mdlayher/apcupsd" "go.fuhry.dev/runtime/metrics/metricbus/mbclient" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" ) @@ -31,7 +29,8 @@ func main() { flag.StringVar(&apcUpsdAddr, "apcupsd-addr", apcUpsdAddr, "address at which apcupsd can be contacted") flag.IntVar(&nominalPower, "apcupsd-nominal-power", nominalPower, "max power output in W for the UPS - only used if apcupsd does not specify NOMPOWER") - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() flag.Parse() svc := mbclient.NewService(ctx) diff --git a/cmd/echo_client/BUILD.bazel b/cmd/echo_client/BUILD.bazel index 6f28a7b..7e06801 100644 --- a/cmd/echo_client/BUILD.bazel +++ b/cmd/echo_client/BUILD.bazel @@ -9,6 +9,7 @@ go_library( "//grpc", "//mtls", "//proto/service/echo", + "//utils/context", "//utils/log", ], ) diff --git a/cmd/echo_client/main.go b/cmd/echo_client/main.go index 98063ec..76f7cf1 100644 --- a/cmd/echo_client/main.go +++ b/cmd/echo_client/main.go @@ -1,19 +1,18 @@ package main import ( - "context" "flag" - "os/signal" - "syscall" "go.fuhry.dev/runtime/grpc" "go.fuhry.dev/runtime/mtls" echo_pb "go.fuhry.dev/runtime/proto/service/echo" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" ) func main() { - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() flag.Parse() logger := log.Default().WithPrefix("EchoClient") diff --git a/cmd/echo_server/BUILD.bazel b/cmd/echo_server/BUILD.bazel index db44ca8..206c25c 100644 --- a/cmd/echo_server/BUILD.bazel +++ b/cmd/echo_server/BUILD.bazel @@ -10,6 +10,7 @@ go_library( "//grpc", "//mtls", "//proto/service/echo", + "//utils/context", "@org_golang_google_grpc//:grpc", ], ) diff --git a/cmd/echo_server/main.go b/cmd/echo_server/main.go index a4b30fd..d7c6347 100644 --- a/cmd/echo_server/main.go +++ b/cmd/echo_server/main.go @@ -1,15 +1,13 @@ package main import ( - "context" "flag" - "os/signal" - "syscall" "go.fuhry.dev/runtime/echo" "go.fuhry.dev/runtime/grpc" "go.fuhry.dev/runtime/mtls" echo_pb "go.fuhry.dev/runtime/proto/service/echo" + "go.fuhry.dev/runtime/utils/context" google_grpc "google.golang.org/grpc" ) @@ -25,7 +23,8 @@ func main() { panic(err) } - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() err = s.PublishAndServe(ctx, func(s *google_grpc.Server) { echo_pb.RegisterEchoServer(s, echo.NewEchoServer()) diff --git a/cmd/ephs_client/BUILD.bazel b/cmd/ephs_client/BUILD.bazel index e0a14f5..ee40c32 100644 --- a/cmd/ephs_client/BUILD.bazel +++ b/cmd/ephs_client/BUILD.bazel @@ -8,6 +8,7 @@ go_library( deps = [ "//constants", "//ephs", + "//utils/context", "//utils/log", "@com_github_urfave_cli_v3//:cli", ], diff --git a/cmd/ephs_client/main.go b/cmd/ephs_client/main.go index f15ca24..b0b113d 100644 --- a/cmd/ephs_client/main.go +++ b/cmd/ephs_client/main.go @@ -1,21 +1,19 @@ package main import ( - "context" "errors" "flag" "fmt" "io" "os" - "os/signal" "path" "strings" - "syscall" "github.com/urfave/cli/v3" "go.fuhry.dev/runtime/constants" "go.fuhry.dev/runtime/ephs" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" ) @@ -157,7 +155,7 @@ func cmdMkdir(ctx context.Context, cmd *cli.Command) error { } func main() { - ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() defer cancel() flag.Parse() diff --git a/cmd/ephs_server/BUILD.bazel b/cmd/ephs_server/BUILD.bazel index f983df0..3d77a80 100644 --- a/cmd/ephs_server/BUILD.bazel +++ b/cmd/ephs_server/BUILD.bazel @@ -10,6 +10,7 @@ go_library( "//grpc", "//mtls", "//proto/service/ephs", + "//utils/context", "//utils/log", "@org_golang_google_grpc//:grpc", ], diff --git a/cmd/ephs_server/main.go b/cmd/ephs_server/main.go index d0e24cf..2995fa3 100644 --- a/cmd/ephs_server/main.go +++ b/cmd/ephs_server/main.go @@ -1,15 +1,13 @@ package main import ( - "context" "flag" - "os/signal" - "syscall" "go.fuhry.dev/runtime/ephs/servicer" "go.fuhry.dev/runtime/grpc" "go.fuhry.dev/runtime/mtls" ephs_pb "go.fuhry.dev/runtime/proto/service/ephs" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" google_grpc "google.golang.org/grpc" @@ -46,7 +44,8 @@ func main() { log.Fatal(err) } - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() err = s.PublishAndServe(ctx, func(s *google_grpc.Server) { ephs_pb.RegisterEphsServer(s, serv) diff --git a/cmd/grpc_health_probe/BUILD.bazel b/cmd/grpc_health_probe/BUILD.bazel index cda6e8e..1d65ca1 100644 --- a/cmd/grpc_health_probe/BUILD.bazel +++ b/cmd/grpc_health_probe/BUILD.bazel @@ -8,6 +8,7 @@ go_library( deps = [ "//grpc", "//mtls", + "//utils/context", "//utils/log", "@org_golang_google_grpc//health/grpc_health_v1", ], diff --git a/cmd/grpc_health_probe/main.go b/cmd/grpc_health_probe/main.go index a32adc9..648a0ab 100644 --- a/cmd/grpc_health_probe/main.go +++ b/cmd/grpc_health_probe/main.go @@ -1,17 +1,15 @@ package main import ( - "context" "flag" "net" "os" - "os/signal" "strconv" - "syscall" "time" "go.fuhry.dev/runtime/grpc" "go.fuhry.dev/runtime/mtls" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" "google.golang.org/grpc/health/grpc_health_v1" @@ -55,7 +53,7 @@ func main() { } } - ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() defer cancel() var deadline time.Time diff --git a/cmd/http_proxy/BUILD.bazel b/cmd/http_proxy/BUILD.bazel index b296f29..edf922b 100644 --- a/cmd/http_proxy/BUILD.bazel +++ b/cmd/http_proxy/BUILD.bazel @@ -9,6 +9,7 @@ go_library( "//config_watcher", "//ephs", "//http", + "//utils/context", "//utils/log", "@com_github_coreos_go_systemd//daemon", "@in_gopkg_yaml_v3//:yaml_v3", diff --git a/cmd/http_proxy/main.go b/cmd/http_proxy/main.go index c0c6b90..fa7c042 100644 --- a/cmd/http_proxy/main.go +++ b/cmd/http_proxy/main.go @@ -1,10 +1,7 @@ package main import ( - "context" "flag" - "os/signal" - "syscall" "time" "github.com/coreos/go-systemd/daemon" @@ -13,11 +10,12 @@ import ( "go.fuhry.dev/runtime/config_watcher" "go.fuhry.dev/runtime/ephs" "go.fuhry.dev/runtime/http" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" ) func main() { - ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() defer cancel() ephs.DefaultClientContext = ctx diff --git a/cmd/machines_event_monitor/BUILD.bazel b/cmd/machines_event_monitor/BUILD.bazel index 1e73bcf..986d02c 100644 --- a/cmd/machines_event_monitor/BUILD.bazel +++ b/cmd/machines_event_monitor/BUILD.bazel @@ -7,6 +7,7 @@ go_library( visibility = ["//visibility:private"], deps = [ "//machines", + "//utils/context", "//utils/log", ], ) diff --git a/cmd/machines_event_monitor/main.go b/cmd/machines_event_monitor/main.go index c79325a..c1ce442 100644 --- a/cmd/machines_event_monitor/main.go +++ b/cmd/machines_event_monitor/main.go @@ -1,13 +1,11 @@ package main import ( - "context" "flag" "os" - "os/signal" - "syscall" "go.fuhry.dev/runtime/machines" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" ) @@ -15,7 +13,8 @@ func main() { flag.Parse() logger := log.WithPrefix("main") - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() client, err := machines.NewDefaultMachinesClient("host.attestation.client") if err != nil { diff --git a/cmd/metricbus_server/BUILD.bazel b/cmd/metricbus_server/BUILD.bazel index 7a2dbc7..46abb28 100644 --- a/cmd/metricbus_server/BUILD.bazel +++ b/cmd/metricbus_server/BUILD.bazel @@ -7,6 +7,7 @@ go_library( visibility = ["//visibility:private"], deps = [ "//metrics/metricbus/mbserver", + "//utils/context", "//utils/log", ], ) diff --git a/cmd/metricbus_server/main.go b/cmd/metricbus_server/main.go index c48621c..3bc2279 100644 --- a/cmd/metricbus_server/main.go +++ b/cmd/metricbus_server/main.go @@ -1,17 +1,16 @@ package main import ( - "context" "flag" - "os/signal" - "syscall" "go.fuhry.dev/runtime/metrics/metricbus/mbserver" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" ) func main() { - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() serverCtx, cancel := context.WithCancel(context.Background()) flag.Parse() diff --git a/cmd/mtls_exporter/BUILD.bazel b/cmd/mtls_exporter/BUILD.bazel index 797409d..24d2978 100644 --- a/cmd/mtls_exporter/BUILD.bazel +++ b/cmd/mtls_exporter/BUILD.bazel @@ -7,6 +7,7 @@ go_library( visibility = ["//visibility:private"], deps = [ "//metrics/mtls", + "//utils/context", "@com_github_coreos_go_systemd//daemon", ], ) diff --git a/cmd/mtls_exporter/main.go b/cmd/mtls_exporter/main.go index 636dbad..6c6abd2 100644 --- a/cmd/mtls_exporter/main.go +++ b/cmd/mtls_exporter/main.go @@ -1,18 +1,17 @@ package main import ( - "context" "flag" - "os/signal" - "syscall" "time" "github.com/coreos/go-systemd/daemon" "go.fuhry.dev/runtime/metrics/mtls" + "go.fuhry.dev/runtime/utils/context" ) func main() { - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() flag.Parse() svc := mtls.NewMtlsMetricsService(ctx) diff --git a/cmd/mtls_supervisor/BUILD.bazel b/cmd/mtls_supervisor/BUILD.bazel index 103280f..4c16b5b 100644 --- a/cmd/mtls_supervisor/BUILD.bazel +++ b/cmd/mtls_supervisor/BUILD.bazel @@ -10,6 +10,7 @@ go_library( visibility = ["//visibility:private"], deps = [ "//mtls/fsnotify", + "//utils/context", "//utils/debounce", "//utils/hashset", "//utils/log", diff --git a/cmd/mtls_supervisor/main.go b/cmd/mtls_supervisor/main.go index 6e0c213..fe36c94 100644 --- a/cmd/mtls_supervisor/main.go +++ b/cmd/mtls_supervisor/main.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "context" "crypto" "fmt" "io" @@ -17,6 +16,7 @@ import ( "time" "go.fuhry.dev/runtime/mtls/fsnotify" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/debounce" "go.fuhry.dev/runtime/utils/hashset" "go.fuhry.dev/runtime/utils/log" @@ -32,7 +32,7 @@ func main() { log.SetLevel(log.DEBUG) } - ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() logger := log.Default().WithPrefix("supervisor") defer cancel() diff --git a/cmd/prometheus_http_discovery/BUILD.bazel b/cmd/prometheus_http_discovery/BUILD.bazel index 01c072d..a35c126 100644 --- a/cmd/prometheus_http_discovery/BUILD.bazel +++ b/cmd/prometheus_http_discovery/BUILD.bazel @@ -9,6 +9,7 @@ go_library( "//constants", "//mtls", "//sd", + "//utils/context", "//utils/log", "@com_github_coreos_go_systemd//daemon", ], diff --git a/cmd/prometheus_http_discovery/main.go b/cmd/prometheus_http_discovery/main.go index 7e6462a..b286dfb 100644 --- a/cmd/prometheus_http_discovery/main.go +++ b/cmd/prometheus_http_discovery/main.go @@ -1,14 +1,11 @@ package main import ( - "context" "encoding/json" "flag" "fmt" "net/http" - "os/signal" "sync" - "syscall" "time" "github.com/coreos/go-systemd/daemon" @@ -16,6 +13,7 @@ import ( "go.fuhry.dev/runtime/constants" "go.fuhry.dev/runtime/mtls" "go.fuhry.dev/runtime/sd" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" ) @@ -26,7 +24,8 @@ type endpoint struct { func main() { mtls.SetDefaultIdentity("node-exporter") - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() serverCtx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/cmd/sase_ws_tcp_proxy/BUILD.bazel b/cmd/sase_ws_tcp_proxy/BUILD.bazel index aae5a06..2cfd80c 100644 --- a/cmd/sase_ws_tcp_proxy/BUILD.bazel +++ b/cmd/sase_ws_tcp_proxy/BUILD.bazel @@ -11,6 +11,7 @@ go_library( "//rand", "//sase", "//sd", + "//utils/context", ], ) diff --git a/cmd/sase_ws_tcp_proxy/main.go b/cmd/sase_ws_tcp_proxy/main.go index 89e5057..51d7d2c 100644 --- a/cmd/sase_ws_tcp_proxy/main.go +++ b/cmd/sase_ws_tcp_proxy/main.go @@ -1,13 +1,10 @@ package main import ( - "context" "flag" "fmt" - "os/signal" "strconv" "strings" - "syscall" "time" "go.fuhry.dev/runtime/constants" @@ -15,6 +12,7 @@ import ( "go.fuhry.dev/runtime/rand" "go.fuhry.dev/runtime/sase" "go.fuhry.dev/runtime/sd" + "go.fuhry.dev/runtime/utils/context" ) func main() { @@ -28,7 +26,8 @@ func main() { flag.Parse() - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() listenPort, err := strconv.Atoi(addr[strings.LastIndex(addr, ":")+1:]) if err != nil { diff --git a/cmd/sd_health_exporter/BUILD.bazel b/cmd/sd_health_exporter/BUILD.bazel index 061f94f..7030a73 100644 --- a/cmd/sd_health_exporter/BUILD.bazel +++ b/cmd/sd_health_exporter/BUILD.bazel @@ -8,6 +8,7 @@ go_library( deps = [ "//mtls", "//sd", + "//utils/context", "@io_etcd_go_etcd_client_v3//:client", ], ) diff --git a/cmd/sd_health_exporter/main.go b/cmd/sd_health_exporter/main.go index bd58ab1..26db852 100644 --- a/cmd/sd_health_exporter/main.go +++ b/cmd/sd_health_exporter/main.go @@ -1,21 +1,19 @@ package main import ( - "context" "crypto/tls" "encoding/json" "flag" "fmt" "net" "net/http" - "os/signal" "strings" "sync" - "syscall" etcd_client "go.etcd.io/etcd/client/v3" "go.fuhry.dev/runtime/mtls" "go.fuhry.dev/runtime/sd" + "go.fuhry.dev/runtime/utils/context" ) func main() { @@ -32,7 +30,8 @@ func main() { keys := make(map[string][]byte, 0) keysLock := &sync.RWMutex{} - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() handler := http.NewServeMux() server := &http.Server{ diff --git a/cmd/sd_publish/BUILD.bazel b/cmd/sd_publish/BUILD.bazel index ca300a0..4ac44c6 100644 --- a/cmd/sd_publish/BUILD.bazel +++ b/cmd/sd_publish/BUILD.bazel @@ -8,6 +8,7 @@ go_library( deps = [ "//constants", "//sd", + "//utils/context", "//utils/hostname", ], ) diff --git a/cmd/sd_publish/main.go b/cmd/sd_publish/main.go index 76813c9..10e4d30 100644 --- a/cmd/sd_publish/main.go +++ b/cmd/sd_publish/main.go @@ -1,19 +1,18 @@ package main import ( - "context" "flag" - "os/signal" "strings" - "syscall" "go.fuhry.dev/runtime/constants" "go.fuhry.dev/runtime/sd" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/hostname" ) func main() { - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() svc := &sd.SDPublisher{} var port uint diff --git a/cmd/sd_register/BUILD.bazel b/cmd/sd_register/BUILD.bazel index e69af4e..63e4a58 100644 --- a/cmd/sd_register/BUILD.bazel +++ b/cmd/sd_register/BUILD.bazel @@ -8,6 +8,7 @@ go_library( deps = [ "//constants", "//sd", + "//utils/context", "//utils/hostname", "//utils/log", ], diff --git a/cmd/sd_register/main.go b/cmd/sd_register/main.go index 2f85d09..3db2a50 100644 --- a/cmd/sd_register/main.go +++ b/cmd/sd_register/main.go @@ -1,18 +1,16 @@ package main import ( - "context" "flag" "fmt" "os" - "os/signal" "path" "strings" "sync" - "syscall" "go.fuhry.dev/runtime/constants" "go.fuhry.dev/runtime/sd" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/hostname" "go.fuhry.dev/runtime/utils/log" ) @@ -57,7 +55,8 @@ func main() { flag.StringVar(®ion, "region", defaultRegion, "regional shard to publish under; separate multiple shards with commas") flag.Parse() - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() if len(confs) < 1 { flag.Usage() diff --git a/cmd/sd_watcher/BUILD.bazel b/cmd/sd_watcher/BUILD.bazel index a22f229..de324cd 100644 --- a/cmd/sd_watcher/BUILD.bazel +++ b/cmd/sd_watcher/BUILD.bazel @@ -8,6 +8,7 @@ go_library( deps = [ "//constants", "//sd", + "//utils/context", ], ) diff --git a/cmd/sd_watcher/main.go b/cmd/sd_watcher/main.go index a88a793..63d814d 100644 --- a/cmd/sd_watcher/main.go +++ b/cmd/sd_watcher/main.go @@ -1,16 +1,14 @@ package main import ( - "context" "flag" "fmt" "os" - "os/signal" - "syscall" "time" "go.fuhry.dev/runtime/constants" "go.fuhry.dev/runtime/sd" + "go.fuhry.dev/runtime/utils/context" ) func main() { @@ -37,7 +35,8 @@ func main() { panic(err) } - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() watcher := &sd.SDWatcher{ Domain: domain, diff --git a/metrics/metricbus/mbclient/example/BUILD.bazel b/metrics/metricbus/mbclient/example/BUILD.bazel index 0220762..b6ec816 100644 --- a/metrics/metricbus/mbclient/example/BUILD.bazel +++ b/metrics/metricbus/mbclient/example/BUILD.bazel @@ -5,7 +5,10 @@ go_library( srcs = ["main.go"], importpath = "go.fuhry.dev/runtime/metrics/metricbus/mbclient/example", visibility = ["//visibility:private"], - deps = ["//metrics/metricbus/mbclient"], + deps = [ + "//metrics/metricbus/mbclient", + "//utils/context", + ], ) go_binary( diff --git a/metrics/metricbus/mbclient/example/main.go b/metrics/metricbus/mbclient/example/main.go index 9f9a72f..411be18 100644 --- a/metrics/metricbus/mbclient/example/main.go +++ b/metrics/metricbus/mbclient/example/main.go @@ -1,18 +1,17 @@ package main import ( - "context" "flag" "math/rand" - "os/signal" - "syscall" "time" "go.fuhry.dev/runtime/metrics/metricbus/mbclient" + "go.fuhry.dev/runtime/utils/context" ) func main() { - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() flag.Parse() svc := mbclient.NewServiceWithDiscriminator(ctx, "merr") diff --git a/sd/BUILD.bazel b/sd/BUILD.bazel index 9f1257b..9c2fa50 100644 --- a/sd/BUILD.bazel +++ b/sd/BUILD.bazel @@ -19,6 +19,7 @@ go_library( "//mtls/certutil", "//net/dns", "//utils", + "//utils/context", "//utils/hostname", "//utils/log", "@com_github_go_ldap_ldap_v3//:ldap", diff --git a/sd/etcd_factory.go b/sd/etcd_factory.go index d9a9524..11c183e 100644 --- a/sd/etcd_factory.go +++ b/sd/etcd_factory.go @@ -1,7 +1,7 @@ package sd import ( - "context" + "errors" "flag" "fmt" "sync" @@ -11,6 +11,7 @@ import ( "go.etcd.io/etcd/client/pkg/v3/srv" etcd_client "go.etcd.io/etcd/client/v3" "go.fuhry.dev/runtime/mtls" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/hostname" "go.fuhry.dev/runtime/utils/log" ) @@ -39,6 +40,9 @@ func NewDefaultEtcdClient() (*etcd_client.Client, error) { } clientSingleton, err = NewEtcdClient(id, etcdDiscoveryDomain) + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + return nil, err + } if err == nil { break } @@ -63,7 +67,9 @@ func NewEtcdClient(id mtls.Identity, domain string) (*etcd_client.Client, error) func NewEtcdClientWithDeadline(id mtls.Identity, domain string, deadline time.Time) (*etcd_client.Client, error) { var client *etcd_client.Client - tlsConfig, err := id.TlsConfig(context.Background()) + ctx, _ := context.Interruptible() + + tlsConfig, err := id.TlsConfig(ctx) if err != nil { return nil, fmt.Errorf("failed to setup client TLS configuration: %v", err) } @@ -82,6 +88,7 @@ func NewEtcdClientWithDeadline(id mtls.Identity, domain string, deadline time.Ti dialTimeout := time.Until(deadline) clientConfig := etcd_client.Config{ + Context: ctx, DialTimeout: dialTimeout, Endpoints: clients.Endpoints, TLS: tlsConfig, diff --git a/thirdparty/registry/BUILD.bazel b/thirdparty/registry/BUILD.bazel index f91e4d0..44f7322 100644 --- a/thirdparty/registry/BUILD.bazel +++ b/thirdparty/registry/BUILD.bazel @@ -11,6 +11,7 @@ go_library( "//metrics/metricbus/mbclient", "//mtls", "//sd", + "//utils/context", "//utils/log", "@com_github_distribution_distribution_v3//configuration", "@com_github_distribution_distribution_v3//registry/auth/htpasswd", diff --git a/thirdparty/registry/main.go b/thirdparty/registry/main.go index 30d026d..6ad7a1f 100644 --- a/thirdparty/registry/main.go +++ b/thirdparty/registry/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "crypto/tls" "flag" "fmt" @@ -9,9 +8,7 @@ import ( "net/http" _ "net/http/pprof" "os" - "os/signal" "strconv" - "syscall" "github.com/distribution/distribution/v3/configuration" _ "github.com/distribution/distribution/v3/registry/auth/htpasswd" @@ -28,6 +25,7 @@ import ( "go.fuhry.dev/runtime/metrics/metricbus/mbclient" "go.fuhry.dev/runtime/mtls" "go.fuhry.dev/runtime/sd" + "go.fuhry.dev/runtime/utils/context" "go.fuhry.dev/runtime/utils/log" ) @@ -52,7 +50,8 @@ func main() { flag.StringVar(&configPath, "registry.config-path", "config.yml", "path to configuration file") flag.Parse() logger = log.WithPrefix("registry") - ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + ctx, cancel := context.Interruptible() + defer cancel() metricsService := mbclient.NewService(ctx) defer metricsService.FlushAndWait() -- 2.50.1