# Creating many “clients” vs wrapping a “client” in an actor

**URL:** <https://forums.swift.org/t/creating-many-clients-vs-wrapping-a-client-in-an-actor/67380>\
**Category:** SwiftNIO\
**Created:** [September 19, 2023, 11:11pm UTC](https://forums.swift.org/t/creating-many-clients-vs-wrapping-a-client-in-an-actor/67380 "2023-09-19T23:11:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [September 19, 2023, 11:11pm UTC](https://forums.swift.org/t/creating-many-clients-vs-wrapping-a-client-in-an-actor/67380/1 "2023-09-19T23:11:04Z")

</div>

i have a server that has some integrations with third-party APIs, and accordingly, has a few “client” singletons that it uses to interact with them. the “clients” have varying shapes, but the thing they all have in common is they all wrap a NIO [`ClientBootstrap`](https://swiftinit.org/docs/swift-nio/nioposix/clientbootstrap).

```swift
struct ParticularClient
{
    let bootstrap:ClientBootstrap

    ...
}

```

this, of course, means `ParticularClient` cannot be `Sendable`, because [`ClientBootstrap` is not `Sendable`](https://forums.swift.org/t/why-isn-t-clientbootstrap-sendable/62577).

that’s okay if you only need to use the client from one concurrency domain, for example, iterating an `AsyncSequence` of requests:

```swift
mutating
func respond(to requests:AsyncStream<Server.DisplayRequest>) async throws
{
    // create clients here
    for await request:Server.DisplayRequest in requests
    {
        try Task.checkCancellation()
        // can use clients here
    }
}

```

but if you want to say, perform some long-running operation in the background that also uses the client, you’re out of luck because the client is a singleton and cannot be shared across concurrency domains.

there are two possible directions i can think of here:

1. make the clients `actors`, which will make them share-able again. but using actor isolation just to synchronize access to a `ClientBootstrap` feels like overkill. after all, why must accessing a `ClientBootstrap` require synchronization in the first place?

2. make separate, dedicated clients for each long-running operation, which implies creating [multiple `ClientBootstraps`](https://forums.swift.org/t/re-using-clientbootstrap-but-why/67080/2) to connect to the same API.

which approach is better?

---

<div class="post-metadata">

**Author:** ![sliemeobn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sliemeobn/32/27698_2.png) [@sliemeobn](https://forums.swift.org/u/sliemeobn)\
**Post date:** [September 20, 2023, 7:31am UTC](https://forums.swift.org/t/creating-many-clients-vs-wrapping-a-client-in-an-actor/67380/2 "2023-09-20T07:31:14Z")

</div>

I just stumbled over the very same annoyance of the `ClientBootrap` not being sendable - including the same thought process about using an actor being overkill.

For now I just spin up new (ephemeral) bootstrap on connect and store the resulting channel, which in my case is no issue at all because the intended usage is a long-lasting connection. I also believe that creating a bootstrap is not exactly a "heavy" operation, so it's probably rarely worth adding a ton of threadsafety complexity around it.

I'm not quite sure I understand what you mean by "separate, dedicated clients" in this context, because im my mind this has nothing to do with bootstraps but rather channels.

---

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [September 20, 2023, 7:46am UTC](https://forums.swift.org/t/creating-many-clients-vs-wrapping-a-client-in-an-actor/67380/3 "2023-09-20T07:46:40Z")

</div>

> [@taylorswift](#):
>
> after all, why must accessing a `ClientBootstrap` require synchronization in the first place?

Because they aren't `Sendable`. They're a giant pile of mutable state without synchronzation: you really, genuinely cannot use them in multiple concurrency domains. These not being `Sendable` is not an oversight, it's an intentional design decision.

> [@sliemeobn](#):
>
> I also believe that creating a bootstrap is not exactly a "heavy" operation, so it's probably rarely worth adding a ton of threadsafety complexity around it.

This is accurate: `ClientBootstraps` are cheap, feel free to create plenty.

We've got tentative plans to deprecate-and-replace the current bootstraps, because they aren't really serving their purpose well anymore. In particular the builder pattern has aged poorly. The next versions will likely be value types, which makes this entire problem go away.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [September 20, 2023, 3:43pm UTC](https://forums.swift.org/t/creating-many-clients-vs-wrapping-a-client-in-an-actor/67380/4 "2023-09-20T15:43:05Z")

</div>

> [@lukasa](#):
>
> This is accurate: `ClientBootstraps` are cheap, feel free to create plenty.

i see, i was creating [`NIOSSLContext`](https://swiftinit.org/docs/swift-nio-ssl/niossl/niosslcontext) in the same function, as i was conceptualizing SSL context creation and bootstrap creation as “expensive, do-once” tasks to be grouped together. but if `ClientBootstrap`s are cheap to make, i might as well separate the SSL context creation from the bootstrap creation.

the [documentation](https://swiftinit.org/docs/swift-nio/nioposix/clientbootstrap) really emphasizes the importance of creating `ClientBootstrap` once and reusing it, i think this advice is unhelpful and caused me to spend a lot of time optimizing something that didn’t need to be optimized.

> [@sliemeobn](#):
>
> I'm not quite sure I understand what you mean by "separate, dedicated clients" in this context, because im my mind this has nothing to do with bootstraps but rather channels.

in this context, a “client” is just a type that wraps some value parameters (API key, account ID, etc.) and a `ClientBootstrap`. a “client” can be reused multiple times to connect to the same server.

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [September 21, 2023, 7:52pm UTC](https://forums.swift.org/t/creating-many-clients-vs-wrapping-a-client-in-an-actor/67380/5 "2023-09-21T19:52:23Z")

</div>

> [@taylorswift](#):
>
> the [documentation](https://swiftinit.org/docs/swift-nio/nioposix/clientbootstrap) really emphasizes the importance of creating `ClientBootstrap` once

I says that you "usually" re-use them. I don't think that can be read as 'really emphasising the importance of creating them once'. In any case: Let's fix the docs! We can just change the sentence to "You may re-use" instead of 'usually'.

WDYT? [docs: ClientBootstrap reuse is not important but permissible on the same thread/EL by weissi · Pull Request #2520 · apple/swift-nio · GitHub](https://github.com/apple/swift-nio/pull/2520)

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [September 21, 2023, 8:13pm UTC](https://forums.swift.org/t/creating-many-clients-vs-wrapping-a-client-in-an-actor/67380/6 "2023-09-21T20:13:48Z")

</div>

looks good! left a couple comments regarding grammar as well 🙂
