Skip to content

Commit a4537b1

Browse files
committed
Accessors for configured send-time and buffer-size limit
Issue: SPR-16089
1 parent 48eb416 commit a4537b1

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecorator.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,13 +34,13 @@
3434
* Wrap a {@link org.springframework.web.socket.WebSocketSession WebSocketSession}
3535
* to guarantee only one thread can send messages at a time.
3636
*
37-
* <p>If a send is slow, subsequent attempts to send more messages from other
38-
* threads will not be able to acquire the flush lock and messages will be
39-
* buffered instead -- at that time, the specified buffer-size limit and
40-
* send-time limit will be checked and the session closed if the limits are
41-
* exceeded.
37+
* <p>If a send is slow, subsequent attempts to send more messages from other threads
38+
* will not be able to acquire the flush lock and messages will be buffered instead.
39+
* At that time, the specified buffer-size limit and send-time limit will be checked
40+
* and the session will be closed if the limits are exceeded.
4241
*
4342
* @author Rossen Stoyanchev
43+
* @author Juergen Hoeller
4444
* @since 4.0.3
4545
*/
4646
public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorator {
@@ -52,7 +52,6 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat
5252

5353
private final int bufferSizeLimit;
5454

55-
5655
private final Queue<WebSocketMessage<?>> buffer = new LinkedBlockingQueue<>();
5756

5857
private final AtomicInteger bufferSize = new AtomicInteger();
@@ -63,7 +62,6 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat
6362

6463
private volatile boolean closeInProgress;
6564

66-
6765
private final Lock flushLock = new ReentrantLock();
6866

6967
private final Lock closeLock = new ReentrantLock();
@@ -82,10 +80,33 @@ public ConcurrentWebSocketSessionDecorator(WebSocketSession delegate, int sendTi
8280
}
8381

8482

83+
/**
84+
* Return the configured send-time limit (milliseconds).
85+
* @since 4.3.13
86+
*/
87+
public int getSendTimeLimit() {
88+
return this.sendTimeLimit;
89+
}
90+
91+
/**
92+
* Return the configured buffer-size limit (number of bytes).
93+
* @since 4.3.13
94+
*/
95+
public int getBufferSizeLimit() {
96+
return this.bufferSizeLimit;
97+
}
98+
99+
/**
100+
* Return the current buffer size (number of bytes).
101+
*/
85102
public int getBufferSize() {
86103
return this.bufferSize.get();
87104
}
88105

106+
/**
107+
* Return the time (milliseconds) since the current send started,
108+
* or 0 if no send is currently in progress.
109+
*/
89110
public long getTimeSinceSendStarted() {
90111
long start = this.sendStartTime;
91112
return (start > 0 ? (System.currentTimeMillis() - start) : 0);
@@ -142,18 +163,18 @@ private boolean tryFlushMessageBuffer() throws IOException {
142163
return false;
143164
}
144165

145-
private void checkSessionLimits() throws IOException {
166+
private void checkSessionLimits() {
146167
if (!shouldNotSend() && this.closeLock.tryLock()) {
147168
try {
148169
if (getTimeSinceSendStarted() > this.sendTimeLimit) {
149170
String format = "Message send time %d (ms) for session '%s' exceeded the allowed limit %d";
150171
String reason = String.format(format, getTimeSinceSendStarted(), getId(), this.sendTimeLimit);
151-
setLimitExceeded(reason);
172+
limitExceeded(reason);
152173
}
153174
else if (this.bufferSize.get() > this.bufferSizeLimit) {
154175
String format = "The send buffer size %d bytes for session '%s' exceeded the allowed limit %d";
155176
String reason = String.format(format, this.bufferSize.get(), getId(), this.bufferSizeLimit);
156-
setLimitExceeded(reason);
177+
limitExceeded(reason);
157178
}
158179
}
159180
finally {
@@ -162,7 +183,7 @@ else if (this.bufferSize.get() > this.bufferSizeLimit) {
162183
}
163184
}
164185

165-
private void setLimitExceeded(String reason) {
186+
private void limitExceeded(String reason) {
166187
this.limitExceeded = true;
167188
throw new SessionLimitExceededException(reason, CloseStatus.SESSION_NOT_RELIABLE);
168189
}

0 commit comments

Comments
 (0)