Design: How to Solve 503 Error of API when Resources are not ava

  • 时间:2020-09-08 11:19:41
  • 分类:网络文摘
  • 阅读:110 次

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) —

推荐阅读:
数学题:他们到达A、B两地的中点C地时都会提速20%  一个数的近似值是20万,这个数最大是多少?最小是多少?  图中有多少个长方形  一块正方形的纸板(如图),先剪下宽7厘米的长方形  数学题:9个队员进行单循环制猜丁壳比赛  数学题:有三位登山者要攀登一座荒无人烟的大山。出发时每人只能携带够6天的食物  数学题:一个多位数四舍五入后是1亿,这个数最小是多少?  新网站优化对于一个企业来说到底有多重要  大学生如何在渗透测试行业立足  网站渗透测试中的历程经验记录分析 
评论列表
添加评论