go-zero/core/syncx/managedresource.go

49 lines
1.1 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 interface{}
lock sync.RWMutex
generate func() interface{}
equals func(a, b interface{}) bool
}
2021-02-28 16:16:22 +08:00
// NewManagedResource returns a ManagedResource.
2020-07-26 17:09:05 +08:00
func NewManagedResource(generate func() interface{}, equals func(a, b interface{}) bool) *ManagedResource {
return &ManagedResource{
generate: generate,
equals: equals,
}
}
2021-02-28 16:16:22 +08:00
// MarkBroken marks the resouce broken.
2020-07-26 17:09:05 +08:00
func (mr *ManagedResource) MarkBroken(resource interface{}) {
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.
2020-07-26 17:09:05 +08:00
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
}