1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-03-19 21:27:58 +02:00

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.
This commit is contained in:
Nick Meves 2021-06-12 11:28:22 -07:00
parent d91c3f867d
commit baf6cf3816
2 changed files with 5 additions and 10 deletions

View File

@ -4,13 +4,14 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io"
"io/ioutil"
"time"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/clock" "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/clock"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption" "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
"github.com/pierrec/lz4" "github.com/pierrec/lz4"
"github.com/vmihailenco/msgpack/v4" "github.com/vmihailenco/msgpack/v4"
"io"
"io/ioutil"
"time"
) )
// SessionState is used to store information about the currently authenticated user session // SessionState is used to store information about the currently authenticated user session
@ -29,6 +30,7 @@ type SessionState struct {
Groups []string `msgpack:"g,omitempty"` Groups []string `msgpack:"g,omitempty"`
PreferredUsername string `msgpack:"pu,omitempty"` PreferredUsername string `msgpack:"pu,omitempty"`
// Internal helpers, not serialized
Clock clock.Clock `msgpack:"-"` Clock clock.Clock `msgpack:"-"`
Lock Lock `msgpack:"-"` Lock Lock `msgpack:"-"`
} }

View File

@ -63,13 +63,10 @@ func Reset() *clockapi.Mock {
// package. // package.
type Clock struct { type Clock struct {
mock *clockapi.Mock mock *clockapi.Mock
sync.Mutex
} }
// Set sets the Clock to a clock.Mock at the given time.Time // Set sets the Clock to a clock.Mock at the given time.Time
func (c *Clock) Set(t time.Time) { func (c *Clock) Set(t time.Time) {
c.Lock()
defer c.Unlock()
if c.mock == nil { if c.mock == nil {
c.mock = clockapi.NewMock() 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 // Add moves clock forward time.Duration if it is mocked. It will error
// if the clock is not mocked. // if the clock is not mocked.
func (c *Clock) Add(d time.Duration) error { func (c *Clock) Add(d time.Duration) error {
c.Lock()
defer c.Unlock()
if c.mock == nil { if c.mock == nil {
return errors.New("clock not mocked") 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 // Reset removes local clock.Mock. Returns any existing Mock if set in case
// lingering time operations are attached to it. // lingering time operations are attached to it.
func (c *Clock) Reset() *clockapi.Mock { func (c *Clock) Reset() *clockapi.Mock {
c.Lock()
defer c.Unlock()
existing := c.mock existing := c.mock
c.mock = nil c.mock = nil
return existing return existing