MonadResource
/ResourceT
is one way of acquiring scarce resources in a way that guarantees resources will be freed in the case of an exception. Another approach is the bracket
pattern, which is supported by Snap via the bracketSnap
function. You can use this to create the ResourceT
context needed by LevelDB:
import qualified Control.Monad.Trans.Resource as Res
bracketSnap Res.createInternalState Res.closeInternalState $ \resState -> do
let openAction = open "thedb" defaultOptions { createIfMissing = True }
db <- Res.runInternalState openAction resState
This could be made simpler with some changes in Snap and leveldb:
- Instead of only providing the
open
function, which presumes aMonadResource
context, there could be a function which returns aResource
value. I'm making this tweak in persistent for the 2.0 release. - Snap could provide support for either
MonadResource
or theResource
monad (two separate concepts with unfortunately similar names).