连接池 – OkHttp

huangapple go评论79阅读模式
英文:

Connection Pool - OkHttp

问题

我们在WAS环境中使用OkHttp。请帮忙解答以下问题:

  • 问题1: 在容器环境中,理想的连接池大小和Keep Alive设置应是多少?是否有任何计算公式可供使用?我们将使用OkHttp客户端连接到两个不同的URL。

  • 问题2: 我们不希望出现任何客户端端的连接失败。OkHttp如何处理旧的连接?我在OkHttp中没有看到任何检查旧连接的参数。

HTTP Java客户端中有一个参数用于启用检查旧连接的功能:

http.connection.stalecheck

我们使用的OkHttp客户端如下所示,请问是否有任何重要的配置我遗漏了?

new OkHttpClient.Builder()
              .readTimeout(10, TimeUnit.SECONDS)
              .retryOnConnectionFailure(false)
              .connectTimeOut(5, TimeUnit.SECONDS)
              .connectionPool(new ConnectionPool(10, 5, TimeUnit.SECONDS)
              .build();
英文:

We are using OkHttp in WAS environment Could you please help us with the below queries -:

  • Question 1: What should be the ideal connection pool size and keep Alive for a container environment , is there any formula to calculate it, we will be using Okhttp client to connect to two different URL's

  • Question 2: We don't want to have any client side failures , how does OkHttp handle stale connections , I don't see any parameter in OkHttp to check for stale connections?

HTTP Java client Java has this parameter to turn on stale connection check:

http.connection.stalecheck

We are using OkHttp client as mentioned below , is there any important configuration am I missing?

new OkHttpClient.Builder()
              .readTimeout(10,TimeUnit.SECONDS)
              .retryOnConnectionFailure(false)
              .connectTimeOut(5,TimeUnit.SECONDS)
              .connectionPool(new ConnectionPool(10,5,TimeUnit.SECONDS)
              .build();

答案1

得分: 5

理想的连接池大小和保持连接时间在容器环境中应该是多少呢?

以256作为起点。 这个大小足够大,可以获得很好的命中率,又足够小,不会引起内存占用过多的问题。

如果你经常连接到许多不同的主机,可能需要将连接池大小调大。如果你运行在非常小的容器中,或者对内存使用非常敏感,可能需要将连接池大小调小。

我们不希望有任何客户端的连接问题。

在 OkHttpClient.Builder 上有一个设置,叫做 retryOnConnectionFailure。默认情况下它是true,会自动处理客户端连接问题。如果你想要自己处理客户端连接问题,可以将其设置为false。

英文:

> What should be the ideal connection pool size and keep Alive for a container environment

Take 256 as a starting point. It's big enough that you'll get a good hit rate and small enough that you won't notice the memory used.

If you hit lots of different hosts frequently you may adjust it up. If you are running on very small containers or have memory sensitivity you may adjust it down.

> We dont want to have any client side failures

There's a setting on OkHttpClient.Builder, retryOnConnectionFailure. It's true by default and will handle client-side failures for you. If you did want to handle client-side failures you'd set this to false.

huangapple
  • 本文由 发表于 2020年7月23日 13:36:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63047533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定