NodeJS 和 RethinkDB - 侦听 table 更改(实时)时如何处理连接中断(连接重试)?
NodeJS and RethinkDB - How to handle connection interruption (connection retry) when listening for table changes (realtime)?
ReqlRuntimeError: Connection is closed in:
r.table("users").changes()
^^^^^^^^^^^^^^^^^^^^^^^^^^
at ReqlRuntimeError.ReqlError [as constructor] (/home/user/DEV/express-socketio/node_modules/rethinkdb/errors.js:23:13)
at new ReqlRuntimeError (/home/user/DEV/express-socketio/node_modules/rethinkdb/errors.js:90:51)
at mkErr (/home/user/DEV/express-socketio/node_modules/rethinkdb/util.js:177:10)
at Feed.IterableResult._promptNext (/home/user/DEV/express-socketio/node_modules/rethinkdb/cursor.js:169:16)
at Feed.IterableResult._addResponse (/home/user/DEV/express-socketio/node_modules/rethinkdb/cursor.js:84:12)
at TcpConnection.<anonymous> (/home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:360:22)
at TcpConnection.cancel (/home/user/DEV/express-socketio/node_modules/rethinkdb/util.js:26:16)
at TcpConnection.cancel (/home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:789:43)
at wrappedCb (/home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:270:17)
at /home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:280:18
at tryCatcher (/home/user/DEV/express-socketio/node_modules/bluebird/js/main/util.js:26:23)
at Promise._resolveFromResolver (/home/user/DEV/express-socketio/node_modules/bluebird/js/main/promise.js:483:31)
at new Promise (/home/user/DEV/express-socketio/node_modules/bluebird/js/main/promise.js:71:37)
at TcpConnection.<anonymous> (/home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:264:33)
at TcpConnection.close (/home/user/DEV/express-socketio/node_modules/rethinkdb/util.js:43:16)
at /home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:782:46
[ERROR] 22:55:08 ReqlRuntimeError: Connection is closed in:
r.table("users").changes()
^^^^^^^^^^^^^^^^^^^^^^^^^^
我在执行以下测试时遇到此错误:
- 我的 nodejs 正在侦听 table(实时),
- 然后我通过关闭 rethinkdb docker 容器来模拟连接中断
- 错误破坏了整个应用程序。
我正在寻找如何处理此类错误,以便应用程序知道连接已丢失,
但是,在某种程度上,在 X minutes/seconds/etc 一段时间后,尝试重新连接,甚至重新启动应用程序,我不知道...
我找到了这个 https://github.com/rethinkdb/rethinkdb/issues/4886,
但与避免应用程序崩溃或在连接丢失后尝试重新连接无关。
我该如何处理?有什么想法吗?
提前致谢。
example 不是那么完整。考虑在服务器上打开 websocket 时可能 运行 的代码:
// write every change to file
function onChange(err, cursor) {
// log every change to console
// Change format: https://rethinkdb.com/docs/changefeeds/javascript/
cursor.each(console.log);
// if file not open, open file
openFile()
// write changes to file
cursor.each(writeChangesToFile)
// if you decide you no longer want these changes
if (stopChangesNow) {
cursor.close() // https://rethinkdb.com/api/javascript/#close-cursor
cancel()
}
}
// stop writing changes
function cancel(stream) {
// close file opened above
closeFile()
}
try {
r.table('users').changes().run(conn, onChange)
} catch(e) {
cancel()
}
您需要 run()
以便您的应用程序可以处理更改。
除非您需要清理套接字、输出流、文件句柄等,否则您可能不需要 cancel()
函数。您需要 try/catch 以避免崩溃。
当 REST 服务器崩溃时,它只会终止正在进行的请求。
当 websocket 服务器崩溃时,它会终止所有会话,从而给更多用户带来问题。
ReqlRuntimeError: Connection is closed in:
r.table("users").changes()
^^^^^^^^^^^^^^^^^^^^^^^^^^
at ReqlRuntimeError.ReqlError [as constructor] (/home/user/DEV/express-socketio/node_modules/rethinkdb/errors.js:23:13)
at new ReqlRuntimeError (/home/user/DEV/express-socketio/node_modules/rethinkdb/errors.js:90:51)
at mkErr (/home/user/DEV/express-socketio/node_modules/rethinkdb/util.js:177:10)
at Feed.IterableResult._promptNext (/home/user/DEV/express-socketio/node_modules/rethinkdb/cursor.js:169:16)
at Feed.IterableResult._addResponse (/home/user/DEV/express-socketio/node_modules/rethinkdb/cursor.js:84:12)
at TcpConnection.<anonymous> (/home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:360:22)
at TcpConnection.cancel (/home/user/DEV/express-socketio/node_modules/rethinkdb/util.js:26:16)
at TcpConnection.cancel (/home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:789:43)
at wrappedCb (/home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:270:17)
at /home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:280:18
at tryCatcher (/home/user/DEV/express-socketio/node_modules/bluebird/js/main/util.js:26:23)
at Promise._resolveFromResolver (/home/user/DEV/express-socketio/node_modules/bluebird/js/main/promise.js:483:31)
at new Promise (/home/user/DEV/express-socketio/node_modules/bluebird/js/main/promise.js:71:37)
at TcpConnection.<anonymous> (/home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:264:33)
at TcpConnection.close (/home/user/DEV/express-socketio/node_modules/rethinkdb/util.js:43:16)
at /home/user/DEV/express-socketio/node_modules/rethinkdb/net.js:782:46
[ERROR] 22:55:08 ReqlRuntimeError: Connection is closed in:
r.table("users").changes()
^^^^^^^^^^^^^^^^^^^^^^^^^^
我在执行以下测试时遇到此错误:
- 我的 nodejs 正在侦听 table(实时),
- 然后我通过关闭 rethinkdb docker 容器来模拟连接中断
- 错误破坏了整个应用程序。
我正在寻找如何处理此类错误,以便应用程序知道连接已丢失, 但是,在某种程度上,在 X minutes/seconds/etc 一段时间后,尝试重新连接,甚至重新启动应用程序,我不知道...
我找到了这个 https://github.com/rethinkdb/rethinkdb/issues/4886, 但与避免应用程序崩溃或在连接丢失后尝试重新连接无关。
我该如何处理?有什么想法吗? 提前致谢。
example 不是那么完整。考虑在服务器上打开 websocket 时可能 运行 的代码:
// write every change to file
function onChange(err, cursor) {
// log every change to console
// Change format: https://rethinkdb.com/docs/changefeeds/javascript/
cursor.each(console.log);
// if file not open, open file
openFile()
// write changes to file
cursor.each(writeChangesToFile)
// if you decide you no longer want these changes
if (stopChangesNow) {
cursor.close() // https://rethinkdb.com/api/javascript/#close-cursor
cancel()
}
}
// stop writing changes
function cancel(stream) {
// close file opened above
closeFile()
}
try {
r.table('users').changes().run(conn, onChange)
} catch(e) {
cancel()
}
您需要 run()
以便您的应用程序可以处理更改。
除非您需要清理套接字、输出流、文件句柄等,否则您可能不需要 cancel()
函数。您需要 try/catch 以避免崩溃。
当 REST 服务器崩溃时,它只会终止正在进行的请求。
当 websocket 服务器崩溃时,它会终止所有会话,从而给更多用户带来问题。