The only way of entry-server programming Netty

Disclaimer: if not more, in essence https://blog.csdn.net/qq_29857681/article/details/90475138

First, understand the concept (the concept of this kind of thing, personal humble opinion, the first pass is certainly not read and want to read it to look different blog to see more to understand a)

  1. Asynchronous callbacks
    1. In fact, the Observer pattern
    2. While the caller initiation request to pass a callback function Processor callback function is called after processing the request formed asynchronous callback
  2. Event-driven
    1. First registration event type, and then wait for the callback function corresponding to the type of event occurs, will call the callback function
  3. NIO
    1. Synchronous non-blocking IO
    2. Linux is a kernel event table, OS has been polling the event table, when there is a corresponding event callback user program
    3. A reduced copy data from user space to kernel space
  4. IO multiplexing
    1. select
    2. poll
    3. epoll ( Redis Nginx and underlying all of this is currently the most popular Linux IO model )
      1. Level trigger (LT): default mode, that is, when epoll_wait detects an event descriptor is ready and notify the application, the application can not handle the event immediately; the next call epoll_wait, you will be notified of this event again
      2. Edge-triggered (ET): When epoll_wait detects an event descriptor is ready and notify the application, the application must handle the event immediately. If untreated, the next time you call epoll_wait, will not notice this event again. (Until you do a certain operation resulted in the descriptor is not ready to become a state, that is edge-triggered notification only when the state becomes ready by the not-ready only once).
      3. LT and ET was supposed to be a pulse signal, which may use it to explain more vivid. Level and Edge refers to the trigger point, as long as the Level is level, then it has been triggered, while Edge was triggered when the rising and falling edges. For example: 0-> 1 is Edge, 1-> 1 is Level.

        ET model epoll considerably reduced the number of trigger events, and therefore more efficient than the next LT mode.

          select poll epoll
        Operation method Traversal Traversal Callback
        The underlying implementation Array List Hash table
        IO efficiency Linear traversing each call, the time complexity is O (n) Linear traversing each call, the time complexity is O (n) Event Notification way, whenever fd ready, the system registered callback function will be called, will be ready fd into readyList inside, the time complexity of O (1)
        Maximum number of connections 1024 (x86) or 2048 (x64) unlimited unlimited
        fd copy Each call to select, from the set of fd need to copy the user mode to kernel mode Each call poll, from the need to set fd copy to user mode kernel mode Copied into the kernel when calling epoll_ctl and saved after each epoll_wait not copy
  5. React model
    1. Thread pool model is generally divided into two types: L / F leader and follower mode, HS / HA semi-synchronous / semi-asynchronous mode. 
      HS / HA  semi-synchronous /  semi-asynchronous mode  : three layers, the synchronization layer, a queue layer, an asynchronous layer, also known as producer-consumer model, main thread I / O event queue and then go down to lose the data parsing, and consumers read data processing application logic;
      advantages: simplifies the programming of the lower layer asynchronous I / O and high sync separation application services, and does not decrease the performance of the underlying service. The interlayer communication concentrator. 
      Disadvantages: the need to transfer data between the threads, thereby bringing the dynamic memory allocation, copying the data, the context switching overhead caused. High-level of service do not benefit from the underlying asynchronous service efficiency. 
      L / F  leader follower mode  : the LF thread pool, the thread can be in one of three thread states: leader, follower or processor. Leader thread is responsible for monitoring the state tb network port, when a message arrives, the message thread is responsible for the separation, and the follower from the state in accordance with a mechanism such as a thread based on the priority FIFO or the like to select as a new leader, and then to set up their own state to allocate processor and handle the event. After processing thread follower own state to state to wait again become the leader. At the same time the entire thread pool thread can be in only one state leader, which guarantees the same event multiple threads will not be repeated. 
      Disadvantages: implementation complexity and lack of flexibility; 

Code entry: http://www.cnblogs.com/applerosa/p/7141684.html

Guess you like

Origin blog.csdn.net/qq_29857681/article/details/90475138