/
data_store_update_sink_impl.go
51 lines (44 loc) · 1.46 KB
/
data_store_update_sink_impl.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
49
50
51
package datastore
import (
"sync"
"github.com/launchdarkly/go-server-sdk/v7/interfaces"
"github.com/launchdarkly/go-server-sdk/v7/internal"
)
// DataStoreUpdateSinkImpl is the internal implementation of DataStoreUpdateSink. It is exported
// because the actual implementation type, rather than the interface, is required as a dependency
// of other SDK components.
type DataStoreUpdateSinkImpl struct {
lastStatus interfaces.DataStoreStatus
broadcaster *internal.Broadcaster[interfaces.DataStoreStatus]
lock sync.Mutex
}
// NewDataStoreUpdateSinkImpl creates the internal implementation of DataStoreUpdateSink.
func NewDataStoreUpdateSinkImpl(
broadcaster *internal.Broadcaster[interfaces.DataStoreStatus],
) *DataStoreUpdateSinkImpl {
return &DataStoreUpdateSinkImpl{
lastStatus: interfaces.DataStoreStatus{Available: true},
broadcaster: broadcaster,
}
}
func (d *DataStoreUpdateSinkImpl) getStatus() interfaces.DataStoreStatus {
d.lock.Lock()
defer d.lock.Unlock()
return d.lastStatus
}
func (d *DataStoreUpdateSinkImpl) getBroadcaster() *internal.Broadcaster[interfaces.DataStoreStatus] {
return d.broadcaster
}
// UpdateStatus is called from the data store to push a status update.
func (d *DataStoreUpdateSinkImpl) UpdateStatus(newStatus interfaces.DataStoreStatus) {
d.lock.Lock()
modified := false
if newStatus != d.lastStatus {
d.lastStatus = newStatus
modified = true
}
d.lock.Unlock()
if modified {
d.broadcaster.Broadcast(newStatus)
}
}