func makeRequests()

in http2-client/Sources/http2-client/main.swift [240:281]


func makeRequests(channel: Channel,
                  host: String,
                  uris: [String],
                  channelErrorForwarder: EventLoopFuture<Void>) -> EventLoopFuture<[(String, EventLoopPromise<[HTTPClientResponsePart]>)]> {
    // Step 1 is to find the HTTP2StreamMultiplexer so we can create HTTP/2 streams for our requests.
    return channel.pipeline.handler(type: HTTP2StreamMultiplexer.self).map { http2Multiplexer -> [(String, EventLoopPromise<[HTTPClientResponsePart]>)] in
        var remainingURIs = uris
        // Helper function to initialise an HTTP/2 stream.
        func requestStreamInitializer(uri: String,
                                      responseReceivedPromise: EventLoopPromise<[HTTPClientResponsePart]>,
                                      channel: Channel,
                                      streamID: HTTP2StreamID) -> EventLoopFuture<Void> {
            let uri = remainingURIs.removeFirst()
            channel.eventLoop.assertInEventLoop()
            return channel.pipeline.addHandlers([HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https),
                                                 SendRequestHandler(host: host,
                                                                    request: .init(target: uri,
                                                                                   headers: [],
                                                                                   body: nil,
                                                                                   trailers: nil),
                                                                    responseReceivedPromise: responseReceivedPromise)],
                                                position: .last)
        }

        // Step 2: Let's create an HTTP/2 stream for each request.
        var responseReceivedPromises: [(String, EventLoopPromise<[HTTPClientResponsePart]>)] = []
        for uri in uris {
            let promise = channel.eventLoop.makePromise(of: [HTTPClientResponsePart].self)
            channelErrorForwarder.cascadeFailure(to: promise)
            responseReceivedPromises.append((uri, promise))
            // Create the actual HTTP/2 stream using the multiplexer's `createStreamChannel` method.
            http2Multiplexer.createStreamChannel(promise: nil) { (channel: Channel, streamID: HTTP2StreamID) -> EventLoopFuture<Void> in
                // Call the above handler to initialise the stream which will send off the actual request.
                requestStreamInitializer(uri: uri,
                                         responseReceivedPromise: promise,
                                         channel: channel,
                                         streamID: streamID)
            }
        }
        return responseReceivedPromises
    }
}