在 Data.Pool 中,如果创建新资源的操作抛出异常会怎样?
In Data.Pool, what happens if the action that creates a new resource throws an exception?
在Data.Pool中,createPool
的第一个参数是"action that creates a new resource"。
如果此操作抛出异常会怎样?我可以控制发生的事情吗? (恐怕它会进入无限循环,不断尝试创建新资源。)
(对于 withResource
函数,他们说:"If the action throws an exception of any type, the resource is destroyed, and not returned to the pool." 但这似乎是完全不同的事情。这似乎发生在 资源具有已经成功创建。所以我想这不是我上述问题答案的线索。)
在您 takeResource
或 tryTakeResource
并且池中没有资源之前,不会创建资源。如果创建资源的操作抛出异常,则池保持不变,并在调用代码中引发异常。如果您重复调用 takeResource
或 tryTakeResource
,即使之前的调用已抛出异常,创建操作也会重复执行。 withResource
调用 takeResource
而不处理任何异常;如果创建资源的操作在 takeResoure
中抛出异常,则将在调用 withResource
.
的代码中引发异常
创建资源的代码在takeResource
and tryTakeResource
中。它们的区别在于当used == maxResources
takeResource
retry
s; tryTakeResouce
returns Nothing
.
当没有可用条目且 used
不是 maxResources
时,它们都会执行以下操作:
create `onException` atomically (modifyTVar_ inUse (subtract 1))
当创建资源时发生异常时,资源数量 inUse
不会改变,并且在调用 takeResource
或 tryTakeResource
.[=33= 的代码中引发异常]
在Data.Pool中,createPool
的第一个参数是"action that creates a new resource"。
如果此操作抛出异常会怎样?我可以控制发生的事情吗? (恐怕它会进入无限循环,不断尝试创建新资源。)
(对于 withResource
函数,他们说:"If the action throws an exception of any type, the resource is destroyed, and not returned to the pool." 但这似乎是完全不同的事情。这似乎发生在 资源具有已经成功创建。所以我想这不是我上述问题答案的线索。)
在您 takeResource
或 tryTakeResource
并且池中没有资源之前,不会创建资源。如果创建资源的操作抛出异常,则池保持不变,并在调用代码中引发异常。如果您重复调用 takeResource
或 tryTakeResource
,即使之前的调用已抛出异常,创建操作也会重复执行。 withResource
调用 takeResource
而不处理任何异常;如果创建资源的操作在 takeResoure
中抛出异常,则将在调用 withResource
.
创建资源的代码在takeResource
and tryTakeResource
中。它们的区别在于当used == maxResources
takeResource
retry
s; tryTakeResouce
returns Nothing
.
当没有可用条目且 used
不是 maxResources
时,它们都会执行以下操作:
create `onException` atomically (modifyTVar_ inUse (subtract 1))
当创建资源时发生异常时,资源数量 inUse
不会改变,并且在调用 takeResource
或 tryTakeResource
.[=33= 的代码中引发异常]