简体   繁体   English

如何防止从 close_wait 连接打开太多文件

[英]How prevent too many file open from close_wait connections

My program is fetching some images on a min.io server via their Java SDK .我的程序通过他们的Java SDK在 min.io 服务器上获取一些图像。

The issue is that even after inputStream.close() the connections remain open from the java code.问题是即使在inputStream.close()之后,java 代码中的连接仍然保持打开状态。 I can see it with lsof -p <PID> .我可以用lsof -p <PID>看到它。

After a while, it disappears but sometimes it does not, I guess fast enough, and the java server throws some too many open files errors.过了一会儿,它消失了,但有时它不会消失,我猜速度够快,而且 java 服务器会抛出一些too many open files的错误。

Is there like a garbage collector that removes the connections from the operating system?有没有像垃圾收集器一样从操作系统中删除连接? How can I prevent these too many open files errors?如何防止这些too many open files错误?

Just in case, here is the code:以防万一,这里是代码:

public static byte[] getImageByImageBinaryId(String imagId) throws IOException {
    InputStream object = null;

    try {
        object = getMinioClientClient().getObject(ServerProperties.MINIO_BUCKET_NAME, imagId);
        return IOUtils.toByteArray(object);
    } catch (Exception e) {
        log.error(e);
    } finally {
        IOUtils.closeQuietly(object);
    }

    return null;
}

Internally minio-java uses OkHttp to make HTTP calls. minio-java 内部使用 OkHttp 进行 HTTP 调用。 OkHttp, like many Http clients, internally uses a connection pool to speed up repeated calls to the same location. OkHttp 与许多 Http 客户端一样,在内部使用连接池来加速对同一位置的重复调用。 If you need for connections to not persist you can pass in your own OkHttp client to one of the available constructors with your own pooling config but I do not recommend it.如果您需要不保持连接,您可以将您自己的 OkHttp 客户端传递给具有您自己的池配置的可用构造函数之一,但我不建议这样做。

Minio should probably expose a close method to clean up these resources but their expected use case probably involves clients living the whole life of your application. Minio 可能应该公开一种关闭方法来清理这些资源,但它们的预期用例可能涉及客户在应用程序的整个生命周期中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM