dylanshine / openai-kit

A community Swift package used to interact with the OpenAI API
https://platform.openai.com/docs/api-reference
MIT License
692 stars 107 forks source link

Revert injection of HTTPClient #33

Closed dylanshine closed 1 year ago

dylanshine commented 1 year ago

After discussing with members of the Swift Server Workgroup (https://www.swift.org/sswg/ ), I've come to the conclusion that we'll need to revert the Client initializer back to injecting the HTTPClient.

Their recommendations are as follows:

  1. Normally users should have one HTTPClient per app. HTTPClients should be long-lived since they pool the connections for you.
  2. Calling a sync shutdown in a deinit may block the concurrent executor, which can lead to unexpected hangs. Especially if you use your client from an async/await context.

Generally, we would advise creating a single HTTPClient on startup and shutting it down on application close. Automatically shutting it down in deinit leads to patterns where users create HTTPClients over and over which is bad performance-wise since the underlying connection pool can not be shared over multiple executions.

dylanshine commented 1 year ago

@ronaldmannak for visibility