Design: How to Solve 503 Error of API when Resources are not ava
- 时间:2020-09-08 11:19:41
- 分类:网络文摘
- 阅读:121 次
The HTTP 503 Error indidicates the server is busy. It might be due to the fact that the servers are overloaded (high load average).
One of my API is designed to return 503 Error when the cached file is not available. It is designed as the follows:
1 2 3 4 | if (!is_file($file)) { throw_503(); die(); } |
if (!is_file($file)) {
throw_503();
die();
}The $file will be updated periodically e.g. every minute via crontab. When the OS is writing to that file, the is_file will return false because the file is in-use.
One way of solving this is to have multiple caching versions, to avoid the single point of failure. For example, if a file-1 is not available we can check for file-2. These two files should not be updated at the same time.
Another way to resolve this issue is to add a delay check (hacky solution), for example:
1 2 3 4 5 6 7 | if (!is_file($file)) { sleep(1); // delay check. } if (!is_file($file)) { throw_503(); die(); } |
if (!is_file($file)) {
sleep(1); // delay check.
}
if (!is_file($file)) {
throw_503();
die();
}–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:不同颜色的玉米其营养价值也各不相同 富含膳食纤维蔬菜之红薯的保健作用 红薯怎么吃润肠通便及红薯食用禁忌 吃海带的十大好处和海带的饮食禁忌 经常食用这三种饭有助于预防肠癌 保健食品需谨慎 所谓效果是心理作用 关于预防癌症的十条饮食和生活建议 春季喝蜂蜜功效胜补品抗过敏润肺止咳 这十种食用方法让蜂蜜营养又健康 婴幼儿等四类人群不宜食用蜂蜜
- 评论列表
-
- 添加评论