I work on a client-server application for the first time using the Netty library, and if everything is easier with the server, it turned on and immediately waits for a connection, then with the client everything is different in my case. I do not know how best to make a request for technical information for work and a request for authorization of the user. I tried several options and in each I'm not sure.

  1. The most logical and simplest one that could have occurred to me was that when the program was started, one permanent connection was created, through which technical information was first requested, processed and then only, and if the user wants, you can authorize. The problem is only in one, it seems to me: it is not known how long it will take from the launch of the program to authorization, and all this time the connection should be established, and if the user launches many instances of the program, then what? - a lot of connections, an extra load and a timeout for the answer can not be put ...
  2. Attempting to avoid problems from the first paragraph suggested another idea - to establish a connection and then terminate it after the completion of the request, but this option immediately disappeared, because you need to get an answer through this connection. Yes, you can set a timeout to wait for a response, say, 5 seconds and not terminate the connection manually, and the problem with receiving the packet disappears, and a third appears ... it would be more logical to use an already established connection, if it is still alive than each just create a new one and recreate it in the case of a timeout.

In general, I would be satisfied with the second option, but I have problems with its implementation. And what do you advise?

    1 answer 1

    You can close a connection if it has been inactive (not used) for a certain amount of time. This is a fairly standard solution used in connection pooling.

    • I wrote about this at the end of 2 points, but how do I know when the connection was completed on a time-out, and when it is still alive, so as not to create a new one? - user918231
    • @BRONNER, connection timeout is usually the time to establish a connection. So your question looks a bit confusing. The connection is alive if you have not closed it. Although the connection can be closed by a remote server. So connections need to be checked from time to time. See implementations of existing pools. - a_gura
    • @a_gura, yes, I was mistaken, reading timeout, of course, in netty, this is achieved using this class netty.io/4.0/api/io/netty/handler/timeout/… - user918231