HTTPClientResponseDelegate

HTTPClientResponseDelegate allows an implementation to receive notifications about request processing and to control how response parts are processed.

HTTPHandler.swift:648
protocol HTTPClientResponseDelegate : AnyObject
Browse conforming types

You can implement this protocol if you need fine-grained control over an HTTP request/response, for example, if you want to inspect the response headers before deciding whether to accept a response body, or if you want to stream your request body. Pass an instance of your conforming class to the execute(request:delegate:eventLoop:deadline:) method and this package will call each delegate method appropriately as the request takes place.

Backpressure

A HTTPClientResponseDelegate can be used to exert backpressure on the server response. This is achieved by way of the futures returned from didReceiveHead(task:_:) and didReceiveBodyPart(task:_:). The following functions are part of the “backpressure system” in the delegate:

The first three methods are strictly exclusive, with that exclusivity managed by the futures returned by didReceiveHead(task:_:) and didReceiveBodyPart(task:_:). What this means is that until the returned future is completed, none of these three methods will be called again. This allows delegates to rate limit the server to a capacity it can manage. didFinishRequest(task:) does not return a future, as we are expecting no more data from the server at this time.

didReceiveError(task:_:) is somewhat special: it signals the end of this regime. didReceiveError(task:_:) is not exclusive: it may be called at any time, even if a returned future is not yet completed. didReceiveError(task:_:) is terminal, meaning that once it has been called none of these four methods will be called again. This can be used as a signal to abandon all outstanding work.