A non-blocking socket writes in Java and blocks socket entries

Why does someone prefer to block records over non-blocking records? My understanding is that you only need to block recording if you want to make sure the other side received the TCP packet after returning the recording method, but I'm not even sure if this is possible. You will need to hide, and the flash will need to clear the buffer buffer of the underlying operating system. So, is there a drawback to non-blocking sockets? Does a large base write buffer buffer have a bad idea in terms of performance? I understand that the smaller the base socket write buffer, the more likely you are to hit the slow / buggy client and you need to drop / lower packets at the application level, while the socket base buffer is full and isWritable () returns false.

+6
source share
2 answers

I understand that you will only need to block recording if you want to make sure that the other side received the TCP packet after returning the recording method

Your understanding is wrong. This does not guarantee that.

Locking the recording block until all data is transferred to the socket send buffer, from where it will be transferred asynchronously to the network. If the reader is slow, its socket receive buffer will fill up, which will eventually lead to the socket send buffer being full, which will block the record, blocking the entire stream. Non-blocking I / O gives you the ability to detect and handle this situation.

+6
source

The problem with a non-blocking entry is that you may not have anything useful if the entry is incomplete. You can get loops like

// non-blocking write while(bb.remaining() > 0) sc.write(bb); 

OR

 // blocking write sc.write(bb); 

The former may burn the CPU, while the latter may be more desirable.

The big problem is being read. Once you decide whether you want to block or non-block reads, your records should be the same. Unfortunately, there is no way to make them different. If you want to non-block reads, you must have non-block records.

+3
source

Source: https://habr.com/ru/post/901251/


All Articles