Skip to content

Commit f517e61

Browse files
committed
Option to delay fetching bulk data between batches of new clients
1 parent 52ff6a5 commit f517e61

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

docs/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ For **Duration** settings, the value should be be an integer followed by `ms`, `
6565
| `logLevel` | `LOG_LEVEL` | String | `info` | Should be `debug`, `info`, `warn`, `error`, or `none`. To learn more, read [Logging](./logging.md). |
6666
| `bigSegmentsStaleAsDegraded` | `BIG_SEGMENTS_STALE_AS_DEGRADED` | Boolean | `false` | Indicates if environments should be considered degraded if Big Segments are not fully synchronized. |
6767
| `bigSegmentsStaleThreshold` | `BIG_SEGMENTS_STALE_THRESHOLD` | Duration | `5m` | Indicates how long until Big Segments should be considered stale. |
68-
| n/a | `STREAMING_MIN_DELAY` | Duration | `0` | The minimum latency of responding to a new client connection. Used only in proxy mode for streaming clients. Useful for reducing memory when under heavy load, as many clients can share a single data fetch. |
68+
| n/a | `BATCH_FETCH_PERIOD` | Duration | `0` | The minimum latency between bulk fetching all data for a batch of new clients. Used only in proxy mode for streaming clients. Useful for reducing memory when under heavy load, as many clients can share a single data fetch. |
6969

7070
_(1)_ The default values for `streamUri`, `baseUri`, and `clientSideBaseUri` are `https://stream.launchdarkly.com`, `https://sdk.launchdarkly.com`, and `https://clientsdk.launchdarkly.com`, respectively. You should never need to change these URIs unless you are either using a special instance of the LaunchDarkly service, in which case Support will tell you how to set them, or you are accessing LaunchDarkly using a reverse proxy or some other mechanism that rewrites URLs.
7171

internal/streams/stream_provider_server_side.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ type serverSideEnvStreamRepository struct {
3333
store EnvStoreQueries
3434
loggers ldlog.Loggers
3535

36-
flightGroup singleflight.Group
36+
flightGroup singleflight.Group
37+
previousFlight time.Time
3738
}
3839

3940
func (s *serverSideStreamProvider) Handler(credential sdkauth.ScopedCredential) http.HandlerFunc {
@@ -111,7 +112,20 @@ func (r *serverSideEnvStreamRepository) Replay(channel, id string) chan eventsou
111112
// getReplayEvent will return a ServerSidePutEvent with all the data needed for a Replay.
112113
func (r *serverSideEnvStreamRepository) getReplayEvent() (eventsource.Event, error) {
113114
data, err, _ := r.flightGroup.Do("getReplayEvent", func() (interface{}, error) {
114-
start := time.Now()
115+
// We do not want to call this flight group too often, as it can use a lot of RAM.
116+
// This will ensure that we don't call it more than once every BATCH_FETCH_PERIOD.
117+
delayS, has := os.LookupEnv("BATCH_FETCH_PERIOD")
118+
if has {
119+
if delay, err := time.ParseDuration(delayS); err == nil {
120+
if time.Since(r.previousFlight) < delay {
121+
time.Sleep(delay - time.Since(r.previousFlight))
122+
}
123+
} else {
124+
r.loggers.Warnf("Ignoring invalid BATCH_FETCH_PERIOD: %s\n", delayS)
125+
}
126+
r.previousFlight = time.Now()
127+
}
128+
115129
flags, err := r.store.GetAll(ldstoreimpl.Features())
116130

117131
if err != nil {
@@ -131,17 +145,6 @@ func (r *serverSideEnvStreamRepository) getReplayEvent() (eventsource.Event, err
131145

132146
// This call uses a lot of system resources (RAM in particular).
133147
event := MakeServerSidePutEvent(allData)
134-
// So we sleep for a bit to allow a bunch of concurrent calls to
135-
// all make use of this same flightGroup.
136-
delayS, has := os.LookupEnv("STREAMING_MIN_DELAY")
137-
if has {
138-
if delay, err := time.ParseDuration(delayS); err == nil {
139-
time.Sleep(delay - time.Since(start))
140-
} else {
141-
r.loggers.Warnf("Ignoring invalid STREAMING_MIN_DELAY: %s\n", delayS)
142-
}
143-
}
144-
145148
return event, nil
146149
})
147150

0 commit comments

Comments
 (0)