4
4
5
5
package org .opensmartgridplatform .throttling ;
6
6
7
+ import java .util .concurrent .TimeUnit ;
7
8
import lombok .extern .slf4j .Slf4j ;
8
9
import org .opensmartgridplatform .shared .wsheaderattribute .priority .MessagePriorityEnum ;
9
10
import org .opensmartgridplatform .throttling .model .NetworkSegment ;
11
+ import org .opensmartgridplatform .throttling .model .PermitRequest ;
10
12
import org .opensmartgridplatform .throttling .model .ThrottlingSettings ;
11
13
import org .opensmartgridplatform .throttling .services .PermitService ;
12
14
import org .opensmartgridplatform .throttling .services .RateLimitService ;
13
15
14
16
@ Slf4j
15
17
public class PermitsPerNetworkSegment {
18
+
16
19
private final PermitService permitService ;
17
20
private final RateLimitService rateLimitService ;
18
21
private final boolean highPrioPoolEnabled ;
19
22
private final int maxWaitForHighPrioInMs ;
23
+ private final int pauseWaitForHighPrioInMs ;
20
24
21
25
public PermitsPerNetworkSegment (
22
26
final PermitService permitService ,
23
27
final RateLimitService rateLimitService ,
24
28
final boolean highPrioPoolEnabled ,
25
- final int maxWaitForHighPrioInMs ) {
29
+ final int maxWaitForHighPrioInMs ,
30
+ final int pauseWaitForHighPrioInMs ) {
26
31
this .permitService = permitService ;
27
32
this .rateLimitService = rateLimitService ;
28
33
this .highPrioPoolEnabled = highPrioPoolEnabled ;
29
34
this .maxWaitForHighPrioInMs = maxWaitForHighPrioInMs ;
35
+ this .pauseWaitForHighPrioInMs = pauseWaitForHighPrioInMs ;
30
36
}
31
37
32
38
public boolean requestPermit (
33
39
final NetworkSegment networkSegment ,
34
- final int clientId ,
35
- final int requestId ,
40
+ final PermitRequest permitRequest ,
36
41
final int priority ,
37
42
final ThrottlingSettings throttlingSettings ) {
38
43
39
44
final boolean newConnectionRequestAllowed =
40
45
this .rateLimitService .isNewConnectionRequestAllowed (
41
46
networkSegment .baseTransceiverStationId (), networkSegment .cellId (), throttlingSettings );
42
47
43
- log .debug (
44
- "Request [{}] for permit is {} by rate-limiter" ,
45
- requestId ,
46
- newConnectionRequestAllowed ? "allowed" : "NOT allowed" );
47
-
48
48
if (newConnectionRequestAllowed ) {
49
- log .debug ("Request [{}] for permit is allowed by rate-limiter" , requestId );
49
+ log .debug ("Request [{}] for permit is allowed by rate-limiter" , permitRequest . getRequestId () );
50
50
return this .tryAcquiringPermit (
51
- networkSegment , clientId , requestId , priority , throttlingSettings .getMaxConcurrency ());
51
+ networkSegment , permitRequest , priority , throttlingSettings .getMaxConcurrency ());
52
52
}
53
53
54
- log .debug ("Request [{}] for permit is NOT allowed by rate-limiter" , requestId );
54
+ log .debug (
55
+ "Request [{}] for permit is NOT allowed by rate-limiter" , permitRequest .getRequestId ());
55
56
return false ;
56
57
}
57
58
58
59
public boolean releasePermit (
59
- final NetworkSegment networkSegment , final int clientId , final int requestId ) {
60
- return this .permitService .removePermit (networkSegment , clientId , requestId );
60
+ final NetworkSegment networkSegment , final PermitRequest permitRequest ) {
61
+ return this .permitService .removePermit (networkSegment , permitRequest );
61
62
}
62
63
63
64
private boolean tryAcquiringPermit (
64
65
final NetworkSegment networkSegment ,
65
- final int clientId ,
66
- final int requestId ,
66
+ final PermitRequest permitRequest ,
67
67
final int priority ,
68
68
final int maxConcurrency ) {
69
69
@@ -72,23 +72,26 @@ private boolean tryAcquiringPermit(
72
72
return false ;
73
73
}
74
74
75
+ final boolean highPrio =
76
+ this .highPrioPoolEnabled && priority > MessagePriorityEnum .DEFAULT .getPriority ();
77
+
75
78
final boolean granted =
76
- this .permitService .createPermit (networkSegment , clientId , requestId , maxConcurrency );
79
+ this .permitService .createPermit (networkSegment , permitRequest , maxConcurrency , highPrio );
77
80
78
81
if (granted ) {
79
- log .debug ("Request [{}] is granted a permit." , requestId );
82
+ log .debug ("Request [{}] is granted a permit." , permitRequest . getRequestId () );
80
83
return true ;
81
84
82
85
} else {
83
- log .debug ("Request [{}], is NOT granted a permit." , requestId );
86
+ log .debug ("Request [{}], is NOT granted a permit." , permitRequest . getRequestId () );
84
87
85
- if (this . highPrioPoolEnabled && priority > MessagePriorityEnum . DEFAULT . getPriority () ) {
88
+ if (highPrio ) {
86
89
log .debug (
87
90
"Request [{}] is a high priority request and high priority pool is enabled -> we will wait for a permit release..." ,
88
- requestId );
91
+ permitRequest . getRequestId () );
89
92
90
93
return this .waitUntilPermitIsAvailable (
91
- networkSegment , clientId , requestId , maxConcurrency , this .maxWaitForHighPrioInMs );
94
+ networkSegment , permitRequest , maxConcurrency , this .maxWaitForHighPrioInMs );
92
95
}
93
96
}
94
97
@@ -97,26 +100,35 @@ private boolean tryAcquiringPermit(
97
100
98
101
private boolean waitUntilPermitIsAvailable (
99
102
final NetworkSegment networkSegment ,
100
- final int clientId ,
101
- final int requestId ,
103
+ final PermitRequest permitRequest ,
102
104
final int maxConcurrency ,
103
105
final int maxWaitForHighPrioInMs ) {
104
106
105
107
final long startTime = System .currentTimeMillis ();
106
108
107
- log .debug ("High priority request [{}] is waiting until permit is available." , requestId );
109
+ log .debug (
110
+ "High priority request [{}] is waiting until permit is available." ,
111
+ permitRequest .getRequestId ());
108
112
109
113
while (System .currentTimeMillis () - startTime < maxWaitForHighPrioInMs ) {
110
114
111
115
final boolean granted =
112
- this .permitService .createPermitWithHighPriority (
113
- networkSegment , clientId , requestId , maxConcurrency );
116
+ this .permitService .createPermit (networkSegment , permitRequest , maxConcurrency , true );
114
117
115
118
if (!granted ) {
119
+
120
+ if (this .pauseWaitForHighPrioInMs > -1 ) {
121
+ try {
122
+ TimeUnit .MILLISECONDS .sleep (this .pauseWaitForHighPrioInMs );
123
+ } catch (final InterruptedException e ) {
124
+ Thread .currentThread ().interrupt ();
125
+ }
126
+ }
127
+
116
128
continue ;
117
129
}
118
130
119
- log .debug ("High priority request [{}] is granted a permit." , requestId );
131
+ log .debug ("High priority request [{}] is granted a permit." , permitRequest . getRequestId () );
120
132
return true ;
121
133
}
122
134
0 commit comments