diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-07-21 14:18:35 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-07-21 14:18:35 +0200 |
| commit | dc7bd11d855f0b3266612a18606801f4e09ecc09 (patch) | |
| tree | 6d3d483601abc00402f7be06d4d630238128ea2b /container.go | |
| parent | 8b2ace1c7cb82e1af923b8fc5fa7ffba820b9238 (diff) | |
| download | go-schwift-dc7bd11d855f0b3266612a18606801f4e09ecc09.tar.gz | |
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.
Diffstat (limited to 'container.go')
| -rw-r--r-- | container.go | 14 |
1 files changed, 7 insertions, 7 deletions
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 } |
