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. --- container.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'container.go') diff --git a/container.go b/container.go index 7ddd415..5d03000 100644 --- a/container.go +++ b/container.go @@ -21,6 +21,7 @@ package schwift import ( "context" "net/http" + "sync" ) // Container represents a Swift container. Instances are usually obtained by @@ -30,7 +31,8 @@ type Container struct { a *Account name string // cache - headers *ContainerHeaders + headers *ContainerHeaders + headersMutex sync.Mutex } // IsEqualTo returns true if both Container instances refer to the same container. @@ -75,10 +77,9 @@ func (c *Container) Exists(ctx context.Context) (bool, error) { // has not been cached yet, a HEAD request is issued on the container. // // This operation fails with http.StatusNotFound if the container does not exist. -// -// WARNING: This method is not thread-safe. Calling it concurrently on the same -// object results in undefined behavior. func (c *Container) Headers(ctx context.Context) (ContainerHeaders, error) { + c.headersMutex.Lock() + defer c.headersMutex.Unlock() if c.headers != nil { return *c.headers, nil } @@ -167,10 +168,9 @@ func (c *Container) Delete(ctx context.Context, opts *RequestOptions) error { // Invalidate clears the internal cache of this Container instance. The next call // to Headers() on this instance will issue a HEAD request on the container. -// -// WARNING: This method is not thread-safe. Calling it concurrently on the same -// object results in undefined behavior. func (c *Container) Invalidate() { + c.headersMutex.Lock() + defer c.headersMutex.Unlock() c.headers = nil } -- cgit v1.3.1