git.iko.soy/typeable/oauth2-proxy
on master help sign in
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.

commitbaf6cf38165215f6a95a2b3d9646b04b0a87c336 authorNick Meves <nick.meves@greenhouse.io> dateSat 12 Jun 2021, 11:28 (5 years ago) parentd91c3f8
2 files changed+5−10
pkg/apis/sessions/session_state.go+5−3 pkg/clock/clock.go−7
pkg/apis/sessions/session_state.go+5−3
@@ -4,13 +4,14 @@ import (
44 "bytes"
55 "context"
66 "fmt"
7+ "io"
8+ "io/ioutil"
9+ "time"
10+
711 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/clock"
812 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
913 "github.com/pierrec/lz4"
1014 "github.com/vmihailenco/msgpack/v4"
11− "io"
12− "io/ioutil"
13− "time"
1415)
1516
1617// SessionState is used to store information about the currently authenticated user session
@@ -29,6 +30,7 @@ type SessionState struct {
2930 Groups []string `msgpack:"g,omitempty"`
3031 PreferredUsername string `msgpack:"pu,omitempty"`
3132
33+ // Internal helpers, not serialized
3234 Clock clock.Clock `msgpack:"-"`
3335 Lock Lock `msgpack:"-"`
3436}
pkg/clock/clock.go−7
@@ -63,13 +63,10 @@ func Reset() *clockapi.Mock {
6363// package.
6464type Clock struct {
6565 mock *clockapi.Mock
66− sync.Mutex
6766}
6867
6968// Set sets the Clock to a clock.Mock at the given time.Time
7069func (c *Clock) Set(t time.Time) {
71− c.Lock()
72− defer c.Unlock()
7370 if c.mock == nil {
7471 c.mock = clockapi.NewMock()
7572 }
@@ -79,8 +76,6 @@ func (c *Clock) Set(t time.Time) {
7976// Add moves clock forward time.Duration if it is mocked. It will error
8077// if the clock is not mocked.
8178func (c *Clock) Add(d time.Duration) error {
82− c.Lock()
83− defer c.Unlock()
8479 if c.mock == nil {
8580 return errors.New("clock not mocked")
8681 }
@@ -91,8 +86,6 @@ func (c *Clock) Add(d time.Duration) error {
9186// Reset removes local clock.Mock. Returns any existing Mock if set in case
9287// lingering time operations are attached to it.
9388func (c *Clock) Reset() *clockapi.Mock {
94− c.Lock()
95− defer c.Unlock()
9689 existing := c.mock
9790 c.mock = nil
9891 return existing