Uncaught RedisException: socket error on read socket

問題描述

最近在使用redis的發佈訂閱系統來作公司項目的消息隊列。一臺服務器發佈消息,多臺服務器訂閱獲取消息。在系統使用高峯期上午10點左右偶發性的有些服務器的Redis連接被斷開並報錯:redis

Uncaught RedisException: socket error on read socket

查看redis日誌後發現這麼一條錯誤日誌:緩存

Client id=319669 addr=192.168.1.10:52846 fd=83 name= age=534515 idle=1 flags=N db=0 sub=2 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=882 omem=452122 events=rw cmd=subscribe scheduled to be closed ASAP for overcoming of output buffer limits

解決方式

錯誤日誌很明顯的說明對於即將關閉的這個連接有輸出緩衝區的限制。初步判斷是因爲發送消息的服務器產生的消息過多。訂閱的服務器來不及消費,並達到了系統輸出緩衝區大小的限制。從而關閉了這個連接。谷歌了一下發現redis.conf有以下的限制。服務器

# The client output buffer limits can be used to force disconnection of clients
# that are not reading data from the server fast enough for some reason (a
# common reason is that a Pub/Sub client can't consume messages as fast as the
# publisher can produce them).
#
# The limit can be set differently for the three different classes of clients:
#
# normal -> normal clients including MONITOR clients
# slave  -> slave clients
# pubsub -> clients subscribed to at least one pubsub channel or pattern
#
# The syntax of every client-output-buffer-limit directive is the following:
#
# client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>
#
# A client is immediately disconnected once the hard limit is reached, or if
# the soft limit is reached and remains reached for the specified number of
# seconds (continuously).
# So for instance if the hard limit is 32 megabytes and the soft limit is
# 16 megabytes / 10 seconds, the client will get disconnected immediately
# if the size of the output buffers reach 32 megabytes, but will also get
# disconnected if the client reaches 16 megabytes and continuously overcomes
# the limit for 10 seconds.
#
# By default normal clients are not limited because they don't receive data
# without asking (in a push way), but just after a request, so only
# asynchronous clients may create a scenario where data is requested faster
# than it can read.
#
# Instead there is a default limit for pubsub and slave clients, since
# subscribers and slaves receive data in a push fashion.
#
# Both the hard or the soft limit can be disabled by setting them to zero.
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

翻譯一下redis關於輸出緩衝區限制的註釋

# The client output buffer limits can be used to force disconnection of clients
客戶端輸出緩衝區限制可用於強制斷開客戶端
# that are not reading data from the server fast enough for some reason (a
# common reason is that a Pub/Sub client can't consume messages as fast as the
# publisher can produce them).
因爲某些緣由,不能很快地從服務器讀取數據(一個常見的緣由是,Pub/Sub 客戶端不能像發佈者那樣快速地消耗消息。)

# The limit can be set differently for the three different classes of clients:
對於三種不一樣類型的客戶端,能夠設置不一樣的限制:

# normal -> normal clients including MONITOR clients
 普通鏈接 
# slave  -> slave clients
與slave之間的鏈接
# pubsub -> clients subscribed to at least one pubsub channel or pattern
客戶端訂閱經過普通訂閱或者模式匹配訂閱了至少一個頻道的連接

# The syntax of every client-output-buffer-limit directive is the following:
每一個客戶端輸出緩衝區限制指令的語法以下:

# client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>
class:表示連接類型
hard:表示容許該連接類型客戶端的輸出緩存區大小
soft:跟seconds配合使用,容許在seconds秒內緩存區限制的內存大小。
seconds:配合soft使用。同上
# A client is immediately disconnected once the hard limit is reached, or if
# the soft limit is reached and remains reached for the specified number of
# seconds (continuously).
一旦達輸出緩衝區的內存大小達到了head limit(硬限制) 或者若是輸出緩衝區的內存大小達到了soft limit(軟限制)且持續時間達到了
seconds 客戶端當即斷連接
# By default normal clients are not limited because they don't receive data
# without asking (in a push way), but just after a request, so only
# asynchronous clients may create a scenario where data is requested faster
# than it can read

上面吧啦吧啦的一大堆是在說正常的客戶端不受限制,由於它們沒有在不請求的狀況下接受數據
(如get命令:發送一個請求到服務器端,而後接收服務器的數據)。
既然正常的客戶端不受限制那麼來看它的默認配置:client-output-buffer-limit normal 0 0 0。
能夠看出統統設置爲0是不受緩衝區大小的限制。
# Both the hard or the soft limit can be disabled by setting them to zero.
經過設置爲零,硬或軟限制均可以被禁用。

總結

經過調整pubsub的緩存區的限制能夠解決由於客戶端緩存區大小限制致使的redis連接斷開的問題。socket

相關文章
相關標籤/搜索