34
34
import java .time .Instant ;
35
35
36
36
public class ConcurrencyLimiter extends AFn implements IObj {
37
- final private Keyword KW_QUEUE = Keyword .intern ("queue" );
38
- final private Keyword KW_STATISTICS = Keyword .intern ("statistics" );
39
- final private Keyword KW_CURRENT_QUEUE_SIZE = Keyword .intern ("current-queue-size" );
40
- final private Keyword KW_CURRENT_CONCURRENCY = Keyword .intern ("current-concurrency" );
41
- final private Keyword KW_REMAINING_QUEUE_SIZE = Keyword .intern ("remaining-queue-size" );
42
- final private Keyword KW_REMAINING_CONCURRENCY = Keyword .intern ("remaining-concurrency" );
43
-
44
37
private final BlockingQueue <Task > queue ;
45
38
private final ExecutorService executor ;
46
39
private final Semaphore limit ;
@@ -50,7 +43,6 @@ public class ConcurrencyLimiter extends AFn implements IObj {
50
43
51
44
protected IFn onQueueCallback ;
52
45
protected IFn onRunCallback ;
53
- protected IFn onPollCallback ;
54
46
55
47
public ConcurrencyLimiter (final ExecutorService executor ,
56
48
final int maxConcurrency ,
@@ -70,10 +62,6 @@ public void setOnRunCallback(IFn f) {
70
62
this .onRunCallback = f ;
71
63
}
72
64
73
- public void setOnPollCallback (IFn f ) {
74
- this .onPollCallback = f ;
75
- }
76
-
77
65
public IObj withMeta (IPersistentMap meta ) {
78
66
this .metadata = meta ;
79
67
return this ;
@@ -95,7 +83,7 @@ public CompletableFuture invoke(Object arg1) {
95
83
}
96
84
97
85
if (this .onQueueCallback != null ) {
98
- this .onQueueCallback .invoke ();
86
+ this .onQueueCallback .invoke (this );
99
87
}
100
88
101
89
this .executor .submit ((Runnable )this );
@@ -155,15 +143,6 @@ public void run() {
155
143
this .executor .submit (task );
156
144
}
157
145
}
158
-
159
- if (this .onRunCallback != null ) {
160
- var stats = PersistentArrayMap .EMPTY
161
- .assoc (KW_CURRENT_CONCURRENCY , this .getCurrentConcurrency ())
162
- .assoc (KW_CURRENT_QUEUE_SIZE , this .getCurrentQueueSize ())
163
- .assoc (KW_REMAINING_CONCURRENCY , this .getRemainingConcurrency ())
164
- .assoc (KW_REMAINING_QUEUE_SIZE , this .getRemainingQueueSize ());
165
- this .onRunCallback .invoke (stats );
166
- }
167
146
}
168
147
169
148
private static class Task implements Runnable {
@@ -187,8 +166,8 @@ public boolean isCancelled() {
187
166
188
167
@ SuppressWarnings ("unchecked" )
189
168
public void run () {
190
- if (this .limiter .onPollCallback != null ) {
191
- this .limiter .onPollCallback .invoke (this .createdAt );
169
+ if (this .limiter .onRunCallback != null ) {
170
+ this .limiter .onRunCallback .invoke (this . limiter , this .createdAt );
192
171
}
193
172
194
173
final CompletionStage future ;
@@ -217,7 +196,7 @@ public void run() {
217
196
}
218
197
}
219
198
220
- public static class CapacityLimitReachedException extends RuntimeException {
199
+ public static class CapacityLimitReachedException extends RuntimeException {
221
200
public CapacityLimitReachedException (String msg ) {
222
201
super (msg );
223
202
}
0 commit comments