...

Source file src/github.com/launchdarkly/go-server-sdk/v6/ldcomponents/polling_data_source_builder.go

Documentation: github.com/launchdarkly/go-server-sdk/v6/ldcomponents

     1  package ldcomponents
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
     7  	"github.com/launchdarkly/go-server-sdk/v6/internal/datasource"
     8  	"github.com/launchdarkly/go-server-sdk/v6/internal/endpoints"
     9  	"github.com/launchdarkly/go-server-sdk/v6/subsystems"
    10  )
    11  
    12  // DefaultPollingBaseURI is the default value for [PollingDataSourceBuilder.BaseURI].
    13  const DefaultPollingBaseURI = "https://app.launchdarkly.com"
    14  
    15  // DefaultPollInterval is the default value for [PollingDataSourceBuilder.PollInterval]. This is also the minimum value.
    16  const DefaultPollInterval = 30 * time.Second
    17  
    18  // PollingDataSourceBuilder provides methods for configuring the polling data source.
    19  //
    20  // See [PollingDataSource] for usage.
    21  type PollingDataSourceBuilder struct {
    22  	baseURI      string
    23  	pollInterval time.Duration
    24  }
    25  
    26  // PollingDataSource returns a configurable factory for using polling mode to get feature flag data.
    27  //
    28  // Polling is not the default behavior; by default, the SDK uses a streaming connection to receive feature flag
    29  // data from LaunchDarkly. In polling mode, the SDK instead makes a new HTTP request to LaunchDarkly at regular
    30  // intervals. HTTP caching allows it to avoid redundantly downloading data if there have been no changes, but
    31  // polling is still less efficient than streaming and should only be used on the advice of LaunchDarkly support.
    32  //
    33  // To use polling mode, create a builder with PollingDataSource(), set its properties with the methods of
    34  // [PollingDataSourceBuilder], and then store it in the DataSource field of
    35  // [github.com/launchdarkly/go-server-sdk/v6.Config]:
    36  //
    37  //	config := ld.Config{
    38  //	    DataSource: ldcomponents.PollingDataSource().PollInterval(45 * time.Second),
    39  //	}
    40  func PollingDataSource() *PollingDataSourceBuilder {
    41  	return &PollingDataSourceBuilder{
    42  		pollInterval: DefaultPollInterval,
    43  	}
    44  }
    45  
    46  // PollInterval sets the interval at which the SDK will poll for feature flag updates.
    47  //
    48  // The default and minimum value is [DefaultPollInterval]. Values less than this will be set to the default.
    49  func (b *PollingDataSourceBuilder) PollInterval(pollInterval time.Duration) *PollingDataSourceBuilder {
    50  	if pollInterval < DefaultPollInterval {
    51  		b.pollInterval = DefaultPollInterval
    52  	} else {
    53  		b.pollInterval = pollInterval
    54  	}
    55  	return b
    56  }
    57  
    58  // Used in tests to skip parameter validation.
    59  //
    60  //nolint:unused // it is used in tests
    61  func (b *PollingDataSourceBuilder) forcePollInterval(
    62  	pollInterval time.Duration,
    63  ) *PollingDataSourceBuilder {
    64  	b.pollInterval = pollInterval
    65  	return b
    66  }
    67  
    68  // Build is called internally by the SDK.
    69  func (b *PollingDataSourceBuilder) Build(context subsystems.ClientContext) (subsystems.DataSource, error) {
    70  	context.GetLogging().Loggers.Warn(
    71  		"You should only disable the streaming API if instructed to do so by LaunchDarkly support")
    72  	configuredBaseURI := endpoints.SelectBaseURI(
    73  		context.GetServiceEndpoints(),
    74  		endpoints.PollingService,
    75  		b.baseURI,
    76  		context.GetLogging().Loggers,
    77  	)
    78  	pp := datasource.NewPollingProcessor(context, context.GetDataSourceUpdateSink(), configuredBaseURI, b.pollInterval)
    79  	return pp, nil
    80  }
    81  
    82  // DescribeConfiguration is used internally by the SDK to inspect the configuration.
    83  func (b *PollingDataSourceBuilder) DescribeConfiguration(context subsystems.ClientContext) ldvalue.Value {
    84  	return ldvalue.ObjectBuild().
    85  		SetBool("streamingDisabled", true).
    86  		SetBool("customBaseURI",
    87  			endpoints.IsCustom(context.GetServiceEndpoints(), endpoints.PollingService, b.baseURI)).
    88  		Set("pollingIntervalMillis", durationToMillisValue(b.pollInterval)).
    89  		SetBool("usingRelayDaemon", false).
    90  		Build()
    91  }
    92  

View as plain text