Skip to content

Commit 07dee9e

Browse files
refactor: name RW mutexes consistently (#2822)
Followup on #2819 Signed-off-by: Alexander Yastrebov <[email protected]>
1 parent be980b8 commit 07dee9e

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

filters/auth/jwt_validation.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ type (
2626
}
2727
)
2828

29-
var m sync.RWMutex
30-
3129
var refreshInterval = time.Hour
3230
var refreshRateLimit = time.Minute * 5
3331
var refreshTimeout = time.Second * 10
3432
var refreshUnknownKID = true
3533

3634
// the map of jwks keyfunctions stored per jwksUri
37-
var jwksMap map[string]*keyfunc.JWKS = make(map[string]*keyfunc.JWKS)
35+
var (
36+
jwksMu sync.RWMutex
37+
jwksMap map[string]*keyfunc.JWKS = make(map[string]*keyfunc.JWKS)
38+
)
3839

3940
func NewJwtValidationWithOptions(o TokenintrospectionOptions) filters.Spec {
4041
return &jwtValidationSpec{
@@ -75,16 +76,16 @@ func (s *jwtValidationSpec) CreateFilter(args []interface{}) (filters.Filter, er
7576
}
7677

7778
func hasKeyFunction(url string) bool {
78-
m.RLock()
79-
defer m.RUnlock()
79+
jwksMu.RLock()
80+
defer jwksMu.RUnlock()
8081

8182
_, ok := jwksMap[url]
8283
return ok
8384
}
8485

8586
func putKeyFunction(url string, jwks *keyfunc.JWKS) {
86-
m.Lock()
87-
defer m.Unlock()
87+
jwksMu.Lock()
88+
defer jwksMu.Unlock()
8889

8990
jwksMap[url] = jwks
9091
}
@@ -114,8 +115,8 @@ func registerKeyFunction(url string) (err error) {
114115
}
115116

116117
func getKeyFunction(url string) (jwks *keyfunc.JWKS) {
117-
m.RLock()
118-
defer m.RUnlock()
118+
jwksMu.RLock()
119+
defer jwksMu.RUnlock()
119120

120121
return jwksMap[url]
121122
}

filters/auth/oidc_introspection.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919
oidcClaimsCacheKey = "oidcclaimscachekey"
2020
)
2121

22-
var gjsonModifierMutex = sync.RWMutex{}
22+
var gjsonMu sync.RWMutex
2323

2424
type (
2525
oidcIntrospectionSpec struct {
@@ -87,16 +87,16 @@ func (spec *oidcIntrospectionSpec) CreateFilter(args []interface{}) (filters.Fil
8787
return nil, filters.ErrInvalidFilterParameters
8888
}
8989

90-
gjsonModifierMutex.RLock()
90+
gjsonMu.RLock()
9191
// method is not thread safe
9292
modExists := gjson.ModifierExists("_", gjsonThisModifier)
93-
gjsonModifierMutex.RUnlock()
93+
gjsonMu.RUnlock()
9494

9595
if !modExists {
96-
gjsonModifierMutex.Lock()
96+
gjsonMu.Lock()
9797
// method is not thread safe
9898
gjson.AddModifier("_", gjsonThisModifier)
99-
gjsonModifierMutex.Unlock()
99+
gjsonMu.Unlock()
100100
}
101101

102102
return filter, nil

proxy/backendtest/backendrecorder.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ type RecordedRequest struct {
1818

1919
type BackendRecorderHandler struct {
2020
server *httptest.Server
21-
requests []RecordedRequest
22-
mutex sync.RWMutex
2321
done <-chan time.Time
22+
mu sync.RWMutex
23+
requests []RecordedRequest
2424
}
2525

2626
func NewBackendRecorder(closeAfter time.Duration) *BackendRecorderHandler {
@@ -42,18 +42,18 @@ func (rec *BackendRecorderHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
4242
if err != nil {
4343
log.Errorf("backendrecorder: error writing reading request body: %v", err)
4444
}
45-
rec.mutex.Lock()
45+
rec.mu.Lock()
4646
rec.requests = append(rec.requests, RecordedRequest{
4747
URL: r.URL,
4848
Body: string(body),
4949
})
50-
rec.mutex.Unlock()
50+
rec.mu.Unlock()
5151
}
5252

5353
func (rec *BackendRecorderHandler) GetRequests() []RecordedRequest {
54-
rec.mutex.RLock()
54+
rec.mu.RLock()
5555
requests := rec.requests
56-
rec.mutex.RUnlock()
56+
rec.mu.RUnlock()
5757
return requests
5858
}
5959

secrets/encrypter.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func newFileSecretSource(file string) SecretSource {
4949
}
5050

5151
type Encrypter struct {
52+
mu sync.RWMutex
5253
cipherSuites []cipher.AEAD
53-
mux sync.RWMutex
5454
secretSource SecretSource
5555
closer chan struct{}
5656
closedHook chan struct{}
@@ -78,8 +78,8 @@ func WithSource(s SecretSource) (*Encrypter, error) {
7878
}
7979

8080
func (e *Encrypter) CreateNonce() ([]byte, error) {
81-
e.mux.RLock()
82-
defer e.mux.RUnlock()
81+
e.mu.RLock()
82+
defer e.mu.RUnlock()
8383
if len(e.cipherSuites) > 0 {
8484
nonce := make([]byte, e.cipherSuites[0].NonceSize())
8585
if _, err := io.ReadFull(crand.Reader, nonce); err != nil {
@@ -92,8 +92,8 @@ func (e *Encrypter) CreateNonce() ([]byte, error) {
9292

9393
// Encrypt encrypts given plaintext
9494
func (e *Encrypter) Encrypt(plaintext []byte) ([]byte, error) {
95-
e.mux.RLock()
96-
defer e.mux.RUnlock()
95+
e.mu.RLock()
96+
defer e.mu.RUnlock()
9797
if len(e.cipherSuites) > 0 {
9898
nonce, err := e.CreateNonce()
9999
if err != nil {
@@ -106,8 +106,8 @@ func (e *Encrypter) Encrypt(plaintext []byte) ([]byte, error) {
106106

107107
// Decrypt decrypts given cipher text
108108
func (e *Encrypter) Decrypt(cipherText []byte) ([]byte, error) {
109-
e.mux.RLock()
110-
defer e.mux.RUnlock()
109+
e.mu.RLock()
110+
defer e.mu.RUnlock()
111111
for _, c := range e.cipherSuites {
112112
nonceSize := c.NonceSize()
113113
if len(cipherText) < nonceSize {
@@ -147,8 +147,8 @@ func (e *Encrypter) RefreshCiphers() error {
147147
}
148148
suites[i] = aesgcm
149149
}
150-
e.mux.Lock()
151-
defer e.mux.Unlock()
150+
e.mu.Lock()
151+
defer e.mu.Unlock()
152152
e.cipherSuites = suites
153153
return nil
154154
}

0 commit comments

Comments
 (0)