Skip to content

fix(networking): Share one URLSession across environments - #547

Open
abelonogov-ld wants to merge 1 commit into
v11from
andrey/shared-url-session
Open

abelonogov-ld wants to merge 1 commit into
v11from
andrey/shared-url-session

Conversation

@abelonogov-ld

@abelonogov-ld abelonogov-ld commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

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 DarklyService created its own URLSession. Every URLSession has its own connection pool, so environments could never reuse each other's connections. Seven environments posting diagnostics and events to mobile.launchdarkly.com therefore 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 headerDelegate are set on each request. So DarklyService now 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: LDSwiftEventSource creates its own session for each stream.

Test: DarklyServiceSpec checks that services for two environments use the same session.

Describe alternatives you've considered

  • Have ClientServiceFactory own the session and pass it to each service. That shares a session only within one LDClient.start, needs a change to the factory protocol and mock, and gains nothing, since the configuration never varies.
  • Hold uploads back until all environments are ready, or batch them across environments. That is a bigger behavior change and doesn't address the per-session connection pools.

Additional context

Before this change the sessions were never invalidated either. A shared, process-lifetime session is the usual URLSession pattern.


Note

Overview
Fixes excess TCP/TLS setup when an app runs multiple LaunchDarkly environments by sharing a single URLSession across all DarklyService instances instead of creating one per environment.

Session configuration (TLS 1.2 minimum, no URL cache, reload-ignoring cache policy) moves from init into a static DarklyService.session; each service still sets per-request headers, timeouts, and delegates on individual URLRequests. 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.

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>
@abelonogov-ld
abelonogov-ld requested a review from a team as a code owner November 4, 2026 23:49

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant