You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tz...@apache.org on 2020/09/15 08:29:27 UTC

[flink-statefun] 01/06: [FLINK-19220] Add a close method

This is an automated email from the ASF dual-hosted git repository.

tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink-statefun.git

commit d45c7be850ccd4c1653fee882db5a2be83783a92
Author: Igal Shilman <ig...@gmail.com>
AuthorDate: Mon Sep 14 17:15:01 2020 +0200

    [FLINK-19220] Add a close method
    
    This commit adds a helper method to the OkHttpUtils that closes
    the resources obtained by okhttp.
---
 .../statefun/flink/core/httpfn/OkHttpUtils.java    | 26 ++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/httpfn/OkHttpUtils.java b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/httpfn/OkHttpUtils.java
index e1f3814..feabfb2 100644
--- a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/httpfn/OkHttpUtils.java
+++ b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/httpfn/OkHttpUtils.java
@@ -17,11 +17,16 @@
 package org.apache.flink.statefun.flink.core.httpfn;
 
 import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
 import okhttp3.ConnectionPool;
 import okhttp3.Dispatcher;
 import okhttp3.OkHttpClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 final class OkHttpUtils {
+  private static final Logger LOG = LoggerFactory.getLogger(OkHttpUtils.class);
+
   private OkHttpUtils() {}
 
   static OkHttpClient newClient() {
@@ -39,4 +44,25 @@ final class OkHttpUtils {
         .retryOnConnectionFailure(true)
         .build();
   }
+
+  static void closeSilently(@Nullable OkHttpClient client) {
+    if (client == null) {
+      return;
+    }
+    final Dispatcher dispatcher = client.dispatcher();
+    try {
+      dispatcher.executorService().shutdownNow();
+    } catch (Throwable ignored) {
+    }
+    try {
+      dispatcher.cancelAll();
+    } catch (Throwable throwable) {
+      LOG.warn("Exception caught while trying to close the HTTP client", throwable);
+    }
+    try {
+      client.connectionPool().evictAll();
+    } catch (Throwable throwable) {
+      LOG.warn("Exception caught while trying to close the HTTP connection pool", throwable);
+    }
+  }
 }