| | | 1 | | using Google.Cloud.PubSub.V1; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | |
| | | 4 | | namespace AsyncResponse.Transports.GooglePubSub; |
| | | 5 | | |
| | 206 | 6 | | internal sealed class GooglePubSubReplyTargetProvider( |
| | 206 | 7 | | IOptions<GooglePubSubAsyncResponseOptions> _options) : IAsyncResponseReplyTargetProvider |
| | | 8 | | { |
| | | 9 | | /// <summary>Gets the configured reply target.</summary> |
| | | 10 | | public AsyncResponseReplyTarget GetReplyTarget(string? name = null) |
| | | 11 | | { |
| | 14 | 12 | | var options = _options.Value; |
| | | 13 | | // Options-validator parity with the other transports' providers: a hand-off address must |
| | | 14 | | // come from a configuration that passes the transport's own checks. |
| | 14 | 15 | | GooglePubSubOptionsValidator.ValidateTimeouts(options); |
| | 14 | 16 | | var targetName = string.IsNullOrWhiteSpace(name) |
| | 14 | 17 | | ? options.DefaultReplyTargetName |
| | 14 | 18 | | : name; |
| | | 19 | | |
| | 14 | 20 | | var target = ResolveTarget(options, targetName); |
| | 10 | 21 | | var projectId = GooglePubSubOptionsValidator.Required( |
| | 10 | 22 | | target.ProjectId ?? options.ProjectId, |
| | 10 | 23 | | $"{nameof(GooglePubSubReplyTargetOptions)}.{nameof(GooglePubSubReplyTargetOptions.ProjectId)}"); |
| | 10 | 24 | | var topicId = GooglePubSubOptionsValidator.Required( |
| | 10 | 25 | | target.TopicId, |
| | 10 | 26 | | $"{nameof(GooglePubSubReplyTargetOptions)}.{nameof(GooglePubSubReplyTargetOptions.TopicId)}"); |
| | | 27 | | |
| | | 28 | | // A NAMED target must not be the worker topic in the transport's own project |
| | | 29 | | // (DB-transport parity): its responses would be consumed as worker jobs while the waiter |
| | | 30 | | // times out. |
| | 10 | 31 | | if (StringComparer.Ordinal.Equals(projectId, options.ProjectId) |
| | 10 | 32 | | && StringComparer.Ordinal.Equals(topicId, options.WorkerTopicId)) |
| | | 33 | | { |
| | 2 | 34 | | throw new InvalidOperationException( |
| | 2 | 35 | | $"Google Pub/Sub async-response reply target '{targetName}' uses topic '{topicId}' in project '{projectI |
| | 2 | 36 | | $"{nameof(GooglePubSubAsyncResponseOptions.WorkerTopicId)}; its responses would be consumed as worker jo |
| | | 37 | | } |
| | | 38 | | |
| | 8 | 39 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 8 | 40 | | { |
| | 8 | 41 | | ["projectId"] = projectId, |
| | 8 | 42 | | ["topicId"] = topicId |
| | 8 | 43 | | }; |
| | | 44 | | |
| | 8 | 45 | | return new AsyncResponseReplyTarget |
| | 8 | 46 | | { |
| | 8 | 47 | | Name = targetName, |
| | 8 | 48 | | Transport = GooglePubSubAsyncResponseOptions.TransportName, |
| | 8 | 49 | | Address = TopicName.FromProjectTopic(projectId, topicId).ToString(), |
| | 8 | 50 | | Properties = properties |
| | 8 | 51 | | }; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | private static GooglePubSubReplyTargetOptions ResolveTarget( |
| | | 55 | | GooglePubSubAsyncResponseOptions options, |
| | | 56 | | string targetName) |
| | | 57 | | { |
| | 14 | 58 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 8 | 59 | | return configured; |
| | | 60 | | |
| | 6 | 61 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName) |
| | 6 | 62 | | && !string.IsNullOrWhiteSpace(options.ResponseTopicId)) |
| | | 63 | | { |
| | 2 | 64 | | return new GooglePubSubReplyTargetOptions |
| | 2 | 65 | | { |
| | 2 | 66 | | ProjectId = options.ProjectId, |
| | 2 | 67 | | TopicId = options.ResponseTopicId |
| | 2 | 68 | | }; |
| | | 69 | | } |
| | | 70 | | |
| | 4 | 71 | | throw new InvalidOperationException( |
| | 4 | 72 | | $"Google Pub/Sub async-response reply target '{targetName}' is not configured. " + |
| | 4 | 73 | | $"Configure {nameof(GooglePubSubAsyncResponseOptions.ResponseTopicId)} for the default target " + |
| | 4 | 74 | | $"or add a named target with {nameof(GooglePubSubAsyncResponseOptions.AddReplyTarget)}."); |
| | | 75 | | } |
| | | 76 | | } |