Skip to content

Commit 3d2930d

Browse files
authored
fix(functions): set request timeout to 150 seconds when invoking functions (#728)
1 parent 6b1d60f commit 3d2930d

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Sources/Functions/FunctionsClient.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public final class FunctionsClient: Sendable {
1515
Data, URLResponse
1616
)
1717

18+
/// Request idle timeout: 150s (If an Edge Function doesn't send a response before the timeout, 504 Gateway Timeout will be returned)
19+
///
20+
/// See more: https://supabase.com/docs/guides/functions/limits
21+
public static let requestIdleTimeout: TimeInterval = 150
22+
1823
/// The base URL for the functions.
1924
let url: URL
2025

@@ -246,7 +251,8 @@ public final class FunctionsClient: Sendable {
246251
method: FunctionInvokeOptions.httpMethod(options.method) ?? .post,
247252
query: options.query,
248253
headers: mutableState.headers.merging(with: options.headers),
249-
body: options.body
254+
body: options.body,
255+
timeoutInterval: FunctionsClient.requestIdleTimeout
250256
)
251257

252258
if let region = options.region ?? region {

Sources/Helpers/HTTP/HTTPRequest.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,42 @@ package struct HTTPRequest: Sendable {
1818
package var query: [URLQueryItem]
1919
package var headers: HTTPFields
2020
package var body: Data?
21+
package var timeoutInterval: TimeInterval
2122

2223
package init(
2324
url: URL,
2425
method: HTTPTypes.HTTPRequest.Method,
2526
query: [URLQueryItem] = [],
2627
headers: HTTPFields = [:],
27-
body: Data? = nil
28+
body: Data? = nil,
29+
timeoutInterval: TimeInterval = 60
2830
) {
2931
self.url = url
3032
self.method = method
3133
self.query = query
3234
self.headers = headers
3335
self.body = body
36+
self.timeoutInterval = timeoutInterval
3437
}
3538

3639
package init?(
3740
urlString: String,
3841
method: HTTPTypes.HTTPRequest.Method,
3942
query: [URLQueryItem] = [],
4043
headers: HTTPFields = [:],
41-
body: Data?
44+
body: Data? = nil,
45+
timeoutInterval: TimeInterval = 60
4246
) {
4347
guard let url = URL(string: urlString) else { return nil }
44-
self.init(url: url, method: method, query: query, headers: headers, body: body)
48+
self.init(url: url, method: method, query: query, headers: headers, body: body, timeoutInterval: timeoutInterval)
4549
}
4650

4751
package var urlRequest: URLRequest {
48-
var urlRequest = URLRequest(url: query.isEmpty ? url : url.appendingQueryItems(query))
52+
var urlRequest = URLRequest(url: query.isEmpty ? url : url.appendingQueryItems(query), timeoutInterval: timeoutInterval)
4953
urlRequest.httpMethod = method.rawValue
5054
urlRequest.allHTTPHeaderFields = .init(headers.map { ($0.name.rawName, $0.value) }) { $1 }
5155
urlRequest.httpBody = body
52-
56+
5357
if urlRequest.httpBody != nil, urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
5458
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
5559
}

0 commit comments

Comments
 (0)