27
27
*
28
28
* @see BackPressureLimiter
29
29
*/
30
- public class BackPressureHandlerLimiter implements BatchAwareBackPressureHandler {
31
-
32
- /**
33
- * The {@link BatchAwareBackPressureHandler} which permits should be limited by the {@link #backPressureLimiter}.
34
- */
35
- private final BatchAwareBackPressureHandler backPressureHandler ;
30
+ public class BackPressureHandlerLimiter implements BatchAwareBackPressureHandler , IdentifiableContainerComponent {
36
31
37
32
/**
38
33
* The {@link BackPressureLimiter} which computes a limit on how many permits can be requested at a given moment.
@@ -59,50 +54,54 @@ public class BackPressureHandlerLimiter implements BatchAwareBackPressureHandler
59
54
60
55
private final ReducibleSemaphore semaphore = new ReducibleSemaphore (0 );
61
56
62
- public BackPressureHandlerLimiter (BatchAwareBackPressureHandler backPressureHandler ,
63
- BackPressureLimiter backPressureLimiter , Duration standbyLimitPollingInterval , Duration acquireTimeout ) {
64
- this .backPressureHandler = backPressureHandler ;
57
+ private final int batchSize ;
58
+
59
+ private String id ;
60
+
61
+ public BackPressureHandlerLimiter (BackPressureLimiter backPressureLimiter , Duration acquireTimeout ,
62
+ Duration standbyLimitPollingInterval , int batchSize ) {
65
63
this .backPressureLimiter = backPressureLimiter ;
66
64
this .acquireTimeout = acquireTimeout ;
67
65
this .standbyLimitPollingInterval = standbyLimitPollingInterval ;
66
+ this .batchSize = batchSize ;
68
67
}
69
68
70
69
@ Override
71
- public int requestBatch () throws InterruptedException {
72
- int permits = updatePermitsLimit ();
73
- int batchSize = getBatchSize ();
74
- if (permits < batchSize ) {
75
- return acquirePermits (permits , backPressureHandler ::request );
76
- }
77
- return acquirePermits (batchSize , p -> backPressureHandler .requestBatch ());
70
+ public void setId (String id ) {
71
+ this .id = id ;
78
72
}
79
73
80
74
@ Override
81
- public void releaseBatch () {
82
- semaphore .release (getBatchSize ());
83
- backPressureHandler .releaseBatch ();
75
+ public String getId () {
76
+ return id ;
84
77
}
85
78
86
79
@ Override
87
- public int getBatchSize () {
88
- return backPressureHandler . getBatchSize ( );
80
+ public int requestBatch () throws InterruptedException {
81
+ return request ( batchSize );
89
82
}
90
83
91
84
@ Override
92
85
public int request (int amount ) throws InterruptedException {
93
86
int permits = Math .min (updatePermitsLimit (), amount );
94
- return acquirePermits (permits , backPressureHandler ::request );
87
+ if (permits == 0 ) {
88
+ Thread .sleep (standbyLimitPollingInterval .toMillis ());
89
+ return 0 ;
90
+ }
91
+ if (semaphore .tryAcquire (permits , acquireTimeout .toMillis (), TimeUnit .MILLISECONDS )) {
92
+ return permits ;
93
+ }
94
+ return 0 ;
95
95
}
96
96
97
97
@ Override
98
- public void release (int amount ) {
98
+ public void release (int amount , ReleaseReason reason ) {
99
99
semaphore .release (amount );
100
- backPressureHandler .release (amount );
101
100
}
102
101
103
102
@ Override
104
103
public boolean drain (Duration timeout ) {
105
- return backPressureHandler . drain ( timeout ) ;
104
+ return true ;
106
105
}
107
106
108
107
private int updatePermitsLimit () {
@@ -120,25 +119,6 @@ else if (newLimit > oldLimit) {
120
119
});
121
120
}
122
121
123
- private interface PermitsRequester {
124
- int request (int amount ) throws InterruptedException ;
125
- }
126
-
127
- private int acquirePermits (int amount , PermitsRequester permitsRequester ) throws InterruptedException {
128
- if (amount == 0 ) {
129
- Thread .sleep (standbyLimitPollingInterval .toMillis ());
130
- return 0 ;
131
- }
132
- if (semaphore .tryAcquire (amount , acquireTimeout .toMillis (), TimeUnit .MILLISECONDS )) {
133
- int obtained = permitsRequester .request (amount );
134
- if (obtained < amount ) {
135
- semaphore .release (amount - obtained );
136
- }
137
- return obtained ;
138
- }
139
- return 0 ;
140
- }
141
-
142
122
private static class ReducibleSemaphore extends Semaphore {
143
123
144
124
ReducibleSemaphore (int permits ) {
0 commit comments