From dc7bd11d855f0b3266612a18606801f4e09ecc09 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 21 Jul 2026 14:18:35 +0200 Subject: make Account.Headers and Container.Headers thread-safe For type Object, it is a good tradeoff to not carry a mutex in every instance, since Objects are likely only used in the scope of a particular operation (and thus within an individual goroutine). But Account and Container instances are much more likely to be initialized once on startup and then shared across goroutines, so making them safer to use at the expense of a slight increase in resource usage looks like a good choice. --- account.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'account.go') diff --git a/account.go b/account.go index 2bc3b2b..d783baf 100644 --- a/account.go +++ b/account.go @@ -36,10 +36,11 @@ type Account struct { baseURL string name string // cache - headers *AccountHeaders - caps *Capabilities - modifyCaps func(*Capabilities) - capsMutex sync.Mutex + headers *AccountHeaders + headersMutex sync.Mutex + caps *Capabilities + modifyCaps func(*Capabilities) + capsMutex sync.Mutex } // IsEqualTo returns true if both Account instances refer to the same account. @@ -96,10 +97,9 @@ func (a *Account) Backend() Backend { // has not been cached yet, a HEAD request is issued on the account. // // This operation fails with http.StatusNotFound if the account does not exist. -// -// WARNING: This method is not thread-safe. Calling it concurrently on the same -// object results in undefined behavior. func (a *Account) Headers(ctx context.Context) (AccountHeaders, error) { + a.headersMutex.Lock() + defer a.headersMutex.Unlock() if a.headers != nil { return *a.headers, nil } @@ -124,10 +124,9 @@ func (a *Account) Headers(ctx context.Context) (AccountHeaders, error) { // Invalidate clears the internal cache of this Account instance. The next call // to Headers() on this instance will issue a HEAD request on the account. -// -// WARNING: This method is not thread-safe. Calling it concurrently on the same -// object results in undefined behavior. func (a *Account) Invalidate() { + a.headersMutex.Lock() + defer a.headersMutex.Unlock() a.headers = nil } -- cgit v1.3.1