forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
managedresource.go
48 lines (40 loc) · 1.09 KB
/
managedresource.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
43
44
45
46
47
48
package syncx
import "sync"
// A ManagedResource is used to manage a resource that might be broken and refetched, like a connection.
type ManagedResource struct {
resource interface{}
lock sync.RWMutex
generate func() interface{}
equals func(a, b interface{}) bool
}
// NewManagedResource returns a ManagedResource.
func NewManagedResource(generate func() interface{}, equals func(a, b interface{}) bool) *ManagedResource {
return &ManagedResource{
generate: generate,
equals: equals,
}
}
// MarkBroken marks the resource broken.
func (mr *ManagedResource) MarkBroken(resource interface{}) {
mr.lock.Lock()
defer mr.lock.Unlock()
if mr.equals(mr.resource, resource) {
mr.resource = nil
}
}
// Take takes the resource, if not loaded, generates it.
func (mr *ManagedResource) Take() interface{} {
mr.lock.RLock()
resource := mr.resource
mr.lock.RUnlock()
if resource != nil {
return resource
}
mr.lock.Lock()
defer mr.lock.Unlock()
// maybe another Take() call already generated the resource.
if mr.resource == nil {
mr.resource = mr.generate()
}
return mr.resource
}