简体   繁体   中英

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 .

The issue is that even after inputStream.close() the connections remain open from the java code. I can see it with 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.

Is there like a garbage collector that removes the connections from the operating system? How can I prevent these too many open files errors?

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. OkHttp, like many Http clients, internally uses a connection pool to speed up repeated calls to the same location. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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