【发布时间】:2013-06-25 11:30:26
【问题描述】:
我在 Lwt_pool.create 的检查和验证功能上苦苦挣扎,在这里有一些问题。
val create :
int ->
?check:('a -> (bool -> unit) -> unit) ->
?validate:('a -> bool Lwt.t) -> (unit -> 'a Lwt.t) -> 'a t
首先,让我描述一下我的使用背景。
我希望使用Lwt_pool 来管理数据库连接池。数据库是MongoDB,驱动是我自己做的(Mongo.ml)。驱动其实很简单,就是一个TCP(Unix.file_descr)连接到MongoDB服务器,和服务器发送请求/接收响应。
`create n ?check ?validate f` creates a new pool with at most n members. f is the function to use to create a new pool member.
An element of the pool is validated by the optional validate function before its Lwt_pool.use. Invalid elements are re-created.
The optional function check is called after a use of an element failed. It must call its argument excatly one with true if the pool member is still valid and false otherwise.
以上是创建的文档
所以这是我的问题:
从文档中,我了解 validate 是在使用之前验证连接。
所以我的第一个问题是如何检查 Unix.file_descr 的可用性?我只知道为了检查它,我已经通过它发送了一些东西,对吧?但是如果我通过我的连接发送一些东西来检查,那么我想这会很紧急,而且我还是想通过Lwt_pool.use发送一些东西,为什么还要在使用前做类似的事情呢?
我的第二个问题是关于检查的。
所以check会在使用后使用。从文档中,我真的无法理解。 check 是一个以 my_db_connection (在我的情况下)和 a (fun b -> unit) 作为参数的函数。谁来提供(fun b -> unit)? Lwt_pool 本身有这样的功能吗?还是我应该提供?那该怎么办呢?
谢谢
【问题讨论】: