-
-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make region-client map concurrency safe using custom structure …
…with a mutex
- Loading branch information
Showing
8 changed files
with
106 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package clientmap | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
"sync" | ||
) | ||
|
||
type ClientMap struct { | ||
sync.Mutex | ||
inner map[string]*s3.Client | ||
} | ||
|
||
func New() *ClientMap { | ||
return &ClientMap{ | ||
Mutex: sync.Mutex{}, | ||
inner: make(map[string]*s3.Client), | ||
} | ||
} | ||
|
||
func WithCapacity(cap int) *ClientMap { | ||
return &ClientMap{ | ||
Mutex: sync.Mutex{}, | ||
inner: make(map[string]*s3.Client, cap), | ||
} | ||
} | ||
|
||
func (m *ClientMap) Get(key string) *s3.Client { | ||
m.Lock() | ||
defer m.Unlock() | ||
if v, ok := m.inner[key]; ok { | ||
return v | ||
} | ||
return nil | ||
} | ||
|
||
func (m *ClientMap) Set(key string, value *s3.Client) { | ||
m.Lock() | ||
m.inner[key] = value | ||
m.Unlock() | ||
} | ||
|
||
func (m *ClientMap) Len() int { | ||
m.Lock() | ||
defer m.Unlock() | ||
return len(m.inner) | ||
} | ||
|
||
func (m *ClientMap) Each(fn func(region string, client *s3.Client)) { | ||
m.Lock() | ||
for region, client := range m.inner { | ||
fn(region, client) | ||
} | ||
m.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters