go-zero/core/syncx/managedresource.go

49 lines
1.0 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package syncx
import "sync"
2021-02-28 16:16:22 +08:00
// A ManagedResource is used to manage a resource that might be broken and refetched, like a connection.
2020-07-26 17:09:05 +08:00
type ManagedResource struct {
resource any
2020-07-26 17:09:05 +08:00
lock sync.RWMutex
generate func() any
equals func(a, b any) bool
2020-07-26 17:09:05 +08:00
}
2021-02-28 16:16:22 +08:00
// NewManagedResource returns a ManagedResource.
func NewManagedResource(generate func() any, equals func(a, b any) bool) *ManagedResource {
2020-07-26 17:09:05 +08:00
return &ManagedResource{
generate: generate,
equals: equals,
}
}
2021-05-10 00:09:00 +08:00
// MarkBroken marks the resource broken.
func (mr *ManagedResource) MarkBroken(resource any) {
2020-07-26 17:09:05 +08:00
mr.lock.Lock()
defer mr.lock.Unlock()
if mr.equals(mr.resource, resource) {
mr.resource = nil
}
}
2021-02-28 16:16:22 +08:00
// Take takes the resource, if not loaded, generates it.
func (mr *ManagedResource) Take() any {
2020-07-26 17:09:05 +08:00
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
}