PouchDB 中的最大实时复制数

Maximum number of live replications in PouchDB

我正在使用 PouchDB 开发一个 Ionic 应用程序,它需要将表与远程 CouchDB 服务器同步。 在我的 database.ts 提供者的构造函数中,我有 6 个方法:

this.initialiseDB_Ord();
this.initialiseDB_IngProd();
this.initialiseDB_Ing();
this.initialiseDB_Prod();
this.initialiseDB_OrdProd();
this.initialiseDB_Ut();

这些方法中的每一个都执行以下操作(我选择第一个作为示例):

this._DB_Ord = new PouchDB('orders');
this._remoteDB_Ord = this.addressIP + '/orders';
this._syncOpts_Ord = {  live : true,
                        retry : true,
                        continuous : true};
this._DB_Ord.sync(this._remoteDB_Ord, this._syncOpts_Ord)
.on('change',(info) => {
     console.log('Handling syncing change');
     console.dir(info);
}).on('paused',(info)=> {
     console.log('Handling syncing pause');
     console.dir(info);
}).on('active', (info) => {
     console.log('Handling syncing resumption');
     console.dir(info);
}).on('denied', (err) =>{
     console.log('Handling syncing denied');
     console.dir(err);
}).on('complete', (info) =>{
     console.log('Handling syncing complete');
     console.dir(info);
}).on('error', (err)=>{
     console.log('Handling syncing error');
     console.dir(err);
});

然后我有一个 handleSyncing 方法如下:

handleSyncingUt() {
  this._DB_Ut.changes({
       since             : 'now',
       live              : true,
       include_docs      : true,
       attachments   : true
  })
  .on('change', (change) =>
  {
     console.log('Handling change');
     console.dir(change);
  })
  .on('complete', (info) =>
  {
     console.log('Changes complete');
     console.dir(info);
  })
  .on('error',  (err) =>
  {
     console.log('Changes error');
     console.log(err);
  });
}

如果我最多有 5 个数据库,它就可以正常工作。 添加第六个数据库时,它不会实时同步本地 pouchDB 和远程 couchDB,而是仅在首次打开应用程序时同步。

有人可以帮助我吗?

浏览器有最大数量 sockets per domain 和每页,而您已超过 live:true 个连接数。 Chrome 跨选项卡共享第一个限制,因此您可以验证较少连接的多个选项卡导致 chrome 中的问题,或者通过提高 firefox 的 about:config.

中的限制

您无法真正为普通用户修复此限制,需要调整您管理连接的方式(例如 worker 可以管理 chrome 中多个选项卡的一个数据库同步)和数据设计可能会更改为仅持续观察一个 "changes" 数据库,以了解何时 运行 live:false 传递给其他数据库。

@lossleader 关于 browser/WebView 中的最大连接数是正确的。在 Quizster, we have this same problem as Quizster has to sync as many as 10 PouchDB instances simultaneously. This is done using Howler 订阅一组数据库的更改。在对这些数据库之一进行更改后,Quizster 会在 PouchDB 实例和 CouchDB 集群之间发出一次性(非实时)同步。

更多背景知识:Quizster 必须将这么多 PouchDB 实例同步为:

  1. 这样做可以避免在数据库之间复制更多数据,从而降低共享数据的延迟
  2. Quizster 使用大量数据库视图来加速复制并保持较小的数据集大小。而且,每个视图都有效地指向它自己的 PouchDB 实例。

我计划尽快开源更多 Quizster 堆栈,并希望发布一些教程。

由于您使用的是 Ionic(它使用 browser/WebView 到 运行 您的应用程序,因此预计会有 5 个连接限制,这是一个非常疯狂的限制,对吧?但别担心,无需更改当前代码即可绕过此限制。

让我先解释一下为什么存在这个限制,然后你如何解决它。

您针对 CouchDB 服务器打开的每个 PouchDB 连续同步都算作一个单独的 HTTP 连接。由于 CouchDB 的集成 Web 服务器使用 HTTP 1.1 协议(有点旧),你会奇怪地受到 2 connections by the RFC 2616 or 6 connections (or more, depending on the browser/WebView version).

的限制

检查此 table 以了解针对同一域的 HTTP 1.1 并发连接限制:

IE 6 and 7:      2
IE 8:            6
IE 9:            6
IE 10:           8
IE 11:           8
Firefox 2:       2
Firefox 3:       6
Firefox 4 to 46: 6
Opera 9.63:      4
Opera 10:        8
Opera 11 and 12: 6
Chrome 1 and 2:  6
Chrome 3:        4
Chrome 4 to 23:  6
Safari 3 and 4:  4

如果您想深入了解 HTTP 1.1 限制的原因,check this article

这个问题的解决方案在于 HTTP/2,更新版本的 HTTP 家族协议。 HTTP/2 通过在单个 TCP 连接上多路复用它们来解决并发 HTTP 事务限制。

因为你不能让 CouchDB 服务器自己说话 HTTP/2,你可以放置一个反向代理(HAProxy 或 NGINX)或 API 网关(OpenResty,Kong,Spring 云网关, Netflix Zuul) 并配置代理以服务 HTTP/2 个端点。简而言之,我们有:

IonicApp + PouchDB <--HTTP/2--> Reverse Proxy <--HTTP 1.1--> CouchDB

HAProxy 配置示例 [1]

NGINX 配置示例 [1]