socket analysis

One: traditional socket analysis

1.

Server.accept(); Traditional sockets will block waiting for client requests, and will also block when reading.

InputStream.read(bytes) (read blocking)

The problem is that the original writing method is not multi-threaded concurrent, and it will not process new incoming requests when processing a blocking request. So how to solve the problem of multi-thread concurrency? The way to use the ExecutorService thread pool is one way.

Then the problem comes again. One waiter is allocated for one request. If such a resource consumption is used for long-connection services, it will be a headache. However, from the perspective of historical introduction, short connections can still be handled. For example, the previous tomcat used a question-and-answer format, and there was rarely an exclusive situation. One thread can handle multiple tasks.

Summary: There is only one client in a single thread, and there can be multiple clients in a thread pool, but it consumes a lot of resources and is not suitable for a long-connected server. The short-connection response mode such as tomcat can still be used.

2.NIO

initServer (serversocket binding port)

Then listen (the selector is blocked, the key is obtained, the selector key will be processed, a socket event will be registered, and the selector will not be blocked continuously)

ServerSocketChannel ServerSocket (restaurant)

SocketChannel Socket (client)

Selector is the core (waiter)

 

NIO can implement a single thread to serve multiple clients, and traditional IO is not suitable. Traditional IO is one customer corresponds to one waiter.

NIO can be understood in the way a restaurant waiter orders food, a waiter can be shared by multiple customers. As long as a waiter is constantly waiting to be called, receiving guests or wanting to take orders from guests. NIO is the basis of netty, otherwise netty is not easy to understand. It is the mind that understands, and nothing else.

SimpleChannelHandler handles the reception and distribution of messages, detects whether messages are malicious and frequently accessed, and controls business links such as offline exception handling and data cleaning. One of the boss and worker in the above figure is the listening port, and the other is the control selector.

The working principle of Netty's entire thread:

The Netty client writing method is similar to the server writing method. The client, the codec, cannot be disconnected if it is not connected, and will be directly closed. If it is connected, it is not just connected once.

Analyze the original code of netty, and the working principle of netty, you must know what it is and why.

Raw IO, a thread serving a socket client. NIO can serve multiple clients with one thread, and the selector can select multiple clients, as long as the selector opens the corresponding channel. I think it is to occupy the single-threaded long connection and not let it idle, make full use of it to monitor multiple possible requests, select the appropriate one, and then carry out business processing. Unlike IO, it is a one2one bound and occupied service.

NIO, 1 thread + selector serving multiple guests

The NIO problem, whether an IO is blocked does not depend on whether the execution stops there, but whether the data will be returned immediately, and selector.wakeup() can wake up, so there is basically no blocking. Selector allows attention to certain things, and things will only respond when they are concerned. (eg SelectorKey.Write).

 

Where are Netty, hadoop, dubbo, and akka used? The underlying RPC communication is basically based on netty. Some game servers have started to use netty.netty is a good framework for thread message data acquisition and so on. At first our NIO is not multi-threaded. We create a thread pool and throw it into the thread pool when processing. It is enough to get a thread pool for the operation mode of IO. ExecutorService es = Executors.newCachedThreadPool(); es.execute(new Runnable(){ public void run(){ handler(socket);}}); but an error is reported when multithreaded. A NIO has multiple selectors, and a selector can register multiple services. If the restaurant expands, a waiter can supervise and manage his area by slice, and a restaurant can be divided into multiple areas. A good concurrency idea is not to call tasks directly and actively but to push tasks into the task queue. The working principle of Netty is basically that its own object performs its own tasks, puts it into a queue task, takes out the corresponding task from the queue, registers it, and executes it after wakeup.

Netty source code analysis:

1. Look at the code directly into the project, then print something to keep track of. Process some task queues, processing, and distribution principles.

2. Learned the execution of the top of the stack, and learned the breakpoint setting properties. The first call is at the bottom of the stack.

3. Understand the basics first.

 

Netty recalls that when the client connects to the server, if the connection is successful, it will be disconnected and then closed. If there is no successful connection, it will be closed directly.

 

Thread pool: There are multiple queues to put threads inside, and they are executed concurrently.

1 htread + queue = a single thread thread pool = "thread safety, tasks are executed linearly and serially.

How to maintain single client and multiple connections?

A. Object pool

B. Object group

Where is the object pool used? When a single object is not thread-safe, or when multiple threads concurrency produce blocking effects, you can try to use the object pool design. For example, database connection pools are not thread-safe, or concurrency will cause blocking effects.

There is also a design called object group design. This method does not remove objects, so there is the possibility of multiple threads accessing the same object, so this object is required to have the ability to be concurrent. So is it thread-safe when the client uses the channel to write data? SingleThreadEventExecutor is a single thread + thread pool, which is executed serially, and there will be no concurrency problems.

         Using ip, there may be a possibility of disconnection and reconnection.

Netty heartbeat

1. The user has not read or written for a long time and needs to disconnect the session. At this time, check the state and learn idleStateHandler

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327012785&siteId=291194637