Singleton use of HttpClient in vert.x

Recently I noticed that in our server logs there were quite a few of Caused by: io.vertx.core.http.HttpClosedException: Connection was closed errors, the scenario was service A made an http API call to service B and finally A threw this HttpClosedException. This API call was actually going through AWS internal ALB (A -> AWS ALB -> B), I also searched ALB log, but could not even find any traffic going into service B.

We were using vert.x to implement our serviced. So I then reviewed our code implementation of HttpClient, it was implemented as a Singleton which was encapsulated into a shared library, every service project would make reference to this shared library and use the Singleton HttpClient instance for API calls. The implementation was something similar to the following:

public class MyHttpClient implements HttpClient {
  private HttpClient httpClient;
  private Context context;
  private static volatile MyHttpClient vertxHttpClient;

  private MyHttpClient(Vertx vertx) {
    WorkerVerticle workerVerticle = new WorkerVerticle();
    vertx.deployVerticle(workerVerticle,
      new DeploymentOptions().setWorker(true).setWorkerPoolSize(1));
    while (workerVerticle.getContext() == null) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        // Ignore the exception and continue wait for another 1s
      }
    }
    workerVerticle.getContext().runOnContext(e -> {
      final HttpClientOptions options = new HttpClientOptions()
        .setSsl(true)
        .setIdleTimeout(30)
        .setIdleTimeoutUnit(TimeUnit.SECONDS)
        .setKeepAliveTimeout(30)
        .setHttp2KeepAliveTimeout(30)
        .setConnectTimeout(30000)
        .setMaxPoolSize(32)
        .setHttp2MaxPoolSize(32);

      httpClient = vertx.createHttpClient(options);
    });
  }

  public static MyHttpClient getInstance() {
    MyHttpClient localRef = vertxHttpClient;
    if (localRef == null) {
      synchronized (MyHttpClient.class) {
        localRef = vertxHttpClient;
        if (localRef == null) {
          vertxHttpClient = localRef = new MyHttpClient(Vertx.currentContext().owner());
        }
      }
    }
    return localRef;
  }
}

And for each of the AbstractVerticle in each service, we used HttpClient like below:

public class MainVerticle extends AbstractVerticle {
  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    // ...
    this.httpClient = MyHttpClient.getInstance();
    // ...
  }
}

So, questions:

  • Is it safe or good practice to use Singleton instance of HttpClient in vert.x?
  • According to the official documentation, HttpClient can be shared between verticles, but the creation of the HttpClient should be outside of any of the verticles. So, in our implementation, we intended to reuse the HttpClient instance across multiple verticles to save the performance, but I am not sure if the above implementation of the Singleton pattern is correct
  • Any other suggestion on how we could implement and use singleton of HttpClient in vert.x in a right way?

Thanks.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật