{-# LANGUAGE CPP #-}{-# LANGUAGE FlexibleContexts #-}-- | Allocate resources from a pool, guaranteeing resource handling via the-- ResourceT transformer.moduleData.Conduit.Pool(ManagedResource(..),takeResource,takeResourceCheck,P.Pool,P.createPool,P.withResource,withResourceTimeout,withResourceT)whereimportqualifiedData.PoolasPimportControl.Monad(liftM)importControl.Monad.Trans.ResourceimportControl.Monad.IO.Class(liftIO)importqualifiedData.IORefasIimportControl.Exception(onException,mask)importSystem.Timeout(timeout)importControl.Monad.Trans.Control(control)-- | The result of taking a resource.dataManagedResourcema=ManagedResource{mrValue::a-- ^ The actual resource.,mrReuse::Bool->m()-- ^ Let's you specify whether the resource should be returned to the pool-- (via 'P.putResource') or destroyed (via 'P.destroyResource') on release.-- This defaults to destruction, in case of exceptions.,mrRelease::m()-- ^ Release this resource, either destroying it or returning it to the-- pool.}-- | Like 'P.withResource', but uses 'MonadResource' instead of 'MonadBaseControl'.---- Since 0.1.1withResourceT::MonadResourcem=>P.Poola->(a->mb)->mbwithResourceTpoolf=domr<-takeResourcepoolb<-f$mrValuemrmrReusemrTruemrReleasemrreturnb-- | Like 'P.withResource', but times out the operation if resource-- allocation does not complete within the given timeout period.---- Since 0.1.2withResourceTimeout::#if MIN_VERSION_monad_control(0,3,0)(MonadBaseControlIOm)#else(MonadControlIOm)#endif=>Int-- ^ Timeout period in microseconds->P.Poola->(a->mb)->m(Maybeb){-# SPECIALIZE withResourceTimeout :: Int -> P.Pool a -> (a -> IO b) -> IO (Maybe b) #-}withResourceTimeoutmspoolact=control$\runInIO->mask$\restore->domres<-timeoutms$P.takeResourcepoolcasemresofNothing->runInIO$returnNothingJust(resource,local)->doret<-restore(runInIO(liftMJust$actresource))`onException`P.destroyResourcepoollocalresourceP.putResourcelocalresourcereturnret#if __GLASGOW_HASKELL__ >= 700{-# INLINABLE withResourceTimeout #-}#endif-- | Take a resource from the pool and register a release action.takeResource::MonadResourcem=>P.Poola->m(ManagedResourcema)takeResourcepool=doonRelRef<-liftIO$I.newIORefFalse(relKey,(a,_))<-allocate(P.takeResourcepool)(\(a,local)->doonRel<-I.readIORefonRelRefifonRelthenP.putResourcelocalaelseP.destroyResourcepoollocala)returnManagedResource{mrValue=a,mrReuse=liftIO.I.writeIORefonRelRef,mrRelease=releaserelKey}-- | Same as 'takeResource', but apply some action to check if a resource is-- still valid.takeResourceCheck::MonadResourcem=>P.Poola->(a->mBool)->m(ManagedResourcema)takeResourceCheckpoolcheck=domr<-takeResourcepoolisValid<-check$mrValuemrifisValidthenreturnmrelsedomrReleasemrtakeResourceCheckpoolcheck