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

@ -1,7 +1,6 @@
package manager
import (
"sync/atomic"
"time"
"github.com/pomerium/pomerium/internal/directory"
@ -106,21 +105,3 @@ func WithEventManager(mgr *events.Manager) Option {
c.eventMgr = mgr
}
}
type atomicConfig struct {
value atomic.Value
}
func newAtomicConfig(cfg *config) *atomicConfig {
ac := new(atomicConfig)
ac.Store(cfg)
return ac
}
func (ac *atomicConfig) Load() *config {
return ac.value.Load().(*config)
}
func (ac *atomicConfig) Store(cfg *config) {
ac.value.Store(cfg)
}

View file

@ -14,6 +14,7 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/pomerium/pomerium/internal/atomicutil"
"github.com/pomerium/pomerium/internal/directory"
"github.com/pomerium/pomerium/internal/events"
"github.com/pomerium/pomerium/internal/identity/identity"
@ -43,7 +44,7 @@ type (
// A Manager refreshes identity information using session and user data.
type Manager struct {
cfg *atomicConfig
cfg *atomicutil.Value[*config]
sessionScheduler *scheduler.Scheduler
userScheduler *scheduler.Scheduler
@ -62,7 +63,7 @@ func New(
options ...Option,
) *Manager {
mgr := &Manager{
cfg: newAtomicConfig(newConfig()),
cfg: atomicutil.NewValue(newConfig()),
sessionScheduler: scheduler.New(),
userScheduler: scheduler.New(),

View file

@ -3,11 +3,12 @@ package manager
import (
"context"
"github.com/pomerium/pomerium/internal/atomicutil"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
)
type dataBrokerSyncer struct {
cfg *atomicConfig
cfg *atomicutil.Value[*config]
update chan<- updateRecordsMessage
clear chan<- struct{}
@ -17,7 +18,7 @@ type dataBrokerSyncer struct {
func newDataBrokerSyncer(
ctx context.Context,
cfg *atomicConfig,
cfg *atomicutil.Value[*config],
update chan<- updateRecordsMessage,
clear chan<- struct{},
) *dataBrokerSyncer {

View file

@ -6,7 +6,6 @@ import (
"context"
"fmt"
"net/url"
"sync/atomic"
"golang.org/x/oauth2"
@ -66,30 +65,3 @@ func NewAuthenticator(o oauth.Options) (a Authenticator, err error) {
}
return a, nil
}
// wrap the Authenticator for the AtomicAuthenticator to support a nil default value.
type authenticatorValue struct {
Authenticator
}
// An AtomicAuthenticator is a strongly-typed atomic.Value for storing an authenticator.
type AtomicAuthenticator struct {
current atomic.Value
}
// NewAtomicAuthenticator creates a new AtomicAuthenticator.
func NewAtomicAuthenticator() *AtomicAuthenticator {
a := &AtomicAuthenticator{}
a.current.Store(authenticatorValue{})
return a
}
// Load loads the current authenticator.
func (a *AtomicAuthenticator) Load() Authenticator {
return a.current.Load().(authenticatorValue)
}
// Store stores the authenticator.
func (a *AtomicAuthenticator) Store(value Authenticator) {
a.current.Store(authenticatorValue{value})
}