From baf6cf38165215f6a95a2b3d9646b04b0a87c336 Mon Sep 17 00:00:00 2001 From: Nick Meves Date: Sat, 12 Jun 2021 11:28:22 -0700 Subject: [PATCH] Remove mutex from local Clock instances They will only be used in tests, but it doesn't play nice with copy operations many tests use. The linter was not happy. While the global clock needs mutexes for parallelism, local Clocks only used it for Set/Add and didn't even use the mutex for actual time functions. --- pkg/apis/sessions/session_state.go | 8 +++++--- pkg/clock/clock.go | 7 ------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/pkg/apis/sessions/session_state.go b/pkg/apis/sessions/session_state.go index 752d8afb..08538dae 100644 --- a/pkg/apis/sessions/session_state.go +++ b/pkg/apis/sessions/session_state.go @@ -4,13 +4,14 @@ import ( "bytes" "context" "fmt" + "io" + "io/ioutil" + "time" + "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/clock" "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption" "github.com/pierrec/lz4" "github.com/vmihailenco/msgpack/v4" - "io" - "io/ioutil" - "time" ) // SessionState is used to store information about the currently authenticated user session @@ -29,6 +30,7 @@ type SessionState struct { Groups []string `msgpack:"g,omitempty"` PreferredUsername string `msgpack:"pu,omitempty"` + // Internal helpers, not serialized Clock clock.Clock `msgpack:"-"` Lock Lock `msgpack:"-"` } diff --git a/pkg/clock/clock.go b/pkg/clock/clock.go index 34b7bf23..887bf0aa 100644 --- a/pkg/clock/clock.go +++ b/pkg/clock/clock.go @@ -63,13 +63,10 @@ func Reset() *clockapi.Mock { // package. type Clock struct { mock *clockapi.Mock - sync.Mutex } // Set sets the Clock to a clock.Mock at the given time.Time func (c *Clock) Set(t time.Time) { - c.Lock() - defer c.Unlock() if c.mock == nil { c.mock = clockapi.NewMock() } @@ -79,8 +76,6 @@ func (c *Clock) Set(t time.Time) { // Add moves clock forward time.Duration if it is mocked. It will error // if the clock is not mocked. func (c *Clock) Add(d time.Duration) error { - c.Lock() - defer c.Unlock() if c.mock == nil { return errors.New("clock not mocked") } @@ -91,8 +86,6 @@ func (c *Clock) Add(d time.Duration) error { // Reset removes local clock.Mock. Returns any existing Mock if set in case // lingering time operations are attached to it. func (c *Clock) Reset() *clockapi.Mock { - c.Lock() - defer c.Unlock() existing := c.mock c.mock = nil return existing