Repository navigation
fix(networking): Share one URLSession across environments - #547
Open
abelonogov-ld wants to merge 1 commit into
Open
abelonogov-ld wants to merge 1 commit into
abelonogov-ld wants to merge 1 commit into
Conversation
Each environment's DarklyService built its own URLSession, and each session keeps its own connection pool, so seven environments posting events and diagnostics to the same host opened at least seven TCP+TLS connections. The session configuration does not depend on the environment, so every service now uses one shared session and their requests reuse connections. Streams are unaffected: they use LDSwiftEventSource's own sessions. Co-authored-by: Cursor <cursoragent@cursor.com>
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Requirements
Related issues
Customer report: at cold boot, an app with seven environments opened 19 LaunchDarkly sockets. Seven were the expected flag streams. The other 12 carried 14 small POSTs to the events host that returned no body, at 349 ms p50 / 1,230 ms p90, which is mostly connection setup.
Describe the solution you've provided
Each environment's
DarklyServicecreated its ownURLSession. EveryURLSessionhas its own connection pool, so environments could never reuse each other's connections. Seven environments posting diagnostics and events tomobile.launchdarkly.comtherefore needed at least seven TCP+TLS handshakes, even though that host negotiates HTTP/2 and one connection could carry all of them.The session's configuration doesn't depend on the environment: minimum TLS 1.2 and no URL cache. Headers, the timeout and
headerDelegateare set on each request. SoDarklyServicenow uses a single shared session,DarklyService.session, and requests to the same host reuse connections across environments. The same applies to flag requests in polling mode.Flag streams are unaffected:
LDSwiftEventSourcecreates its own session for each stream.Test:
DarklyServiceSpecchecks that services for two environments use the same session.Describe alternatives you've considered
ClientServiceFactoryown the session and pass it to each service. That shares a session only within oneLDClient.start, needs a change to the factory protocol and mock, and gains nothing, since the configuration never varies.Additional context
Before this change the sessions were never invalidated either. A shared, process-lifetime session is the usual
URLSessionpattern.Note
Overview
Fixes excess TCP/TLS setup when an app runs multiple LaunchDarkly environments by sharing a single
URLSessionacross allDarklyServiceinstances instead of creating one per environment.Session configuration (TLS 1.2 minimum, no URL cache, reload-ignoring cache policy) moves from
initinto a staticDarklyService.session; each service still sets per-request headers, timeouts, and delegates on individualURLRequests. Flag polling, event bulk posts, and diagnostic posts to the same host can reuse HTTP/2 connections across environments. Streaming is unchanged—event sources still use their own sessions.Adds a spec asserting two services with different mobile keys use the same session object (
===).Reviewed by Cursor Bugbot for commit e03f090. Bugbot is set up for automated code reviews on this repo. Configure here.