This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
SqsMqServer.cs
228 lines (187 loc) · 9.68 KB
/
SqsMqServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.Linq;
using Amazon;
using ServiceStack.Aws.Support;
using ServiceStack.Messaging;
namespace ServiceStack.Aws.Sqs
{
public class SqsMqServer : BaseMqServer<SqsMqWorker>
{
private readonly Dictionary<Type, SqsMqWorkerInfo> handlerMap = new Dictionary<Type, SqsMqWorkerInfo>();
private readonly ISqsMqMessageFactory sqsMqMessageFactory;
public TimeSpan PollingDuration { get; set; } = TimeSpan.FromMilliseconds(1000);
public SqsMqServer() : this(new SqsConnectionFactory()) { }
public SqsMqServer(string awsAccessKey, string awsSecretKey, RegionEndpoint region)
: this(new SqsConnectionFactory(awsAccessKey, awsSecretKey, region)) { }
public SqsMqServer(SqsConnectionFactory sqsConnectionFactory) : this(new SqsMqMessageFactory(new SqsQueueManager(sqsConnectionFactory))) { }
public SqsMqServer(ISqsMqMessageFactory sqsMqMessageFactory)
{
Guard.AgainstNullArgument(sqsMqMessageFactory, "sqsMqMessageFactory");
this.sqsMqMessageFactory = sqsMqMessageFactory;
}
public override IMessageFactory MessageFactory => sqsMqMessageFactory;
public SqsConnectionFactory ConnectionFactory => sqsMqMessageFactory.ConnectionFactory;
/// <summary>
/// How many times a message should be retried before sending to the DLQ (Max of 1000).
/// </summary>
public int RetryCount
{
get => sqsMqMessageFactory.RetryCount;
set => sqsMqMessageFactory.RetryCount = value;
}
/// <summary>
/// How often, in seconds, any buffered SQS data is forced to be processed by the client. Only valid if buffering
/// is enabled for a given model/server. By default, this is off entirely, which means if you are using buffering,
/// data will only be processed to the server when operations occur that push a given queue/type over the
/// configured size of the buffer.
/// </summary>
public int BufferFlushIntervalSeconds
{
get => sqsMqMessageFactory.BufferFlushIntervalSeconds;
set => sqsMqMessageFactory.BufferFlushIntervalSeconds = value;
}
/// <summary>
/// Default time (in seconds) each in-flight message remains locked/unavailable on the queue before being returned to visibility
/// Default of 30 seconds
/// See http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html
/// </summary>
public int VisibilityTimeout
{
get => sqsMqMessageFactory.QueueManager.DefaultVisibilityTimeout;
set
{
Guard.AgainstArgumentOutOfRange(value < 0 || value > SqsQueueDefinition.MaxVisibilityTimeoutSeconds,
"SQS MQ VisibilityTimeout must be 0-43200");
sqsMqMessageFactory.QueueManager.DefaultVisibilityTimeout = value;
}
}
/// <summary>
/// Defaut time (in seconds) each request to receive from the queue waits for a message to arrive
/// Default is 0 seconds
/// See http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html
/// </summary>
public int ReceiveWaitTime
{
get => sqsMqMessageFactory.QueueManager.DefaultReceiveWaitTime;
set
{
Guard.AgainstArgumentOutOfRange(value < 0 || value > SqsQueueDefinition.MaxWaitTimeSeconds,
"SQS MQ ReceiveWaitTime must be 0-20");
sqsMqMessageFactory.QueueManager.DefaultReceiveWaitTime = value;
}
}
/// <summary>
/// Disables buffering of send/delete/change/receive calls to SQS (call per request when disabled)
/// </summary>
public bool DisableBuffering
{
get => sqsMqMessageFactory.QueueManager.DisableBuffering;
set => sqsMqMessageFactory.QueueManager.DisableBuffering = value;
}
/// <summary>
/// Execute global transformation or custom logic before a request is processed.
/// Must be thread-safe.
/// </summary>
public Func<IMessage, IMessage> RequestFilter { get; set; }
/// <summary>
/// Execute global transformation or custom logic on the response.
/// Must be thread-safe.
/// </summary>
public Func<object, object> ResponseFilter { get; set; }
protected override void DoDispose()
{
try
{
sqsMqMessageFactory.Dispose();
}
catch (Exception ex)
{
ErrorHandler?.Invoke(ex);
}
}
public override void RegisterHandler<T>(Func<IMessage<T>, object> processMessageFn)
{
RegisterHandler(processMessageFn, null, noOfThreads: 1);
}
public override void RegisterHandler<T>(Func<IMessage<T>, object> processMessageFn, int noOfThreads)
{
RegisterHandler(processMessageFn, null, noOfThreads: noOfThreads);
}
public void RegisterHandler<T>(Func<IMessage<T>, object> processMessageFn, int noOfThreads,
int? retryCount = null, int? visibilityTimeoutSeconds = null,
int? receiveWaitTimeSeconds = null)
{
RegisterHandler(processMessageFn, null, noOfThreads, retryCount, visibilityTimeoutSeconds, receiveWaitTimeSeconds);
}
public override void RegisterHandler<T>(Func<IMessage<T>, object> processMessageFn,
Action<IMessageHandler, IMessage<T>, Exception> processExceptionEx)
{
RegisterHandler(processMessageFn, processExceptionEx, noOfThreads: 1);
}
public override void RegisterHandler<T>(Func<IMessage<T>, object> processMessageFn, Action<IMessageHandler, IMessage<T>, Exception> processExceptionEx, int noOfThreads)
{
RegisterHandler(processMessageFn, processExceptionEx, noOfThreads: noOfThreads);
}
public void RegisterHandler<T>(Func<IMessage<T>, object> processMessageFn,
Action<IMessageHandler, IMessage<T>, Exception> processExceptionEx,
int noOfThreads, int? retryCount = null,
int? visibilityTimeoutSeconds = null, int? receiveWaitTimeSeconds = null,
bool? disableBuffering = null)
{
var type = typeof(T);
Guard.Against<ArgumentException>(handlerMap.ContainsKey(type), string.Concat("SQS Message handler has already been registered for type: ", type.Name));
var retry = RetryCount = retryCount ?? RetryCount;
handlerMap[type] = new SqsMqWorkerInfo
{
MessageHandlerFactory = new MessageHandlerFactory<T>(this, processMessageFn, processExceptionEx)
{
RequestFilter = this.RequestFilter,
ResponseFilter = this.ResponseFilter,
PublishResponsesWhitelist = this.PublishResponsesWhitelist,
PublishToOutqWhitelist = this.PublishToOutqWhitelist,
RetryCount = retry
},
MessageType = type,
RetryCount = retry,
ThreadCount = noOfThreads,
VisibilityTimeout = visibilityTimeoutSeconds ?? this.VisibilityTimeout,
ReceiveWaitTime = receiveWaitTimeSeconds ?? this.ReceiveWaitTime,
DisableBuffering = disableBuffering ?? this.DisableBuffering
};
LicenseUtils.AssertValidUsage(LicenseFeature.ServiceStack, QuotaType.Operations, handlerMap.Count);
}
protected override void Init()
{
if (workers != null)
{
return;
}
sqsMqMessageFactory.ErrorHandler = this.ErrorHandler;
workers = new List<SqsMqWorker>();
foreach (var handler in handlerMap)
{
var msgType = handler.Key;
var info = handler.Value;
// First build the DLQ that will become the redrive policy for other q's for this type
var dlqDefinition = sqsMqMessageFactory.QueueManager.CreateQueue(info.QueueNames.Dlq, info);
// Base in q and workers
sqsMqMessageFactory.QueueManager.CreateQueue(info.QueueNames.In, info, dlqDefinition.QueueArn);
info.ThreadCount.Times(i => workers.Add(
new SqsMqWorker(sqsMqMessageFactory, info, info.QueueNames.In, WorkerErrorHandler) { PollingDuration = PollingDuration }));
// Need an outq?
if (PublishResponsesWhitelist == null || PublishResponsesWhitelist.Any(x => x == msgType.Name))
{
sqsMqMessageFactory.QueueManager.CreateQueue(info.QueueNames.Out, info, dlqDefinition.QueueArn);
}
// Priority q and workers
if (PriorityQueuesWhitelist == null || PriorityQueuesWhitelist.Any(x => x == msgType.Name))
{ // Need priority queue and workers
sqsMqMessageFactory.QueueManager.CreateQueue(info.QueueNames.Priority, info, dlqDefinition.QueueArn);
info.ThreadCount.Times(i => workers.Add(
new SqsMqWorker(sqsMqMessageFactory, info, info.QueueNames.Priority, WorkerErrorHandler) { PollingDuration = PollingDuration }));
}
}
}
}
}