Skip to content

Commit 4f1d525

Browse files
committed
Introduce HTTPRequestTask
1 parent 0a6bef5 commit 4f1d525

File tree

3 files changed

+934
-0
lines changed

3 files changed

+934
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the AsyncHTTPClient open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Logging
16+
import NIO
17+
import NIOConcurrencyHelpers
18+
import NIOHTTP1
19+
20+
/// A handle to the request queuer.
21+
///
22+
/// Use this handle to cancel the request, while it is waiting for a free connection, to execute the request.
23+
/// This protocol is implemented by the `HTTPConnectionPool`.
24+
protocol HTTP1RequestQueuer {
25+
func cancelRequest(task: HTTPRequestTask)
26+
}
27+
28+
/// A handle to the request executor.
29+
///
30+
/// This protocol is implemented by the `HTTP1ClientChannelHandler`.
31+
protocol HTTP1RequestExecutor {
32+
33+
/// Writes a body part into the channel pipeline
34+
func writeRequestBodyPart(_: IOData, task: HTTPRequestTask)
35+
36+
/// Signals that the request body stream has finished
37+
func finishRequestBodyStream(task: HTTPRequestTask)
38+
39+
/// Signals that more bytes from response body stream can be consumed.
40+
///
41+
/// The request executor will call `receiveResponseBodyPart(_ buffer: ByteBuffer)` with more data after
42+
/// this call.
43+
func demandResponseBodyStream(task: HTTPRequestTask)
44+
45+
/// Signals that the request has been cancelled.
46+
func cancelRequest(task: HTTPRequestTask)
47+
}
48+
49+
/// An abstraction over a request.
50+
protocol HTTPRequestTask: AnyObject {
51+
var request: HTTPClient.Request { get }
52+
var logger: Logger { get }
53+
54+
/// The delegates EventLoop
55+
var eventLoop: EventLoop { get }
56+
57+
var connectionDeadline: NIODeadline { get }
58+
var idleReadTimeout: TimeAmount? { get }
59+
60+
var eventLoopPreference: HTTPClient.EventLoopPreference { get }
61+
62+
/// Informs the task, that it was queued for execution
63+
///
64+
/// This happens if all available connections are currently in use
65+
func requestWasQueued(_: HTTP1RequestQueuer)
66+
67+
/// Informs the task about the connection it will be executed on
68+
///
69+
/// This is only here to allow existing tests to pass. We should rework this ASAP to get rid of this functionality
70+
func willBeExecutedOnConnection(_: HTTPConnectionPool.Connection)
71+
72+
/// Will be called by the ChannelHandler to indicate that the request is going to be send.
73+
///
74+
/// This will be called on the Channel's EventLoop. Do **not block** during your execution!
75+
///
76+
/// - Returns: A bool indicating if the request should really be started. Return false if the request has already been cancelled.
77+
/// If the request is cancelled after this method call `executor.cancel()` to stop request execution.
78+
func willExecuteRequest(_: HTTP1RequestExecutor) -> Bool
79+
80+
/// Will be called by the ChannelHandler to indicate that the request head has been sent.
81+
func requestHeadSent(_: HTTPRequestHead)
82+
83+
/// Start request streaming
84+
func startRequestBodyStream()
85+
86+
/// Pause request streaming
87+
func pauseRequestBodyStream()
88+
89+
/// Pause request streaming
90+
func resumeRequestBodyStream()
91+
92+
func receiveResponseHead(_ head: HTTPResponseHead)
93+
func receiveResponseBodyPart(_ buffer: ByteBuffer)
94+
func receiveResponseEnd()
95+
96+
func fail(_ error: Error)
97+
}
98+

Sources/AsyncHTTPClient/HTTPHandler.swift

+4
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ extension URL {
624624
}
625625
}
626626

627+
protocol HTTPClientTaskDelegate {
628+
func cancel()
629+
}
630+
627631
extension HTTPClient {
628632
/// Response execution context. Will be created by the library and could be used for obtaining
629633
/// `EventLoopFuture<Response>` of the execution or cancellation of the execution.

0 commit comments

Comments
 (0)