Fix EmbeddingService

This commit is contained in:
2026-02-27 14:50:47 +01:00
parent e9d0ad3c66
commit 98d9ee2b51
3 changed files with 56 additions and 41 deletions

View File

@@ -75,6 +75,16 @@ final class EmbeddingService {
private let settings = SettingsService.shared
/// Dedicated session for embedding requests keeps embedding traffic isolated
/// from the chat API sessions and self-limits concurrent connections.
private let session: URLSession = {
let config = URLSessionConfiguration.default
config.httpMaximumConnectionsPerHost = 2 // max 2 concurrent embedding requests
config.timeoutIntervalForRequest = 60
config.timeoutIntervalForResource = 120
return URLSession(configuration: config)
}()
private init() {}
// MARK: - Provider Detection
@@ -164,7 +174,7 @@ final class EmbeddingService {
]
request.httpBody = try JSONSerialization.data(withJSONObject: body)
let (data, response) = try await URLSession.shared.data(for: request)
let (data, response) = try await session.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
throw EmbeddingError.invalidResponse
@@ -205,7 +215,7 @@ final class EmbeddingService {
]
request.httpBody = try JSONSerialization.data(withJSONObject: body)
let (data, response) = try await URLSession.shared.data(for: request)
let (data, response) = try await session.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
throw EmbeddingError.invalidResponse
@@ -247,7 +257,7 @@ final class EmbeddingService {
]
request.httpBody = try JSONSerialization.data(withJSONObject: body)
let (data, response) = try await URLSession.shared.data(for: request)
let (data, response) = try await session.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
throw EmbeddingError.invalidResponse