haxl-2.1.2.0: A Haskell library for efficient, concurrent, and concise data access.

Safe HaskellNone
LanguageHaskell2010

Haxl.DataSource.ConcurrentIO

Description

A generic Haxl datasource for performing arbitrary IO concurrently. Every IO operation will be performed in a separate thread. You can use this with any kind of IO, but each different operation requires an instance of the ConcurrentIO class.

For example, to make a concurrent sleep operation:

sleep :: Int -> GenHaxl u w Int
sleep n = dataFetch (Sleep n)

data Sleep
instance ConcurrentIO Sleep where
  data ConcurrentIOReq Sleep a where
    Sleep :: Int -> ConcurrentIOReq Sleep Int

  performIO (Sleep n) = threadDelay (n*1000) >> return n

deriving instance Eq (ConcurrentIOReq Sleep a)
deriving instance Show (ConcurrentIOReq Sleep a)

instance ShowP (ConcurrentIOReq Sleep) where showp = show

instance Hashable (ConcurrentIOReq Sleep a) where
  hashWithSalt s (Sleep n) = hashWithSalt s n

Note that you can have any number of constructors in your ConcurrentIOReq GADT, so most of the boilerplate only needs to be written once.

Documentation

class ConcurrentIO tag where Source #

Associated Types

data ConcurrentIOReq tag a Source #

Methods

performIO :: ConcurrentIOReq tag a -> IO a Source #