-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathConsumerFlowStrategy.java
230 lines (206 loc) · 8.12 KB
/
ConsumerFlowStrategy.java
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
229
230
// Copyright (c) 2023-2024 Broadcom. All Rights Reserved.
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
//
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
package com.rabbitmq.stream;
import java.util.concurrent.atomic.AtomicLong;
/**
* Contract to determine when a subscription provides credits to get more messages.
*
* <p>The broker delivers "chunks" of messages to consumers. A chunk can contain from 1 to several
* thousands of messages. The broker send chunks as long as the subscription has <em>credits</em>. A
* client connection can provide credits for a given subscription and the broker will send the
* corresponding number of chunks (1 credit = 1 chunk).
*
* <p>This credit mechanism avoids overwhelming a consumer with messages. A consumer does not want
* to provide a credit only when it is done with messages of a chunk, because it will be idle
* between its credit request and the arrival of the next chunk. The idea is to keep consumers busy
* as much as possible, without accumulating an in-memory backlog on the client side. There is no
* ideal solution, it depends on the use cases and several parameters (processing time, network,
* etc).
*
* <p>This is an experimental API, subject to change.
*
* @since 0.12.0
* @see MessageHandler.Context#processed()
* @see ConsumerBuilder#flow()
*/
public interface ConsumerFlowStrategy {
/**
* The initial number of credits for a subscription.
*
* <p>It must be greater than 0. Values are usually between 1 and 10.
*
* @return initial number of credits
*/
int initialCredits();
/**
* Return the behavior for {@link MessageHandler.Context#processed()} calls.
*
* <p>This method is called for each chunk of messages. Implementations return a callback that
* will be called when applications consider a message dealt with and call {@link
* MessageHandler.Context#processed()}. The callback can count messages and provide credits
* accordingly.
*
* @param context chunk context
* @return the message processed callback
*/
MessageProcessedCallback start(Context context);
/** Chunk context. */
interface Context {
/**
* Provide credits for the subscription.
*
* <p>{@link ConsumerFlowStrategy} implementation should always provide 1 credit a given chunk.
*
* @param credits the number of credits provided, usually 1
*/
void credits(int credits);
/**
* The number of messages in the chunk.
*
* @return number of messages in the chunk
*/
long messageCount();
}
/** Behavior for {@link MessageHandler.Context#processed()} calls. */
@FunctionalInterface
interface MessageProcessedCallback {
/**
* Method called when {@link MessageHandler.Context#processed()} is called.
*
* <p>There is one instance of this class for a given chunk and it is called for the <code>
* processed()</code> calls of the message of this chunk.
*
* <p>Implementations can count messages and call {@link Context#credits(int)} when appropriate.
*
* <p>Note calls to {@link MessageHandler.Context#processed()} are not idempotent: an
* application can call the method several times for the same message and implementations must
* deal with these multiple calls if they impact their logic.
*
* @param messageContext context of the message
*/
void processed(MessageHandler.Context messageContext);
}
/**
* Strategy that provides 1 initial credit and a credit on each new chunk.
*
* <p>Calls to {@link MessageHandler.Context#processed()} are ignored.
*
* @return flow strategy
*/
static ConsumerFlowStrategy creditOnChunkArrival() {
return creditOnChunkArrival(1);
}
/**
* Strategy that provides the specified number of initial credits and a credit on each new chunk.
*
* <p>Calls to {@link MessageHandler.Context#processed()} are ignored.
*
* @param initialCredits number of initial credits
* @return flow strategy
* @see com.rabbitmq.stream.ConsumerBuilder.FlowConfiguration#initialCredits(int)
*/
static ConsumerFlowStrategy creditOnChunkArrival(int initialCredits) {
return new CreditOnChunkArrivalConsumerFlowStrategy(initialCredits);
}
/**
* Strategy that provides 10 initial credits and a credit when half of the chunk messages are
* processed.
*
* <p>Make sure to call {@link MessageHandler.Context#processed()} on every message when using
* this strategy, otherwise the broker may stop sending messages to the consumer.
*
* @return flow strategy
*/
static ConsumerFlowStrategy creditWhenHalfMessagesProcessed() {
return creditOnProcessedMessageCount(10, 0.5);
}
/**
* Strategy that provides the specified number of initial credits and a credit when half of the
* chunk messages are processed.
*
* <p>Make sure to call {@link MessageHandler.Context#processed()} on every message when using
* this strategy, otherwise the broker may stop sending messages to the consumer.
*
* @param initialCredits number of initial credits
* @return flow strategy
* @see com.rabbitmq.stream.ConsumerBuilder.FlowConfiguration#initialCredits(int)
*/
static ConsumerFlowStrategy creditWhenHalfMessagesProcessed(int initialCredits) {
return creditOnProcessedMessageCount(initialCredits, 0.5);
}
/**
* Strategy that provides the specified number of initial credits and a credit when the specified
* ratio of the chunk messages are processed.
*
* <p>Make sure to call {@link MessageHandler.Context#processed()} on every message when using
* this strategy, otherwise the broker may stop sending messages to the consumer.
*
* @param initialCredits number of initial credits
* @return flow strategy
*/
static ConsumerFlowStrategy creditOnProcessedMessageCount(int initialCredits, double ratio) {
return new MessageCountConsumerFlowStrategy(initialCredits, ratio);
}
/**
* Strategy that provides the specified number of initial credits and a credit on each new chunk.
*
* <p>Calls to {@link MessageHandler.Context#processed()} are ignored.
*/
class CreditOnChunkArrivalConsumerFlowStrategy implements ConsumerFlowStrategy {
private final int initialCredits;
private CreditOnChunkArrivalConsumerFlowStrategy(int initialCredits) {
this.initialCredits = initialCredits;
}
@Override
public int initialCredits() {
return this.initialCredits;
}
@Override
public MessageProcessedCallback start(Context context) {
context.credits(1);
return value -> {};
}
}
/**
* Strategy that provides the specified number of initial credits and a credit when the specified
* ratio of the chunk messages are processed.
*
* <p>Make sure to call {@link MessageHandler.Context#processed()} on every message when using
* this strategy, otherwise the broker may stop sending messages to the consumer.
*/
class MessageCountConsumerFlowStrategy implements ConsumerFlowStrategy {
private final int initialCredits;
private final double ratio;
private MessageCountConsumerFlowStrategy(int initialCredits, double ratio) {
this.initialCredits = initialCredits;
this.ratio = ratio;
}
@Override
public int initialCredits() {
return this.initialCredits;
}
@Override
public MessageProcessedCallback start(Context context) {
long l = (long) (context.messageCount() * ratio);
long limit = Math.max(1, l);
AtomicLong processedMessages = new AtomicLong(0);
return messageOffset -> {
if (processedMessages.incrementAndGet() == limit) {
context.credits(1);
}
};
}
}
}