-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: hide data source configuration behind dataSystem interface #189
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package datasystem | ||
|
||
// DataAvailability represents the availability of data in the SDK. | ||
type DataAvailability string | ||
|
||
const ( | ||
// Defaults means the SDK has no data and will evaluate flags using the application-provided default values. | ||
Defaults = DataAvailability("defaults") | ||
// Cached means the SDK has data, not necessarily the latest, which will be used to evaluate flags. | ||
Cached = DataAvailability("cached") | ||
// Refreshed means the SDK has obtained, at least once, the latest known data from LaunchDarkly. | ||
Refreshed = DataAvailability("refreshed") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package datasystem | ||
|
||
import ( | ||
"github.com/launchdarkly/go-server-sdk/v7/interfaces" | ||
"github.com/launchdarkly/go-server-sdk/v7/internal" | ||
"github.com/launchdarkly/go-server-sdk/v7/internal/datasource" | ||
"github.com/launchdarkly/go-server-sdk/v7/internal/datastore" | ||
"github.com/launchdarkly/go-server-sdk/v7/ldcomponents" | ||
"github.com/launchdarkly/go-server-sdk/v7/subsystems" | ||
) | ||
|
||
// FDv1 implements the configuration and interactions between the SDK's data store, data source, and | ||
// other related components. | ||
type FDv1 struct { | ||
dataSourceStatusBroadcaster *internal.Broadcaster[interfaces.DataSourceStatus] | ||
dataSourceStatusProvider interfaces.DataSourceStatusProvider | ||
dataStoreStatusBroadcaster *internal.Broadcaster[interfaces.DataStoreStatus] | ||
dataStoreStatusProvider interfaces.DataStoreStatusProvider | ||
flagChangeEventBroadcaster *internal.Broadcaster[interfaces.FlagChangeEvent] | ||
dataStore subsystems.DataStore | ||
dataSource subsystems.DataSource | ||
offline bool | ||
} | ||
|
||
// NewFDv1 creates a new FDv1 instance from data store and data source configurers. Offline determines if the | ||
// client is in offline mode. If configuration is invalid, an error will be returned. | ||
func NewFDv1(offline bool, dataStoreFactory subsystems.ComponentConfigurer[subsystems.DataStore], | ||
dataSourceFactory subsystems.ComponentConfigurer[subsystems.DataSource], | ||
clientContext *internal.ClientContextImpl) (*FDv1, error) { | ||
system := &FDv1{ | ||
dataSourceStatusBroadcaster: internal.NewBroadcaster[interfaces.DataSourceStatus](), | ||
dataStoreStatusBroadcaster: internal.NewBroadcaster[interfaces.DataStoreStatus](), | ||
flagChangeEventBroadcaster: internal.NewBroadcaster[interfaces.FlagChangeEvent](), | ||
offline: offline, | ||
} | ||
|
||
dataStoreUpdateSink := datastore.NewDataStoreUpdateSinkImpl(system.dataStoreStatusBroadcaster) | ||
storeFactory := dataStoreFactory | ||
if storeFactory == nil { | ||
storeFactory = ldcomponents.InMemoryDataStore() | ||
} | ||
clientContextWithDataStoreUpdateSink := clientContext | ||
clientContextWithDataStoreUpdateSink.DataStoreUpdateSink = dataStoreUpdateSink | ||
store, err := storeFactory.Build(clientContextWithDataStoreUpdateSink) | ||
if err != nil { | ||
return nil, err | ||
} | ||
system.dataStore = store | ||
|
||
system.dataStoreStatusProvider = datastore.NewDataStoreStatusProviderImpl(store, dataStoreUpdateSink) | ||
|
||
dataSourceUpdateSink := datasource.NewDataSourceUpdateSinkImpl( | ||
store, | ||
system.dataStoreStatusProvider, | ||
system.dataSourceStatusBroadcaster, | ||
system.flagChangeEventBroadcaster, | ||
clientContext.GetLogging().LogDataSourceOutageAsErrorAfter, | ||
clientContext.GetLogging().Loggers, | ||
) | ||
|
||
dataSource, err := createDataSource(clientContext, dataSourceFactory, dataSourceUpdateSink) | ||
if err != nil { | ||
return nil, err | ||
} | ||
system.dataSource = dataSource | ||
system.dataSourceStatusProvider = datasource.NewDataSourceStatusProviderImpl( | ||
system.dataSourceStatusBroadcaster, | ||
dataSourceUpdateSink, | ||
) | ||
|
||
return system, nil | ||
} | ||
|
||
func createDataSource( | ||
context *internal.ClientContextImpl, | ||
dataSourceBuilder subsystems.ComponentConfigurer[subsystems.DataSource], | ||
dataSourceUpdateSink subsystems.DataSourceUpdateSink, | ||
) (subsystems.DataSource, error) { | ||
if context.Offline { | ||
context.GetLogging().Loggers.Info("Starting LaunchDarkly client in offline mode") | ||
dataSourceUpdateSink.UpdateStatus(interfaces.DataSourceStateValid, interfaces.DataSourceErrorInfo{}) | ||
return datasource.NewNullDataSource(), nil | ||
} | ||
factory := dataSourceBuilder | ||
if factory == nil { | ||
// COVERAGE: can't cause this condition in unit tests because it would try to connect to production LD | ||
factory = ldcomponents.StreamingDataSource() | ||
} | ||
contextCopy := *context | ||
contextCopy.BasicClientContext.DataSourceUpdateSink = dataSourceUpdateSink | ||
return factory.Build(&contextCopy) | ||
} | ||
|
||
//nolint:revive // Data system implementation. | ||
func (f *FDv1) DataSourceStatusBroadcaster() *internal.Broadcaster[interfaces.DataSourceStatus] { | ||
return f.dataSourceStatusBroadcaster | ||
} | ||
|
||
//nolint:revive // Data system implementation. | ||
func (f *FDv1) DataSourceStatusProvider() interfaces.DataSourceStatusProvider { | ||
return f.dataSourceStatusProvider | ||
} | ||
|
||
//nolint:revive // Data system implementation. | ||
func (f *FDv1) DataStoreStatusBroadcaster() *internal.Broadcaster[interfaces.DataStoreStatus] { | ||
return f.dataStoreStatusBroadcaster | ||
} | ||
|
||
//nolint:revive // Data system implementation. | ||
func (f *FDv1) DataStoreStatusProvider() interfaces.DataStoreStatusProvider { | ||
return f.dataStoreStatusProvider | ||
} | ||
|
||
//nolint:revive // Data system implementation. | ||
func (f *FDv1) FlagChangeEventBroadcaster() *internal.Broadcaster[interfaces.FlagChangeEvent] { | ||
return f.flagChangeEventBroadcaster | ||
} | ||
|
||
//nolint:revive // Data system implementation. | ||
func (f *FDv1) Start(closeWhenReady chan struct{}) { | ||
f.dataSource.Start(closeWhenReady) | ||
} | ||
|
||
//nolint:revive // Data system implementation. | ||
func (f *FDv1) Stop() error { | ||
if f.dataSource != nil { | ||
_ = f.dataSource.Close() | ||
} | ||
if f.dataStore != nil { | ||
_ = f.dataStore.Close() | ||
} | ||
if f.dataSourceStatusBroadcaster != nil { | ||
f.dataSourceStatusBroadcaster.Close() | ||
} | ||
if f.dataStoreStatusBroadcaster != nil { | ||
f.dataStoreStatusBroadcaster.Close() | ||
} | ||
if f.flagChangeEventBroadcaster != nil { | ||
f.flagChangeEventBroadcaster.Close() | ||
} | ||
return nil | ||
} | ||
|
||
//nolint:revive // Data system implementation. | ||
func (f *FDv1) DataAvailability() DataAvailability { | ||
if f.offline { | ||
return Defaults | ||
} | ||
if f.dataSource.IsInitialized() { | ||
return Refreshed | ||
} | ||
if f.dataStore.IsInitialized() { | ||
return Cached | ||
} | ||
return Defaults | ||
} | ||
|
||
//nolint:revive // Data system implementation. | ||
func (f *FDv1) Store() subsystems.ReadOnlyStore { | ||
return f.dataStore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Package datasystem encapsulates the interactions between the SDK's data store, data source, and other related | ||
// components. | ||
// Currently, there is only one data system implementation, FDv1, which represents the functionality of the SDK | ||
// before the FDv2 protocol was introduced. | ||
package datasystem |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is because the existing read/write enabled
DataStore
interface isn't actually needed in the evaluator. It only needs to be able to retrieve segments/flags.