超过ConcurrentWebSocketSessionDecorator bufferSizeLimit时如何刷新缓冲区?

时间:2018-08-07 09:31:29

标签: java spring websocket concurrency spring-websocket

我在我的项目中使用Spring Websocket,我发现ConcurrentWebSocketSessionDecorator在超过bufferSizeLimit时停止,但是没有办法恢复它。

我以这种方式使用它:

try {
    if (concurrentWebSocketSessionDecorator.isOpen()) {
        concurrentWebSocketSessionDecorator.sendMessage(message);
    }
} catch (SessionLimitExceededException se) {
    logger.warn("sendMessage exceed limit: ", se);
    // TODO flush buffer
} catch (IOException ie) {
    logger.error("sendMessage failed: ", ie);
}

我阅读了并发WebSocketSessionDecorator的代码,方法 checkSessionLimits 设置了 limitExceeded = true 并在出现以下情况时抛出SessionLimitExceededException缓冲区大小超出限制,但是我找不到设置 limitExceeded = false 的任何方法。如何刷新缓冲区并重置limitExceeded?

public void sendMessage(WebSocketMessage<?> message) throws IOException {
    if (shouldNotSend()) {
        return;
    }

    this.buffer.add(message);
    this.bufferSize.addAndGet(message.getPayloadLength());

    do {
        if (!tryFlushMessageBuffer()) {
            if (logger.isTraceEnabled()) {
                String text = String.format("Another send already in progress: " +
                        "session id '%s':, \"in-progress\" send time %d (ms), buffer size %d bytes",
                        getId(), getTimeSinceSendStarted(), getBufferSize());
                logger.trace(text);
            }
            checkSessionLimits();
            break;
        }
    }
    while (!this.buffer.isEmpty() && !shouldNotSend());
}

1 个答案:

答案 0 :(得分:0)

它会在已运行的tryFlushMessageBuffer()中自动刷新:

private boolean tryFlushMessageBuffer() throws IOException {
    if (this.flushLock.tryLock()) {
        try {
            while (true) {
                WebSocketMessage<?> message = this.buffer.poll();
                if (message == null || shouldNotSend()) {
                    break;
                }
                this.bufferSize.addAndGet(message.getPayloadLength() * -1);
                this.sendStartTime = System.currentTimeMillis();
                getDelegate().sendMessage(message);
                this.sendStartTime = 0;
            }
        }
        finally {
            this.sendStartTime = 0;
            this.flushLock.unlock();
        }
        return true;
    }
    return false;
}

我看到您对limitExceeded标志的看法:我们只是不去那个tryFlushMessageBuffer(),因为我们已经存在于shouldNotSend()之后。那是因为您对limitExceeded从未被重置是正确的。

听起来真的像个虫子。请提出有关此事的JIRA:https://jira.spring.io/projects/SPR