Class HttpPullWorker
A source worker that pulls events with HTTP GET requests.
public abstract class HttpPullWorker : IDisposable
- Inheritance
-
HttpPullWorker
- Implements
- Inherited Members
Constructors
HttpPullWorker(Uri)
Creates an instance of the HttpPullWorker class.
protected HttpPullWorker(Uri uri)
Parameters
uriUri- The base URI of the source.
Properties
BaseUri
The base URI.
protected Uri BaseUri { get; }
Property Value
Client
The HTTP client that interacts with the source.
protected HttpClient Client { get; }
Property Value
DefaultPeriod
The default pulling period.
protected virtual TimeSpan DefaultPeriod { get; }
Property Value
ForceDefaultPeriod
Whether to force the pulling period to be DefaultPeriod.
protected virtual bool ForceDefaultPeriod { get; }
Property Value
Remarks
If set to false, the client determines the period from the
max-age directive in the Cache-Control header of the response if it exists, and clamps it to be at least MinimumPeriod. The period falls back to DefaultPeriod if the directive is not found.MinimumPeriod
The minimum pulling period.
protected virtual TimeSpan MinimumPeriod { get; }
Property Value
Methods
AfterHandled(CancellationToken)
Called when a response is handled successfully, or when the server responses with a non-error status code (100~399).
protected virtual Task AfterHandled(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationToken
Returns
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
protected virtual void Dispose(bool disposing)
Parameters
disposingbool
GetUri()
Gets the URI of the next request, usually based on BaseUri.
protected virtual Uri GetUri()
Returns
- Uri
- The URI of the next request.
Remarks
If not overridden, the request URI is always BaseUri.
Handle(Stream, HttpResponseHeaders, CancellationToken)
Handles a response.
protected abstract Task Handle(Stream stream, HttpResponseHeaders headers, CancellationToken cancellationToken)
Parameters
streamStream- The stream that contains the content of the response.
headersHttpResponseHeaders- The response headers.
cancellationTokenCancellationToken- A cancellation token.
Returns
- Task
- The task.
HandleRawResponse(HttpResponseMessage, CancellationToken)
Handles a raw response message.
protected virtual Task HandleRawResponse(HttpResponseMessage response, CancellationToken cancellationToken)
Parameters
responseHttpResponseMessage- The response message.
cancellationTokenCancellationToken- A cancellation token.
Returns
- Task
- A task.
OnError(Exception)
Called when an error occurs.
protected abstract void OnError(Exception ex)
Parameters
exException- The exception.
OnHeartbeat()
Called when a response is received.
protected abstract void OnHeartbeat()
RunAsync(CancellationToken)
Runs the worker.
public Task RunAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationToken- A cancellation token.
Returns
- Task
- The task.
Exceptions
- InvalidOperationException
- The server responses with an unhandled status code.
TryGetAsync(Uri, CancellationToken, HttpRetryStrategies, HttpRetryStrategies, int)
Try to send a GET request to the specified URI.
protected Task<HttpResponseMessage?> TryGetAsync(Uri uri, CancellationToken cancellationToken, HttpRetryStrategies clientErrorRetryStrategies = HttpRetryStrategies.Throw, HttpRetryStrategies serverErrorRetryStrategies = HttpRetryStrategies.Report | HttpRetryStrategies.RetryBeforeExit, int retries = 5)
Parameters
uriUri- The URI.
cancellationTokenCancellationToken- The cancellation token.
clientErrorRetryStrategiesHttpRetryStrategies- The retry strategy on client error.
serverErrorRetryStrategiesHttpRetryStrategies- The retry strategy on server error.
retriesint- Times to retry before the request fails.
Returns
- Task<HttpResponseMessage>
- The response message, or null if the request fails.
TrySendAsync(Func<HttpRequestMessage>, CancellationToken, HttpRetryStrategies, HttpRetryStrategies, int)
Try to send a request to the specified URI.
protected Task<HttpResponseMessage?> TrySendAsync(Func<HttpRequestMessage> msgFactory, CancellationToken cancellationToken, HttpRetryStrategies clientErrorRetryStrategies = HttpRetryStrategies.Throw, HttpRetryStrategies serverErrorRetryStrategies = HttpRetryStrategies.Report | HttpRetryStrategies.RetryBeforeExit, int retries = 5)
Parameters
msgFactoryFunc<HttpRequestMessage>- A function that creates the request message.
cancellationTokenCancellationToken- The cancellation token.
clientErrorRetryStrategiesHttpRetryStrategies- The retry strategy on client error.
serverErrorRetryStrategiesHttpRetryStrategies- The retry strategy on server error.
retriesint- Times to retry before the request fails.
Returns
- Task<HttpResponseMessage>
- The response message, or null if the request fails.