管理多租户 ArangoDB 连接 [英] Manage multi-tenancy ArangoDB connection

查看:28
本文介绍了管理多租户 ArangoDB 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ArangoDB/Go(使用 go-driver)并且需要实现多租户,这意味着每个客户都将在一个单独的数据库中拥有他的数据.

I use ArangoDB/Go (using go-driver) and need to implement multi-tenancy, means every customer is going to have his data in a separate DB.

我想弄清楚的是如何使这种多租户工作.我知道为每个请求创建一个新的数据库连接是不可持续的,这意味着我必须维护一个连接池(不是典型的连接池).当然,我不能只假设我可以制作无限,必须有一个限制.然而,我想得越多,我就越明白我需要一些建议.我是 Go 的新手,来自 PHP 世界,显然它是 PHP 中完全不同的范式.

What I'm trying to figure out is how to make this multi-tenancy work. I understand that it's not sustainable to create a new DB connection for each request, means I have to maintain a pool of connections (not a typical connection pool tho). Of course, I can't just assume that I can make limitless, there has to be a limit. However, the more I think about that the more I understand that I need some advice on it. I'm new to Go, coming from the PHP world, and obviously it's a completely different paradigm in PHP.

一些细节我有一个 API(用 Go 编写),它使用 arangodb/go-driver 与 ArangoDb 对话.创建数据库连接的标准方法是

Some details I have an API (written in Go) which talks to ArangoDb using arangodb/go-driver. A standard way of creating a DB connection is

  • 建立连接conn, err := graphHTTP.NewConnection(...)

  • create a connection conn, err := graphHTTP.NewConnection(...)

创建客户端c, err := graphDriver.NewClient(...)

create client c, err := graphDriver.NewClient(...)

创建数据库连接graphDB,错误:= p.cl.Database(...)

create DB connection graphDB, err := p.cl.Database(...)

如果只有一个数据库,并且在 API 启动时创建数据库连接,则此方法有效.就我而言,它有很多,而且,正如之前所建议的,我需要维护一个数据库连接池.

This works if one has only one DB, and DB connection is created on API's boot up. In my case it's many, and, as previously suggested, I need to maintain a DB connections pool.

让我感到困惑的是如何维护这个池,记住池必须有一个限制.比如说,我的池的大小是 5,随着时间的推移,它已经被连接填满了.一个新请求进来了,它需要连接到不在池中的数据库.在我看来,我只有两个选择:

Where it gets fuzzy for me is how to maintain this pool, keep in mind that pool has to have a limit. Say, my pool is of size 5, abd over time it's been filled up with the connections. A new request comes in, and it needs a connection to a DB which is not in the pool. The way I see it, I have only 2 options:

  1. 杀死一个池连接,如果它没有被使用
  2. 等到 #1 可以完成,或者如果等待时间太长则抛出错误.

最大的不知道,这主要是因为我从来没有做过这样的事情,对我来说是如何跟踪是否正在使用连接.让事情变得更复杂的是,DB 连接有自己的池,它是在传输级别完成的.

The biggest unknow, and this is mainly because I've never done anything like this, for me is how to track whether connection is being used or not. What makes thing even more complex is that DB connection has it's own pool, it's done on the transport level.

有关如何处理此任务的任何建议?

Any recommendations on how to approach this task?

推荐答案

几个月前,我在 Java 概念验证 SaaS 应用程序中实现了这一点.

I implemented this in a Java proof of concept SaaS application a few months ago.

我的方法可以概括为:

  1. 创建一个并发队列来保存 Java 驱动程序实例(Java 驱动程序内置了连接池)
  2. 使用子域来确定正在使用哪个 SaaS 客户端(可以使用 URL 参数,但我不喜欢这种方法)
  3. 从基于 SaaS 客户端的队列中引用正确的连接,如果不在队列中,则创建一个新的连接.
  4. 继续请求.

通过命名每个数据库以匹配子域,这相当简单,但也可以使用从 _systemdb 中查找.

This was fairly trivial by naming each DB to match the subdomain, but a lookup from the _systemdb could also be used.

*编辑Concurrent Queue 每个数据库最多保存一个 Driver 对象,因此大小最多与数据库的数量相匹配.在我的测试中,我根本没有管理这个队列的大小.

*Edit The Concurrent Queue holds at most one Driver object per database and hence the size at most will match the number of databases. In my testing I did not manage the size of this Queue at all.

一个好的服务器应该能够容纳数百个甚至数千个,具体取决于内存,如果扩展足够大,可以使用负载平衡策略将客户端拆分为不同的服务器集群.工作线程也可用于根据年龄删除对象,但这可能会影响吞吐量.

A good server should be able to hold hundreds of these or even thousands depending on memory, and a load balancing strategy can be used to split clients into different server clusters if scaling large enough. A worker thread could also be used to remove objects based on age but that might interfere with throughput.

这篇关于管理多租户 ArangoDB 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆