Skip to content

fix(functions): set request timeout to 150 seconds when invoking functions #728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Sources/Functions/FunctionsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public final class FunctionsClient: Sendable {
Data, URLResponse
)

/// Request idle timeout: 150s (If an Edge Function doesn't send a response before the timeout, 504 Gateway Timeout will be returned)
///
/// See more: https://supabase.com/docs/guides/functions/limits
public static let requestIdleTimeout: TimeInterval = 150

/// The base URL for the functions.
let url: URL

Expand Down Expand Up @@ -246,7 +251,8 @@ public final class FunctionsClient: Sendable {
method: FunctionInvokeOptions.httpMethod(options.method) ?? .post,
query: options.query,
headers: mutableState.headers.merging(with: options.headers),
body: options.body
body: options.body,
timeoutInterval: FunctionsClient.requestIdleTimeout
)

if let region = options.region ?? region {
Expand Down
14 changes: 9 additions & 5 deletions Sources/Helpers/HTTP/HTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,42 @@ package struct HTTPRequest: Sendable {
package var query: [URLQueryItem]
package var headers: HTTPFields
package var body: Data?
package var timeoutInterval: TimeInterval

package init(
url: URL,
method: HTTPTypes.HTTPRequest.Method,
query: [URLQueryItem] = [],
headers: HTTPFields = [:],
body: Data? = nil
body: Data? = nil,
timeoutInterval: TimeInterval = 60
) {
self.url = url
self.method = method
self.query = query
self.headers = headers
self.body = body
self.timeoutInterval = timeoutInterval
}

package init?(
urlString: String,
method: HTTPTypes.HTTPRequest.Method,
query: [URLQueryItem] = [],
headers: HTTPFields = [:],
body: Data?
body: Data? = nil,
timeoutInterval: TimeInterval = 60
) {
guard let url = URL(string: urlString) else { return nil }
self.init(url: url, method: method, query: query, headers: headers, body: body)
self.init(url: url, method: method, query: query, headers: headers, body: body, timeoutInterval: timeoutInterval)
}

package var urlRequest: URLRequest {
var urlRequest = URLRequest(url: query.isEmpty ? url : url.appendingQueryItems(query))
var urlRequest = URLRequest(url: query.isEmpty ? url : url.appendingQueryItems(query), timeoutInterval: timeoutInterval)
urlRequest.httpMethod = method.rawValue
urlRequest.allHTTPHeaderFields = .init(headers.map { ($0.name.rawName, $0.value) }) { $1 }
urlRequest.httpBody = body

if urlRequest.httpBody != nil, urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
}
Expand Down
Loading