...

Source file src/github.com/launchdarkly/go-server-sdk/v6/ldcomponents/streaming_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  // DefaultStreamingBaseURI is the default value for [StreamingDataSourceBuilder.BaseURI].
    13  const DefaultStreamingBaseURI = endpoints.DefaultStreamingBaseURI
    14  
    15  // DefaultInitialReconnectDelay is the default value for [StreamingDataSourceBuilder.InitialReconnectDelay].
    16  const DefaultInitialReconnectDelay = time.Second
    17  
    18  // StreamingDataSourceBuilder provides methods for configuring the streaming data source.
    19  //
    20  // See StreamingDataSource for usage.
    21  type StreamingDataSourceBuilder struct {
    22  	baseURI               string
    23  	initialReconnectDelay time.Duration
    24  }
    25  
    26  // StreamingDataSource returns a configurable factory for using streaming mode to get feature flag data.
    27  //
    28  // By default, the SDK uses a streaming connection to receive feature flag data from LaunchDarkly. To use the
    29  // default behavior, you do not need to call this method. However, if you want to customize the behavior of
    30  // the connection, call this method to obtain a builder, set its properties with the [StreamingDataSourceBuilder]
    31  // methods, and then store it in the DataSource field of [github.com/launchdarkly/go-server-sdk/v6.Config]:
    32  //
    33  //	config := ld.Config{
    34  //	    DataSource: ldcomponents.StreamingDataSource().InitialReconnectDelay(500 * time.Millisecond),
    35  //	}
    36  func StreamingDataSource() *StreamingDataSourceBuilder {
    37  	return &StreamingDataSourceBuilder{
    38  		initialReconnectDelay: DefaultInitialReconnectDelay,
    39  	}
    40  }
    41  
    42  // InitialReconnectDelay sets the initial reconnect delay for the streaming connection.
    43  //
    44  // The streaming service uses a backoff algorithm (with jitter) every time the connection needs to be
    45  // reestablished. The delay for the first reconnection will start near this value, and then increase
    46  // exponentially for any subsequent connection failures.
    47  //
    48  // The default value is [DefaultInitialReconnectDelay].
    49  func (b *StreamingDataSourceBuilder) InitialReconnectDelay(
    50  	initialReconnectDelay time.Duration,
    51  ) *StreamingDataSourceBuilder {
    52  	if initialReconnectDelay <= 0 {
    53  		b.initialReconnectDelay = DefaultInitialReconnectDelay
    54  	} else {
    55  		b.initialReconnectDelay = initialReconnectDelay
    56  	}
    57  	return b
    58  }
    59  
    60  // Build is called internally by the SDK.
    61  func (b *StreamingDataSourceBuilder) Build(context subsystems.ClientContext) (subsystems.DataSource, error) {
    62  	configuredBaseURI := endpoints.SelectBaseURI(
    63  		context.GetServiceEndpoints(),
    64  		endpoints.StreamingService,
    65  		b.baseURI,
    66  		context.GetLogging().Loggers,
    67  	)
    68  
    69  	return datasource.NewStreamProcessor(
    70  		context,
    71  		context.GetDataSourceUpdateSink(),
    72  		configuredBaseURI,
    73  		b.initialReconnectDelay,
    74  	), nil
    75  }
    76  
    77  // DescribeConfiguration is used internally by the SDK to inspect the configuration.
    78  func (b *StreamingDataSourceBuilder) DescribeConfiguration(context subsystems.ClientContext) ldvalue.Value {
    79  	return ldvalue.ObjectBuild().
    80  		SetBool("streamingDisabled", false).
    81  		SetBool("customStreamURI",
    82  			endpoints.IsCustom(context.GetServiceEndpoints(), endpoints.StreamingService, b.baseURI)).
    83  		Set("reconnectTimeMillis", durationToMillisValue(b.initialReconnectDelay)).
    84  		SetBool("usingRelayDaemon", false).
    85  		Build()
    86  }
    87  

View as plain text