mirror of
https://github.com/temporalio/temporal.git
synced 2026-08-31 11:01:53 -07:00
## What changed? Pull out parts of FileBasedConfig to make them reusable. ## Why? Make it easier to build other Clients. ## How did you test it? - [x] built - [x] covered by existing tests
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package dynamicconfig
|
|
|
|
import (
|
|
"sync"
|
|
|
|
expmaps "golang.org/x/exp/maps"
|
|
)
|
|
|
|
type (
|
|
// NotifyingClientImpl implements NotifyingClient and is intended to be embedded in another struct.
|
|
// NotifyingClientImpl must not be copied after first use.
|
|
NotifyingClientImpl struct {
|
|
subscriptionLock sync.Mutex
|
|
subscriptionIdx int
|
|
subscriptions map[int]ClientUpdateFunc
|
|
}
|
|
)
|
|
|
|
var _ NotifyingClient = (*NotifyingClientImpl)(nil)
|
|
|
|
func NewNotifyingClientImpl() NotifyingClientImpl {
|
|
return NotifyingClientImpl{subscriptions: make(map[int]ClientUpdateFunc)}
|
|
}
|
|
|
|
// Subscribe adds a subscription to all updates from this Client.
|
|
func (n *NotifyingClientImpl) Subscribe(f ClientUpdateFunc) (cancel func()) {
|
|
n.subscriptionLock.Lock()
|
|
defer n.subscriptionLock.Unlock()
|
|
|
|
n.subscriptionIdx++
|
|
id := n.subscriptionIdx
|
|
n.subscriptions[id] = f
|
|
|
|
return func() {
|
|
n.subscriptionLock.Lock()
|
|
defer n.subscriptionLock.Unlock()
|
|
delete(n.subscriptions, id)
|
|
}
|
|
}
|
|
|
|
// PublishUpdates calls all subscribed update functions with the changed keys.
|
|
func (n *NotifyingClientImpl) PublishUpdates(changed map[Key][]ConstrainedValue) {
|
|
if len(changed) == 0 {
|
|
return
|
|
}
|
|
|
|
n.subscriptionLock.Lock()
|
|
subscriptions := expmaps.Values(n.subscriptions)
|
|
n.subscriptionLock.Unlock()
|
|
|
|
for _, update := range subscriptions {
|
|
update(changed)
|
|
}
|
|
}
|