mirror of https://github.com/knative/pkg.git
Switch to the atomic bool from uber package (#1595)
- makes code easier to follow - hides the int32->bool conversions inside the library
This commit is contained in:
parent
7b92ffa783
commit
5f6cb70257
3
go.mod
3
go.mod
|
@ -32,6 +32,7 @@ require (
|
|||
github.com/spf13/pflag v1.0.5
|
||||
github.com/tsenart/vegeta v12.7.1-0.20190725001342-b5f4fca92137+incompatible
|
||||
go.opencensus.io v0.22.4
|
||||
go.uber.org/atomic v1.6.0
|
||||
go.uber.org/multierr v1.5.0
|
||||
go.uber.org/zap v1.15.0
|
||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381
|
||||
|
@ -50,7 +51,7 @@ require (
|
|||
k8s.io/code-generator v0.18.6
|
||||
k8s.io/gengo v0.0.0-20200205140755-e0e292d8aa12
|
||||
k8s.io/klog v1.0.0
|
||||
knative.dev/test-infra v0.0.0-20200803175002-5efff0c4bd0a
|
||||
knative.dev/test-infra v0.0.0-20200806191129-68b7defbd189
|
||||
sigs.k8s.io/boskos v0.0.0-20200729174948-794df80db9c9
|
||||
)
|
||||
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1834,8 +1834,8 @@ knative.dev/test-infra v0.0.0-20200505052144-5ea2f705bb55/go.mod h1:WqF1Azka+FxP
|
|||
knative.dev/test-infra v0.0.0-20200513011557-d03429a76034/go.mod h1:aMif0KXL4g19YCYwsy4Ocjjz5xgPlseYV+B95Oo4JGE=
|
||||
knative.dev/test-infra v0.0.0-20200519015156-82551620b0a9/go.mod h1:A5b2OAXTOeHT3hHhVQm3dmtbuWvIDP7qzgtqxA3/2pE=
|
||||
knative.dev/test-infra v0.0.0-20200707183444-aed09e56ddc7/go.mod h1:RjYAhXnZqeHw9+B0zsbqSPlae0lCvjekO/nw5ZMpLCs=
|
||||
knative.dev/test-infra v0.0.0-20200803175002-5efff0c4bd0a h1:0woae+DQoLaB8PBL1V2LKoPVOm3rldG4G1HYGROo1bo=
|
||||
knative.dev/test-infra v0.0.0-20200803175002-5efff0c4bd0a/go.mod h1:Pmg2c7Z7q7BGFUV/GOpU5BlrD3ePJft4MPqx8AYBplc=
|
||||
knative.dev/test-infra v0.0.0-20200806191129-68b7defbd189 h1:PkTuK9kgtsdwrIxUAmaCmDwfRKBoXujiqNRDWO1r8cc=
|
||||
knative.dev/test-infra v0.0.0-20200806191129-68b7defbd189/go.mod h1:Pmg2c7Z7q7BGFUV/GOpU5BlrD3ePJft4MPqx8AYBplc=
|
||||
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=
|
||||
modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
|
||||
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
|
||||
|
|
|
@ -21,8 +21,8 @@ import (
|
|||
"net/http"
|
||||
"net/http/pprof"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
|
||||
"go.uber.org/atomic"
|
||||
"go.uber.org/zap"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
)
|
||||
|
@ -39,7 +39,7 @@ const (
|
|||
// Handler holds the main HTTP handler and a flag indicating
|
||||
// whether the handler is active
|
||||
type Handler struct {
|
||||
enabled int32
|
||||
enabled *atomic.Bool
|
||||
handler http.Handler
|
||||
log *zap.SugaredLogger
|
||||
}
|
||||
|
@ -58,14 +58,14 @@ func NewHandler(logger *zap.SugaredLogger, enableProfiling bool) *Handler {
|
|||
|
||||
logger.Infof("Profiling enabled: %t", enableProfiling)
|
||||
return &Handler{
|
||||
enabled: boolToInt32(enableProfiling),
|
||||
enabled: atomic.NewBool(enableProfiling),
|
||||
handler: mux,
|
||||
log: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if atomic.LoadInt32(&h.enabled) == 1 {
|
||||
if h.enabled.Load() {
|
||||
h.handler.ServeHTTP(w, r)
|
||||
} else {
|
||||
http.NotFoundHandler().ServeHTTP(w, r)
|
||||
|
@ -93,9 +93,7 @@ func (h *Handler) UpdateFromConfigMap(configMap *corev1.ConfigMap) {
|
|||
return
|
||||
}
|
||||
|
||||
new := boolToInt32(enabled)
|
||||
old := atomic.SwapInt32(&h.enabled, new)
|
||||
if old != new {
|
||||
if h.enabled.Swap(enabled) != enabled {
|
||||
h.log.Infof("Profiling enabled: %t", enabled)
|
||||
}
|
||||
}
|
||||
|
@ -107,10 +105,3 @@ func NewServer(handler http.Handler) *http.Server {
|
|||
Handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
func boolToInt32(b bool) int32 {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@ package profiling
|
|||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
@ -96,8 +95,8 @@ func TestUpdateFromConfigMap(t *testing.T) {
|
|||
t.Errorf("StatusCode: %v, want: %v", rr.Code, tt.wantStatusCode)
|
||||
}
|
||||
|
||||
if atomic.LoadInt32(&handler.enabled) != boolToInt32(tt.wantEnabled) {
|
||||
t.Fatalf("Test: %q; want %v, but got %v", tt.name, tt.wantEnabled, handler.enabled)
|
||||
if handler.enabled.Load() != tt.wantEnabled {
|
||||
t.Fatalf("Enabled got %v, want %v", handler.enabled.Load(), tt.wantEnabled)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -286,6 +286,7 @@ go.opencensus.io/trace/internal
|
|||
go.opencensus.io/trace/propagation
|
||||
go.opencensus.io/trace/tracestate
|
||||
# go.uber.org/atomic v1.6.0
|
||||
## explicit
|
||||
go.uber.org/atomic
|
||||
# go.uber.org/multierr v1.5.0
|
||||
## explicit
|
||||
|
@ -945,7 +946,7 @@ k8s.io/utils/buffer
|
|||
k8s.io/utils/integer
|
||||
k8s.io/utils/pointer
|
||||
k8s.io/utils/trace
|
||||
# knative.dev/test-infra v0.0.0-20200803175002-5efff0c4bd0a
|
||||
# knative.dev/test-infra v0.0.0-20200806191129-68b7defbd189
|
||||
## explicit
|
||||
knative.dev/test-infra/scripts
|
||||
# sigs.k8s.io/boskos v0.0.0-20200729174948-794df80db9c9
|
||||
|
|
Loading…
Reference in New Issue