10

http status code 404 使用陷阱

 2 years ago
source link: https://blog.csdn.net/oqqYuan1234567890/article/details/121432153
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

http status code 404 使用陷阱

original.png
皿小草 2021-11-19 22:36:14 642
分类专栏: golang 文章标签: 网络
专栏收录该内容
35 篇文章 0 订阅

最近遇到一个问题,业务逻辑里面需要检查资源是否存在。一般来说,如果返回码是 404, 就可以认为资源不存在,然后做出一些动作
但是,这个 404 不一定能反映真实情况

由于我们的 API 服务一般会部署在各种负载均衡或者网关之后,传输链路可能是这样的

Created with Raphaël 2.3.0 User Cloudflare Gateway API service

由于依赖链路过长,只要有一个环节异常,Cloudflare 与 Gateway 其实都有可能存在改写 status code 的情况。
最常见的就是 404, 有可能是 Cloudflare 没有找到网关,也有可能是网关没有找到服务。 而这个时候,你的服务可能是正常的,有可能是机房网络异常,或者一些不恰当的运维变更导致链路中断。

如果服务本身的 payload 是有一定特征的,比如返回的格式是 json, 里面有具体的响应码,这个时候就可以解析这个返回码,用于判断资源是否存在,可靠性比单纯使用一个 404 返回码来判断要高。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK