Skip to content

Commit

Permalink
Enable milliseconds precision for timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
vzsg committed Sep 7, 2019
1 parent 8bc174e commit b51ef4f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,21 @@ See the tests for examples of both methods.

Equivalent to the [`-U`/`--proxy-user`](https://curl.haxx.se/docs/manpage.html#-U) parameter of curl, which allows specifying the username and password to use when authenticating to the proxy server.

- **timeout(seconds: Int)**
- **timeout(Int)**

Equivalent to the [`-m`/`--max-time`](https://curl.haxx.se/docs/manpage.html#-m) parameter of curl, which allows specifying the maximum time allowed to service the request.
Equivalent to the [`-m`/`--max-time`](https://curl.haxx.se/docs/manpage.html#-m) parameter of curl, which allows specifying the maximum time allowed to service the request in seconds.

- **connectTimeout(seconds: Int)**
- **timeoutMs(Int)**

Equivalent to the [`--connect-timeout`](https://curl.haxx.se/docs/manpage.html#--connect-timeout) parameter of curl, which allows specifying the maximum time allowed for the connection to the server.
Same as `timeout`, but with milliseconds precision.

- **connectTimeout(Int)**

Equivalent to the [`--connect-timeout`](https://curl.haxx.se/docs/manpage.html#--connect-timeout) parameter of curl, which allows specifying the maximum time allowed for the connection to the server in seconds.

- **connectTimeoutMs(Int)** (New in 0.5.0)

Same as `connectTimeout`, but with milliseconds precision.

- **cookieJar(String)**

Expand Down
8 changes: 7 additions & 1 deletion Sources/CurlyClient/CURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,16 @@ class CURLRequest {
/// Maximum time in seconds for the request to complete.
/// The default timeout is never.
timeout(Int),
/// Maximum time in milliseconds for the request to complete.
/// The default timeout is never.
timeoutMs(Int),
/// Maximum time in seconds for the request connection phase.
/// The default timeout is 300 seconds.
connectTimeout(Int),
/// The average transfer speed in bytes per second that the transfer should be below
/// Maximum time in milliseconds for the request connection phase.
/// The default timeout is 300 seconds.
connectTimeoutMs(Int),
/// The average transfer speed in bytes per second that the transfer should be below
/// during `.lowSpeedLimit` seconds for the request to be too slow and abort.
lowSpeedLimit(Int),
/// The time in seconds that the transfer speed should be below the `.lowSpeedLimit`
Expand Down
4 changes: 4 additions & 0 deletions Sources/CurlyClient/CURLRequestOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ extension CURLRequest.Option {
curl.setOption(CURLOPT_PROXYPORT, int: optInt)
case .timeout(let optInt):
curl.setOption(CURLOPT_TIMEOUT, int: optInt)
case .timeoutMs(let optInt):
curl.setOption(CURLOPT_TIMEOUT_MS, int: optInt)
case .connectTimeout(let optInt):
curl.setOption(CURLOPT_CONNECTTIMEOUT, int: optInt)
case .connectTimeoutMs(let optInt):
curl.setOption(CURLOPT_CONNECTTIMEOUT_MS, int: optInt)
case .lowSpeedLimit(let optInt):
curl.setOption(CURLOPT_LOW_SPEED_LIMIT, int: optInt)
case .lowSpeedTime(let optInt):
Expand Down
4 changes: 4 additions & 0 deletions Sources/CurlyClient/CurlyOption+Private.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ extension CurlyOption {
switch self {
case .connectTimeout(let seconds):
return [.connectTimeout(seconds)]
case .connectTimeoutMs(let ms):
return [.connectTimeoutMs(ms)]
case .timeout(let seconds):
return [.timeout(seconds)]
case .timeoutMs(let ms):
return [.timeoutMs(ms)]
case .cookieJar(let file):
return [.cookieFile(file), .cookieJar(file)]
case .proxy(let proxy):
Expand Down
6 changes: 4 additions & 2 deletions Sources/CurlyClient/CurlyOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import Vapor
public enum CurlyOption {
case proxy(String)
case proxyAuth(user: String, password: String)
case timeout(seconds: Int)
case connectTimeout(seconds: Int)
case timeout(Int)
case timeoutMs(Int)
case connectTimeout(Int)
case connectTimeoutMs(Int)
case cookieJar(String)
case followRedirects(Bool)
case insecure(Bool)
Expand Down
6 changes: 3 additions & 3 deletions Tests/CurlyClientTests/CurlyClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class CurlyClientTests: XCTestCase {
"baz": "qux"
], as: MediaType.urlEncodedForm)

req.addCurlyOption(.timeout(seconds: 5))
req.addCurlyOption(.timeout(5))

let res = try app.client().send(req).wait()

Expand All @@ -55,7 +55,7 @@ final class CurlyClientTests: XCTestCase {
func testConvenience() throws {
let app = try testApplication()
let res = try app.client().get("https://httpbin.org/get", beforeSend: { req in
req.addCurlyOption(.timeout(seconds: 5))
req.addCurlyOption(.timeout(5))
}).wait()

XCTAssertNotNil(res.http.body.data)
Expand Down Expand Up @@ -93,7 +93,7 @@ final class CurlyClientTests: XCTestCase {
func testTimeoutError() throws {
let app = try testApplication()
let futureRes = try app.client().get("https://httpstat.us/200?sleep=5000", beforeSend: { req in
req.addCurlyOption(.timeout(seconds: 1))
req.addCurlyOption(.timeoutMs(500))
})

XCTAssertThrowsError(try futureRes.wait(), "Should throw timeout error") { error in
Expand Down

0 comments on commit b51ef4f

Please sign in to comment.