我看到response.body()
未关闭时会引发连接泄漏警告。
下面是引发警告的代码片段。
Request request = (new okhttp3.Request.Builder()).get().url(httpUrlBuilder.build()).addHeader("Origin", this.requestUrl.getProtocol() + "://" + this.requestUrl.getHost() + ":" + this.requestUrl.getPort()).build();
this.webSocket = this.clonedClient.newWebSocket(request, new WebSocketListener() {
public void onOpen(WebSocket webSocket, Response response) {
if(response != null && response.body() != null) {
response.body().close();
}
WatchConnectionManager.logger.debug("WebSocket successfully opened");
WatchConnectionManager.this.webSocketRef.set(webSocket);
WatchConnectionManager.this.currentReconnectAttempt.set(0);
WatchConnectionManager.this.started.set(true);
WatchConnectionManager.this.queue.clear();
WatchConnectionManager.this.queue.add(Boolean.valueOf(true));
}
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
if(WatchConnectionManager.this.forceClosed.get()) {
WatchConnectionManager.logger.debug("Ignoring onFailure for already closed/closing websocket", t);
if(response != null && response.body() != null) {
response.body().close();
}
} else if(response != null && response.code() == 200) {
WatchConnectionManager.this.queue.add(new KubernetesClientException("Received 200 on websocket", response.code(), (Status)null));
response.body().close();
} else {
if(response != null) {
Status status = OperationSupport.createStatus(response);
if(response.body() != null) {
response.body().close();
}
WatchConnectionManager.logger.warn("Exec Failure: HTTP {}, Status: {} - {}", new Object[]{Integer.valueOf(response.code()), status.getCode(), status.getMessage(), t});
if(!WatchConnectionManager.this.started.get()) {
WatchConnectionManager.this.queue.clear();
WatchConnectionManager.this.queue.add(new KubernetesClientException(status));
}
} else {
WatchConnectionManager.logger.warn("Exec Failure", t);
if(!WatchConnectionManager.this.started.get()) {
WatchConnectionManager.this.queue.clear();
WatchConnectionManager.this.queue.add(new KubernetesClientException("Failed to start websocket", t));
}
}
if(WatchConnectionManager.this.currentReconnectAttempt.get() >= WatchConnectionManager.this.reconnectLimit && WatchConnectionManager.this.reconnectLimit >= 0) {
WatchConnectionManager.this.closeEvent(new KubernetesClientException("Connection failure", t));
} else {
WatchConnectionManager.this.scheduleReconnect();
}
}
}
有人可以指出错误可能在哪里以及代码中的任何潜在错误吗?
设置日志级别为FINE后的错误日志如下:
Feb 16, 2018 12:27:55 AM okhttp3.internal.platform.Platform log
WARNING: A connection to https://192.168.42.69:8443/ was leaked. Did you forget to close a response body?
java.lang.Throwable: response.body().close()
at okhttp3.internal.platform.Platform.getStackTraceForCloseable(Platform.java:143)
at okhttp3.RealCall.captureCallStackTrace(RealCall.java:78)
at okhttp3.RealCall.enqueue(RealCall.java:87)
at okhttp3.internal.ws.RealWebSocket.connect(RealWebSocket.java:180)
at okhttp3.OkHttpClient.newWebSocket(OkHttpClient.java:427)