| | | 1 | | using Google.Cloud.PubSub.V1; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.GooglePubSub; |
| | | 4 | | |
| | | 5 | | internal interface IGooglePubSubPublisherClient |
| | | 6 | | { |
| | | 7 | | Task<string> PublishAsync(PubsubMessage message, CancellationToken cancellationToken); |
| | | 8 | | Task ShutdownAsync(TimeSpan timeout); |
| | | 9 | | } |
| | | 10 | | |
| | 194 | 11 | | internal sealed class GooglePubSubPublisherClientAdapter(PublisherClient inner) : IGooglePubSubPublisherClient |
| | | 12 | | { |
| | | 13 | | /// <summary>Publishes the supplied message.</summary> |
| | | 14 | | public Task<string> PublishAsync(PubsubMessage message, CancellationToken cancellationToken) |
| | | 15 | | { |
| | | 16 | | // PublisherClient exposes no cancellation: once handed over, the message sits in the |
| | | 17 | | // client's local batch queue and WILL be flushed (DisposeAsync's ShutdownAsync completes |
| | | 18 | | // only "when all queued messages have been published"). Checking before the hand-off keeps |
| | | 19 | | // a publish cancelled at shutdown from being enqueued-then-delivered while its caller was |
| | | 20 | | // told nothing was sent; a token firing mid-flight still only abandons the wait — that |
| | | 21 | | // residual at-least-once window is inherent to the SDK. |
| | 407 | 22 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 407 | 23 | | return inner.PublishAsync(message).WaitAsync(cancellationToken); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary>Runs the ShutdownAsync operation.</summary> |
| | | 27 | | public Task ShutdownAsync(TimeSpan timeout) |
| | 194 | 28 | | => inner.ShutdownAsync(timeout); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | internal interface IGooglePubSubSubscriberClient |
| | | 32 | | { |
| | | 33 | | Task StartAsync(Func<PubsubMessage, CancellationToken, Task<SubscriberClient.Reply>> handler); |
| | | 34 | | Task StopAsync(SubscriberClient.ShutdownOptions options, CancellationToken cancellationToken); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | internal sealed class GooglePubSubSubscriberClientAdapter(SubscriberClient inner) : IGooglePubSubSubscriberClient |
| | | 38 | | { |
| | | 39 | | /// <summary>Starts this service.</summary> |
| | | 40 | | public Task StartAsync(Func<PubsubMessage, CancellationToken, Task<SubscriberClient.Reply>> handler) |
| | | 41 | | => inner.StartAsync(handler); |
| | | 42 | | |
| | | 43 | | /// <summary>Stops this service.</summary> |
| | | 44 | | public Task StopAsync(SubscriberClient.ShutdownOptions options, CancellationToken cancellationToken) |
| | | 45 | | => inner.StopAsync(options, cancellationToken); |
| | | 46 | | } |