|
1 | 1 | local cjson = require "cjson" |
2 | 2 | local http = require "resty.http" |
| 3 | +local stringx = require "pl.stringx" |
3 | 4 | local types = require "pl.types" |
4 | 5 |
|
5 | 6 | local is_empty = types.is_empty |
| 7 | +local startswith = stringx.startswith |
6 | 8 |
|
7 | 9 | local _M = {} |
8 | 10 |
|
@@ -68,22 +70,39 @@ local function perform_query(path, query_options, http_options) |
68 | 70 |
|
69 | 71 | local response, err = try_query(path, http_options) |
70 | 72 |
|
71 | | - -- If we get an "EOF" error from Mora, this means our query occurred during |
72 | | - -- the middle of a server or replicaset change. In this case, retry the |
73 | | - -- request a couple more times. |
| 73 | + -- If we certain types of errors from Mora, this means our query occurred |
| 74 | + -- during the middle of a server or replicaset change. In this case, retry |
| 75 | + -- the request a few more times. |
74 | 76 | -- |
75 | 77 | -- This should be less likely in mora since |
76 | 78 | -- https://github.com/emicklei/mora/pull/29, but it's still possible for this |
77 | 79 | -- to crop up if the socket gets closed sometime between the request starting |
78 | | - -- and the query actually executing. After more research, this seems to be |
| 80 | + -- and the query actually executing. This can also happen in case of |
| 81 | + -- unexpected mongod shutdowns. After more research, this seems to be |
79 | 82 | -- expected mgo behavior, and it's up to the app to handle these type of |
80 | 83 | -- errors. I'm not entirely sure whether we should try to address the issue |
81 | 84 | -- in mora itself, but in the meantime, we'll retry here. |
82 | | - if err and err == "mongodb error: EOF" then |
83 | | - response, err = try_query(path, http_options) |
84 | | - if err and err == "mongodb error: EOF" then |
85 | | - ngx.sleep(0.5) |
86 | | - response, err = try_query(path, http_options) |
| 85 | + if err then |
| 86 | + -- Loop to retry a few times until no errors occurs or we give up, since we |
| 87 | + -- don't want to wait forever. |
| 88 | + local retries = 0 |
| 89 | + while err and retries < 5 do |
| 90 | + if err == "mongodb error: EOF" |
| 91 | + or err == "mongodb error: node is recovering" |
| 92 | + or err == "mongodb error: interrupted at shutdown" |
| 93 | + or err == "mongodb error: Closed explicitly" |
| 94 | + or startswith(err, "mongodb error: read tcp") |
| 95 | + then |
| 96 | + -- Retry immediately, then sleep between further retries. |
| 97 | + retries = retries + 1 |
| 98 | + if retries > 1 then |
| 99 | + ngx.sleep(0.5) |
| 100 | + end |
| 101 | + |
| 102 | + response, err = try_query(path, http_options) |
| 103 | + else |
| 104 | + break |
| 105 | + end |
87 | 106 | end |
88 | 107 | end |
89 | 108 |
|
|
0 commit comments