/
helpers_load_balancer.go
42 lines (32 loc) · 1.04 KB
/
helpers_load_balancer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
package provider
import (
"fmt"
"sync"
)
var lbBackendSetMutexes SafeMutexMap
type SafeMutexMap struct {
mutexes map[string]*sync.Mutex
m sync.Mutex // Controls access to this map
}
// Given a load balancer ID and backend set name, finds a mutex. If a mutex doesn't exist, then create one for that backend set.
//
// We can add more generic ways of accessing this map in the future; if other resources need to use this pattern for
// resolving concurrent resource access issues. For now, keep it specific to backend sets
func (safeMap *SafeMutexMap) GetOrCreateBackendSetMutex(lbId string, backendSetName string) *sync.Mutex {
if lbId == "" || backendSetName == "" {
return nil
}
safeMap.m.Lock()
defer safeMap.m.Unlock()
key := fmt.Sprintf("%s.%s", lbId, backendSetName)
if safeMap.mutexes == nil {
safeMap.mutexes = map[string]*sync.Mutex{}
}
m, exists := safeMap.mutexes[key]
if !exists {
m = &sync.Mutex{}
safeMap.mutexes[key] = m
}
return m
}