🗒️nestjs fastify 频繁重启

type
status
slug
date
summary
tags
category
password
icon

背景

服务器大量报错
Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
The “[ERR_HTTP_HEADERS_SENT]: Can't set headers after they are sent to the client”

原因分析

  1. Authguard用法异常,直接返回 true,false 异常捕获处解决
  1. 程序异步异常,异步需要 try catch 不必在抛出异常
  1. fastify 框架异常 4.22 版本已经有修复 (npm-check-updates可以检测升级)
  1. fastify safewritehead 直接加一个 try catch,出现异常问题 return(使用 patch-package打补丁)推测 memjs 保存 session 后,已经返回了,或者是重复返回,如果业务能正常使用,这里 catch 到了之后打印一下异常即可;副作用是有些请求的 response 缺少 setcookie 返回
  1. k8s depolyment resource limit 和pm2的内存限制限制要匹配
 

解决方案

v10版本迁移

nestjs fastify
目前版本4.18.0我排查到 onsend 重复发送 ,会导致请求头设置二次报错(重启前会报很多这个错) 4.22.0 版本中有发布修复 fastify 重复发送问题

AuthGuard正确使用

authgurad返回true false接口,异常包装在异常类中处理
上一篇
npm install reason: certificate has expired
下一篇
Nestjs Fastify 上传文件
Loading...
文章列表
王小扬博客
云原生
Git
Elasticsearch
Apollo
产品
Think
生活技巧
软件开发
计算机网络
CI
DB
设计
缓存
Docker
Node
操作系统
Java
大前端
Nestjs
其他
PHP