atomicutil: use atomicutil.Value wherever possible (#3517)

* atomicutil: use atomicutil.Value wherever possible

* fix test

* fix mux router
This commit is contained in:
Caleb Doxsey 2022-07-28 15:38:38 -06:00 committed by GitHub
parent 5c14d2c994
commit 0ac7e45a21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 121 additions and 215 deletions

View file

@ -5,7 +5,6 @@ import (
"net"
"net/http"
"net/http/pprof"
"sync/atomic"
"time"
envoy_service_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
@ -20,6 +19,7 @@ import (
"github.com/pomerium/pomerium/config"
"github.com/pomerium/pomerium/config/envoyconfig"
"github.com/pomerium/pomerium/config/envoyconfig/filemgr"
"github.com/pomerium/pomerium/internal/atomicutil"
"github.com/pomerium/pomerium/internal/controlplane/xdsmgr"
"github.com/pomerium/pomerium/internal/events"
"github.com/pomerium/pomerium/internal/httputil/reproxy"
@ -38,18 +38,6 @@ type versionedConfig struct {
version int64
}
type atomicVersionedConfig struct {
value atomic.Value
}
func (avo *atomicVersionedConfig) Load() versionedConfig {
return avo.value.Load().(versionedConfig)
}
func (avo *atomicVersionedConfig) Store(cfg versionedConfig) {
avo.value.Store(cfg)
}
// A Service can be mounted on the control plane.
type Service interface {
Mount(r *mux.Router)
@ -67,14 +55,14 @@ type Server struct {
Builder *envoyconfig.Builder
EventsMgr *events.Manager
currentConfig atomicVersionedConfig
currentConfig *atomicutil.Value[versionedConfig]
name string
xdsmgr *xdsmgr.Manager
filemgr *filemgr.Manager
metricsMgr *config.MetricsManager
reproxy *reproxy.Handler
httpRouter atomic.Value
httpRouter *atomicutil.Value[*mux.Router]
authenticateSvc Service
proxySvc Service
@ -88,10 +76,11 @@ func NewServer(cfg *config.Config, metricsMgr *config.MetricsManager, eventsMgr
EventsMgr: eventsMgr,
reproxy: reproxy.New(),
haveSetCapacity: map[string]bool{},
currentConfig: atomicutil.NewValue(versionedConfig{
Config: cfg,
}),
httpRouter: atomicutil.NewValue(mux.NewRouter()),
}
srv.currentConfig.Store(versionedConfig{
Config: cfg,
})
var err error
@ -227,7 +216,7 @@ func (srv *Server) Run(ctx context.Context) error {
Handler http.Handler
}{
{"http", srv.HTTPListener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv.httpRouter.Load().(http.Handler).ServeHTTP(w, r)
srv.httpRouter.Load().ServeHTTP(w, r)
})},
{"debug", srv.DebugListener, srv.DebugRouter},
{"metrics", srv.MetricsListener, srv.MetricsRouter},
@ -307,6 +296,6 @@ func (srv *Server) updateRouter(cfg *config.Config) error {
if srv.proxySvc != nil {
srv.proxySvc.Mount(httpRouter)
}
srv.httpRouter.Store(http.Handler(httpRouter))
srv.httpRouter.Store(httpRouter)
return nil
}