Skip to content

Commit

Permalink
Merge pull request #14 from MattGill98/PAYARA-3452-HTTP2-Memory-Leak-…
Browse files Browse the repository at this point in the history
…Cherry-Pick

PAYARA-3452 HTTP/2 Memory Leak Cherry Pick
  • Loading branch information
MattGill98 committed Feb 4, 2019
2 parents 9aab942 + 48d80f3 commit 421da5d
Showing 1 changed file with 10 additions and 16 deletions.
Expand Up @@ -19,12 +19,10 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -104,10 +102,7 @@ public class Http2Session {
private volatile FilterChain http2StreamChain;
private volatile FilterChain htt2SessionChain;

private static final AtomicIntegerFieldUpdater<Http2Session> concurrentStreamCountUpdater =
AtomicIntegerFieldUpdater.newUpdater(Http2Session.class, "concurrentStreamsCount");
@SuppressWarnings("unused")
private volatile int concurrentStreamsCount;
private final AtomicInteger concurrentStreamsCount = new AtomicInteger(0);

private final TreeMap<Integer, Http2Stream> streamsMap = new TreeMap<>();

Expand Down Expand Up @@ -654,7 +649,7 @@ private GoAwayFrame setGoAwayLocally(final ErrorCode errorCode,
? lastPeerStreamId
: 0));
if (goingAwayLastStreamId != Integer.MAX_VALUE) {
if (concurrentStreamsCount != 0) {
if (concurrentStreamsCount.get() > 0) {
pruneStreams();
}
}
Expand Down Expand Up @@ -1035,7 +1030,7 @@ Http2Stream acceptStream(final HttpRequestPacket request,
return null; // if the session is closed is set - return null to ignore stream creation
}

if (concurrentStreamsCount >= getLocalMaxConcurrentStreams()) {
if (concurrentStreamsCount.get() >= getLocalMaxConcurrentStreams()) {
// throw Session level exception because headers were not decompressed,
// so compression context is lost
throw new Http2SessionException(ErrorCode.REFUSED_STREAM);
Expand Down Expand Up @@ -1091,7 +1086,7 @@ public Http2Stream openStream(final HttpRequestPacket request,
ErrorCode.REFUSED_STREAM, "Session is closed");
}

if (concurrentStreamsCount >= getLocalMaxConcurrentStreams()) {
if (concurrentStreamsCount.get() >= getLocalMaxConcurrentStreams()) {
throw new Http2StreamException(streamId, ErrorCode.REFUSED_STREAM);
}

Expand Down Expand Up @@ -1195,12 +1190,11 @@ FilterChain getHttp2SessionChain() {
* Called from {@link Http2Stream} once stream is completely closed.
*/
void deregisterStream() {
decStreamCount();

final boolean isCloseSession;
synchronized (sessionLock) {
decStreamCount();
// If we're in GOAWAY state and there are no streams left - close this session
isCloseSession = isGoingAway() && concurrentStreamsCount == 0;
isCloseSession = isGoingAway() && concurrentStreamsCount.get() <= 0;
if (!isCloseSession) {
if (checkCount++ > http2Configuration.getCleanFrequencyCheck() && streamsMap.size() > streamsHighWaterMark) {
checkCount = 0;
Expand Down Expand Up @@ -1387,12 +1381,12 @@ private void registerUpgradeStream(final Http2Stream stream) throws Http2StreamE
}
}

void incStreamCount() {
concurrentStreamCountUpdater.incrementAndGet(this);
private void incStreamCount() {
concurrentStreamsCount.incrementAndGet();
}

void decStreamCount() {
concurrentStreamCountUpdater.decrementAndGet(this);
private void decStreamCount() {
concurrentStreamsCount.decrementAndGet();
}

private final class ConnectionCloseListener implements CloseListener<Closeable, CloseType> {
Expand Down

0 comments on commit 421da5d

Please sign in to comment.