socketio / socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
https://socketio.github.io/socket.io-client-java/installation.html
Other
5.31k stars 969 forks source link

Why can I only create 64 successful socket connections in version 1.0.2 #718

Closed erma66 closed 1 year ago

erma66 commented 2 years ago

When I tried to test how many socket connections I could create, I found that only 64 were connected and the rest were disconnected

        IO.Options options = new IO.Options();
        // IO factory options
        options.forceNew = false;
        options.multiplex = true;
        options.transports = new String[]{WebSocket.NAME};
        options.upgrade = true;
        options.rememberUpgrade = false;
        options.path = this.path;
        options.query = this.query;
        options.reconnection = true;
        options.reconnectionAttempts = Integer.MAX_VALUE;
        options.reconnectionDelay = 1000;
        options.reconnectionDelayMax = 5000;
        options.randomizationFactor = 0.5;
        options.timeout = 10000;
        try {
            socket = IO.socket(address, options);
            socket.on(listen, objects -> {
                log.debug("receiver message,{}", objects);
                onReceiver.accept(objects);
            });
            log.info("init socket success,query:{}", options.query);
            socket.connect();
        } catch (URISyntaxException e) {
            log.error("init socket success", e);
        } 
wf-zy commented 1 year ago

Did you find out why? It happened to me

erma66 commented 1 year ago

Unfortunately, I have not found the reason, the source code does not have the number 64

Did you find out why? It happened to me

wf-zy commented 1 year ago

But I found that it was possible to continue the connection by starting a new client

erma66 commented 1 year ago

You mean start a new process? The new process is not affected

wf-zy commented 1 year ago

yes,However, newly started clients can still only create 64 connections

erma66 commented 1 year ago

Well, I noticed that each socket Connection starts three threads, perhaps because of the thread count limit?

darrachequesne commented 1 year ago

Hi! I think that's because the maxRequests value of the underlying OkHttpClient defaults to 64: https://square.github.io/okhttp/4.x/okhttp/okhttp3/-dispatcher/max-requests/

You need to provide a custom dispatcher:


int MAX_CLIENTS = 100;

Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(MAX_CLIENTS * 2);
dispatcher.setMaxRequestsPerHost(MAX_CLIENTS * 2);

OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .dispatcher(dispatcher)
        .readTimeout(1, TimeUnit.MINUTES) // important for HTTP long-polling
        .build();

IO.Options options = new IO.Options();
options.callFactory = okHttpClient;
options.webSocketFactory = okHttpClient;

for (int i = 0; i < MAX_CLIENTS; i++) {
    Socket socket = IO.socket(URI.create("https://example.com"), options);
}

Reference: https://socketio.github.io/socket.io-client-java/faq.html#How_to_create_a_lot_of_clients

wf-zy commented 1 year ago

Thank you very much. That solves my problem