当前位置:网站首页>Redis 异常 read error on connection 解决方案
Redis 异常 read error on connection 解决方案
2022-04-23 09:30:00 【潘广宇 de 小白笔记】
一、造成原因与场景:
在接口连接 redis 时,使用了 pconnect 进行 redis 长连接,在 Redis->auth() 中抛出异常:
PHP Fatal error: Uncaught exception 'RedisException' with message 'read error on connection'
注:由于 php-fpm 对连接的重用性,所以,当存在cgi进程管理器[如php-fpm,spawn-cgi],使用 pconnect 效率比 connect 高。但是由于每个持久连接会占用内存,当并发量很大的时,很可能造成内存吃紧
二、出现原因
通过 pconnect 的 redis 长连接不会主动释放,会一直保持在php-fpm进程中,等待完成下一个请求的任务,即使通过 close 也仅是使当前php不能再进行redis请求,但无法真正关闭redis长连接,连接在后续请求中仍然会被重用,直至fpm进程生命周期结束。
这样会出现以下两种情况:
1)原因1 使用了已经断开的连接
假如这个连接已经断开了,下个请求可能直接使用上个断开的连接,当 php-fpm 处理下一个请求 reuse 一个断开的连接可能导致 read error on connection 的异常,以下是 phpredis 源码的解释:
2)原因2:执行超时
a)客户端设置的超时时间过短导致的;
b)客户端未设置超时时间,但是服务端执行时间超过了默认超时时间设置
php 有一个配置项 default_socket_timeout 表示一个 socket 连接的超时时间,默认是60s,而phpredis 这个扩展的底层连接 redis 是使用的 socket,所以当一个连接在60秒内没有被使用的时候,下次连接就会抛出这个异常。
三、解决方案
1)如果是原因1:
长连接改为短连接 或者 在 pconnect 中增加超时时间
$this->redis->pconnect($host, $port, 2); //@todo 2秒后该连接超时,将使用新的长连接
2)如果是原因2:修改超时时间
(不推荐) 方案1:
php.ini 中设置
default_socket_timeout = -1
然后重启php-fpm
方案2:
动态修改 php.ini
ini_set('default_socket_timeout', -1); // 不超时
方案3:
设置 redis 的超时时间为不超时:
$this->redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
参考:
版权声明
本文为[潘广宇 de 小白笔记]所创,转载请带上原文链接,感谢
https://panguangyu.blog.csdn.net/article/details/124296439
边栏推荐
- Creation of raid0 and RAID5 and Simulation of how RAID5 works
- 数据清洗 ETL 工具Kettle的安装
- NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
- 机器学习(六)——贝叶斯分类器
- Three challenges that a successful Devops leader should be aware of
- 3、 6 [Verilog HDL] gate level modeling of basic knowledge
- MySQL of database -- Fundamentals
- [reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
- DVWA range practice
- 错题汇总1
猜你喜欢
NLLLoss+log_ SoftMax=CE_ Loss
501. 二叉搜索树中的众数
Chapter VIII project stakeholder management of information system project manager summary
Flink 流批一体在小米的实践
Kettle实验
Buuctf [actf2020 freshman competition] include1
MySQL of database -- basic common query commands
How to protect open source projects from supply chain attacks - Security Design (1)
Simple understanding of arguments in JS
成功的DevOps Leader 应该清楚的3个挑战
随机推荐
js 原型链的深入
What is monitoring intelligent playback and how to use intelligent playback to query video recording
Kettle experiment
108. Convert an ordered array into a binary search tree
npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’
Get trustedinstaller permission
Simple understanding of arguments in JS
Number of islands
Detailed explanation of delete, truncate and drop principles in MySQL database
Applet error: cannot read property'currenttarget'of undefined
Codeforces Round #784 (Div. 4)
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
Production practice elk
MySQL - Chapter 1 (data type 2)
How to render web pages
653. 两数之和 IV - 输入 BST
Where is int a = 1 stored
GoLand debug go use - white record
Leetcode0587. 安装栅栏(difficult)
Using sqlmap injection to obtain the account and password of the website administrator