现象#
系统
线上出现如下报错:
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 删除过期文件时扫描整个文件夹的时间会增长,从而造成超时。
解决#
- 设置 session 的 maxlifetime,修改小一些,可以是2小时。
- 推荐使用 memcache 等缓存技术来解决 session 共存的问题。