Java.net.SocketException: Connection reset solution

  • 2020-04-01 01:35:13
  • OfStack

Since SEOTcs system updated the SEO scoring algorithm on November 24th, a problem that has been bothering me is that Java data job task will often report the following errors in the execution process:

"2011-12-03 18:00:32 DefaultHttpClient [INFO] I/O exception (java.net.SocketException) caught when processing request: Connection reset by peer: socket write error
DefaultHttpClient [INFO] Retrying request "...

For this reason, I searched some websites in both Chinese and English, searched every corner that I could find, and found the principle of this situation. The Java exception can happen in both the client and the server. There are two reasons for this exception:

1. If the Socket on one side is closed (or actively closed, or closed due to an exception exit), the other side still sends data, and the first packet sent raises the exception (Connect reset by peer).

2. One end exits without closing the Connection, and the other end throws the exception (Connection reset) if it is reading data from the Connection. In simple terms, this is caused by read and write operations after the connection is broken.

So I simply think that by setting some socket timeout time, we can solve:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201303/2013320141710998.png" >

But that's still the case.

This problem has been bothering me for several days, and I have been thinking and comparing tests every day to find out the reason for this code. I can't help but think, under the premise of the same number of keywords, why didn't there be any errors in the previous batch query ranking data, but recently there have been frequent errors, why? Is the requested interface site blocking our server IP? This is not a very good reason, must be somewhere in the program did not release the connection properly caused by the connection!

Guided by this idea, through several days of continuous fighting and practice, I finally found the essence of the problem today, which is caused by the timer method! Is such, this several days, I in manual trigger some batch task, found that under the condition of the filter ranking value is 100, Java java.net.SocketException: Connection reset the get blank brains has been thrown, and refresh particularly severe, after carefully controlled the timer of this code

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201303/2013320141849079.png" >

 

After, finally suddenly wake up, right! There is something wrong here. I will analyze it myself.

A function value, it returns the value of the, is a critical value, but I'm the timer method, to determine the value of the return if it is a critical value, will force it to continue in 10 seconds that method, and this method is to obtain a specific data source code in a page, each time the method will consume a few milliseconds, which is equivalent to in this period of time, is to establish a socket connection, but because it has been returned is the critical value, so this method will be kept in 10 seconds to establish a socket connection to obtain data, If this method each time is about 80 ms (after tests, each such method execution time for around 80 milliseconds), in 10 seconds time, will establish a 10 * 1000/80 = 125 socket connection, which will establish a socket connection 12.5 per second, and as a result of this is the process of the filter, multiple critical value will appear together in a row, so, in a short few seconds, the socket number of connections to the same web page to soar high, hundreds of thousands, even lead to wait for processing requests high number of connections:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201303/2013320141948904.png" >

Why will use this timer method to get a method to perform a few times more, reason is that in order to obtain a stable value of data, but now that I think about it, how the negative effects of the price, the effect is to be reckoned with, but after a few days of comprehensive analysis and test, finally found the culprit, problem solving, heart, suddenly at a draught, can set his mind at to sleep...


Related articles: