Session 文件过多导致的 Laravel Maximum execution time of 30 seconds exceeded

现象#

系统
线上出现如下报错:

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Maximum execution time of 30 seconds exceeded' in /var/www/xxx.com/src/vendor/symfony/finder/Iterator/ExcludeDirectoryFilterIterator.php:74
Stack trace:
#0 {main}

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Maximum execution time of 30 seconds exceeded' in /var/www/xxx.com/src/vendor/nesbot/carbon/src/Carbon/Carbon.php:172
Stack trace:
#0 {main}

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Maximum execution time of 30 seconds exceeded' in /var/www/xxx.com/src/vendor/symfony/finder/Iterator/DateRangeFilterIterator.php:47
Stack trace:
#0 {main}

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Maximum execution time of 30 seconds exceeded' in /var/www/xxx.com/src/vendor/symfony/finder/Iterator/FileTypeFilterIterator.php:49
Stack trace:
#0 {main}

发生比较频繁,但不是每次请求都会出现。

分析#

具体的超时位置不确定,每次报错位置不固定,应该不是业务代码在哪里发生了处理超时。

后来根据代码追踪发现是Session读取造成的,在 storage/framework/sessions/ 一看,果然 ls 半天都出不来结果,最终出现的是有 175000+ 个文件。

laravel 默认使用的 session 驱动是file,会将所有的 session 文件存储在 storage/framework/sessions/ 目录下,由于之前为了偷懒不想登录将 session 的过期时间设置的较长,设置为1天,结果当访问量提升之后,会生成大量的session在一个文件夹下。

这样在 session 删除过期文件时扫描整个文件夹的时间会增长,从而造成超时。

解决#

  1. 设置 session 的 maxlifetime,修改小一些,可以是2小时。
  2. 推荐使用 memcache 等缓存技术来解决 session 共存的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值