Skip to content

Commit d22388d

Browse files
committed
have flightGroups take some time to allow reuse
1 parent 21866d0 commit d22388d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

internal/streams/stream_provider_server_side.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package streams
22

33
import (
44
"net/http"
5+
"os"
6+
"strconv"
57
"sync"
8+
"time"
69

710
"github.com/launchdarkly/ld-relay/v8/internal/sdkauth"
811

@@ -109,6 +112,7 @@ func (r *serverSideEnvStreamRepository) Replay(channel, id string) chan eventsou
109112
// getReplayEvent will return a ServerSidePutEvent with all the data needed for a Replay.
110113
func (r *serverSideEnvStreamRepository) getReplayEvent() (eventsource.Event, error) {
111114
data, err, _ := r.flightGroup.Do("getReplayEvent", func() (interface{}, error) {
115+
start := time.Now()
112116
flags, err := r.store.GetAll(ldstoreimpl.Features())
113117

114118
if err != nil {
@@ -126,7 +130,19 @@ func (r *serverSideEnvStreamRepository) getReplayEvent() (eventsource.Event, err
126130
{Kind: ldstoreimpl.Segments(), Items: removeDeleted(segments)},
127131
}
128132

133+
// This call uses a lot of system resources (RAM in particular).
129134
event := MakeServerSidePutEvent(allData)
135+
// So we sleep for a bit to allow a bunch of concurrent calls to
136+
// all make use of this same flightGroup.
137+
delayS := os.Getenv("LD_STREAMING_DELAY_SECONDS")
138+
if delay, err := strconv.Atoi(delayS); err == nil {
139+
if delay > 0 && delay <= 60 {
140+
time.Sleep(time(delay)*time.Second - time.Since(start))
141+
} else {
142+
r.loggers.Warnf("Ignoring invalid value for LD_STREAMING_DELAY_SECONDS: %s", delayS)
143+
}
144+
}
145+
130146
return event, nil
131147
})
132148

0 commit comments

Comments
 (0)