haxl-2.4.0.0: A Haskell library for efficient, concurrent, and concise data access.
Safe HaskellNone
LanguageHaskell2010

Haxl.Core

Description

Everything needed to define data sources and to invoke the engine.

Synopsis

The monad and operations

newtype GenHaxl u w a Source #

The Haxl monad, which does several things:

  • It is a reader monad for Env, which contains the current state of the scheduler, including unfetched requests and the run queue of computations.
  • It is a writer monad for WriteTree. These can be used to do arbitrary "logs" from any Haxl computation. These are better than doing arbitrary IO from a Haxl computation as these writes also get memoized if the Haxl computation associated with them is memoized. Now if this memoized computation is run again, you'll get the writes twice.

Constructors

GenHaxl 

Fields

Instances

Instances details
Monad (GenHaxl u w) Source # 
Instance details

Defined in Haxl.Core.Monad

Methods

(>>=) :: GenHaxl u w a -> (a -> GenHaxl u w b) -> GenHaxl u w b #

(>>) :: GenHaxl u w a -> GenHaxl u w b -> GenHaxl u w b #

return :: a -> GenHaxl u w a #

Functor (GenHaxl u w) Source # 
Instance details

Defined in Haxl.Core.Monad

Methods

fmap :: (a -> b) -> GenHaxl u w a -> GenHaxl u w b #

(<$) :: a -> GenHaxl u w b -> GenHaxl u w a #

MonadFail (GenHaxl u w) Source # 
Instance details

Defined in Haxl.Core.Monad

Methods

fail :: String -> GenHaxl u w a #

Applicative (GenHaxl u w) Source # 
Instance details

Defined in Haxl.Core.Monad

Methods

pure :: a -> GenHaxl u w a #

(<*>) :: GenHaxl u w (a -> b) -> GenHaxl u w a -> GenHaxl u w b #

liftA2 :: (a -> b -> c) -> GenHaxl u w a -> GenHaxl u w b -> GenHaxl u w c #

(*>) :: GenHaxl u w a -> GenHaxl u w b -> GenHaxl u w b #

(<*) :: GenHaxl u w a -> GenHaxl u w b -> GenHaxl u w a #

MonadThrow (GenHaxl u w) Source #

Since: 0.3.1.0

Instance details

Defined in Haxl.Core.Monad

Methods

throwM :: Exception e => e -> GenHaxl u w a #

MonadCatch (GenHaxl u w) Source #

Since: 0.3.1.0

Instance details

Defined in Haxl.Core.Monad

Methods

catch :: Exception e => GenHaxl u w a -> (e -> GenHaxl u w a) -> GenHaxl u w a #

Fractional a => Fractional (GenHaxl u w a) Source # 
Instance details

Defined in Haxl.Prelude

Methods

(/) :: GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w a #

recip :: GenHaxl u w a -> GenHaxl u w a #

fromRational :: Rational -> GenHaxl u w a #

Num a => Num (GenHaxl u w a) Source # 
Instance details

Defined in Haxl.Prelude

Methods

(+) :: GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w a #

(-) :: GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w a #

(*) :: GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w a #

negate :: GenHaxl u w a -> GenHaxl u w a #

abs :: GenHaxl u w a -> GenHaxl u w a #

signum :: GenHaxl u w a -> GenHaxl u w a #

fromInteger :: Integer -> GenHaxl u w a #

IsString a => IsString (GenHaxl u w a) Source # 
Instance details

Defined in Haxl.Core.Monad

Methods

fromString :: String -> GenHaxl u w a #

Semigroup a => Semigroup (GenHaxl u w a) Source # 
Instance details

Defined in Haxl.Core.Monad

Methods

(<>) :: GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w a #

sconcat :: NonEmpty (GenHaxl u w a) -> GenHaxl u w a #

stimes :: Integral b => b -> GenHaxl u w a -> GenHaxl u w a #

Monoid a => Monoid (GenHaxl u w a) Source # 
Instance details

Defined in Haxl.Core.Monad

Methods

mempty :: GenHaxl u w a #

mappend :: GenHaxl u w a -> GenHaxl u w a -> GenHaxl u w a #

mconcat :: [GenHaxl u w a] -> GenHaxl u w a #

u1 ~ u2 => IfThenElse (GenHaxl u1 w Bool) (GenHaxl u2 w a) Source # 
Instance details

Defined in Haxl.Prelude

Methods

ifThenElse :: GenHaxl u1 w Bool -> GenHaxl u2 w a -> GenHaxl u2 w a -> GenHaxl u2 w a Source #

runHaxl :: forall u w a. Env u w -> GenHaxl u w a -> IO a Source #

Runs a Haxl computation in the given Env.

Note: to make multiple concurrent calls to runHaxl, each one must have a separate Env. A single Env must not be shared between multiple concurrent calls to runHaxl, otherwise deadlocks or worse will likely ensue.

However, multiple Envs may share a single StateStore, and thereby use the same set of datasources.

runHaxlWithWrites :: forall u w a. Env u w -> GenHaxl u w a -> IO (a, [w]) Source #

Env

data Env u w Source #

Constructors

Env 

Fields

  • dataCache :: !(HaxlDataCache u w)

    cached data fetches

  • memoCache :: !(HaxlDataCache u w)

    memoized computations

  • memoKey :: !CallId

    current running memo key

  • flags :: !Flags
     
  • userEnv :: u

    user-supplied data, retrievable with env

  • statsRef :: !(IORef Stats)

    statistics, collected according to the report level in flags.

  • statsBatchIdRef :: !(IORef Int)

    keeps track of a Unique ID for each batch dispatched with stats enabled, for aggregating after.

  • callIdRef :: !(IORef CallId)

    keeps track of a Unique ID for each fetch/memo.

  • profCurrent :: ProfileCurrent

    current profiling label, see withLabel

  • profRef :: !(IORef Profile)

    profiling data, collected according to the report level in flags.

  • states :: StateStore

    Data sources and other components can store their state in here. Items in this store must be instances of StateKey.

  • reqStoreRef :: !(IORef (RequestStore u))

    The set of requests that we have not submitted to data sources yet. Owned by the scheduler.

  • runQueueRef :: !(IORef (JobList u w))

    runnable computations. Things get added to here when we wake up a computation that was waiting for something. When the list is empty, either we're finished, or we're waiting for some data fetch to return.

  • submittedReqsRef :: !(IORef ReqCountMap)

    all outgone fetches which haven't yet returned. Entries are removed from this map as the fetches finish. This field is useful for tracking outgone fetches to detect downstream failures.

  • completions :: !(TVar [CompleteReq u w])

    Requests that have completed. Modified by data sources (via putResult) and the scheduler. Waiting for this list to become non-empty is how the scheduler blocks waiting for data fetches to return.

  • writeLogsRef :: !(IORef (WriteTree w))

    A log of all writes done as part of this haxl computation. Any haxl computation that needs to be memoized runs in its own environment so that we can get a hold of those writes and put them in the IVar associated with the compuatation.

  • writeLogsRefNoMemo :: !(IORef (WriteTree w))

    This is just a specialized version of writeLogsRef, where we put logs that user doesn't want memoized. This is a better alternative to doing arbitrary IO from a (memoized) Haxl computation.

  • dataCacheFetchFallback :: !(Maybe (DataCacheLookup w))

    Allows you to inject a DataCache lookup just before a dataFetch is dispatched. This is useful for injecting fetch results in testing.

type Caches u w = (HaxlDataCache u w, HaxlDataCache u w) Source #

caches :: Env u w -> Caches u w Source #

Operations in the monad

env :: (Env u w -> a) -> GenHaxl u w a Source #

Extracts data from the Env.

withEnv :: Env u w -> GenHaxl u w a -> GenHaxl u w a Source #

Returns a version of the Haxl computation which always uses the provided Env, ignoring the one specified by runHaxl.

withLabel :: ProfileLabel -> GenHaxl u w a -> GenHaxl u w a Source #

Label a computation so profiling data is attributed to the label.

Building the Env

initEnvWithData :: StateStore -> u -> Caches u w -> IO (Env u w) Source #

Initialize an environment with a StateStore, an input map, a preexisting DataCache, and a seed for the random number generator.

initEnv :: StateStore -> u -> IO (Env u w) Source #

Initializes an environment with StateStore and an input map.

emptyEnv :: u -> IO (Env u w) Source #

A new, empty environment.

sanitizeEnv :: Env u w -> IO (Env u w) Source #

If you're using the env from a failed Haxl computation in a second Haxl computation, it is recommended to sanitize the Env to remove all empty IVars - especially if it's possible the first Haxl computation could've been interrupted via an async exception. This is because if the Haxl computation was interrupted by an exception, it's possible that there are entries in the cache which are still blocked, while the results from outgone fetches have been discarded.

Building the StateStore

data StateStore Source #

The StateStore maps a StateKey to the State for that type.

stateGet :: forall r. StateKey r => StateStore -> Maybe (State r) Source #

Retrieves a State from the StateStore container.

stateSet :: forall f. StateKey f => State f -> StateStore -> StateStore Source #

Inserts a State in the StateStore container.

stateEmpty :: StateStore Source #

A StateStore with no entries.

Writes inside the monad

tellWrite :: w -> GenHaxl u w () Source #

Exceptions

throw :: Exception e => e -> GenHaxl u w a Source #

Throw an exception in the Haxl monad

catch :: Exception e => GenHaxl u w a -> (e -> GenHaxl u w a) -> GenHaxl u w a Source #

Catch an exception in the Haxl monad

catchIf :: Exception e => (e -> Bool) -> GenHaxl u w a -> (e -> GenHaxl u w a) -> GenHaxl u w a Source #

Catch exceptions that satisfy a predicate

try :: Exception e => GenHaxl u w a -> GenHaxl u w (Either e a) Source #

Returns Left e if the computation throws an exception e, or Right a if it returns a result a.

tryToHaxlException :: GenHaxl u w a -> GenHaxl u w (Either HaxlException a) Source #

Like try, but lifts all exceptions into the HaxlException hierarchy. Uses unsafeToHaxlException internally. Typically this is used at the top level of a Haxl computation, to ensure that all exceptions are caught.

Data fetching and caching

dataFetch :: (DataSource u r, Request r a) => r a -> GenHaxl u w a Source #

Performs actual fetching of data for a Request from a DataSource.

uncachedRequest :: forall a u w (r :: Type -> Type). (DataSource u r, Request r a) => r a -> GenHaxl u w a Source #

A data request that is not cached. This is not what you want for normal read requests, because then multiple identical requests may return different results, and this invalidates some of the properties that we expect Haxl computations to respect: that data fetches can be arbitrarily reordered, and identical requests can be commoned up, for example.

uncachedRequest is useful for performing writes, provided those are done in a safe way - that is, not mixed with reads that might conflict in the same Haxl computation.

if we are recording or running a test, we fallback to using dataFetch This allows us to store the request in the cache when recording, which allows a transparent run afterwards. Without this, the test would try to call the datasource during testing and that would be an exception.

cacheRequest :: Request req a => req a -> Either SomeException a -> GenHaxl u w () Source #

Inserts a request/result pair into the cache. Throws an exception if the request has already been issued, either via dataFetch or cacheRequest.

This can be used to pre-populate the cache when running tests, to avoid going to the actual data source and ensure that results are deterministic.

dupableCacheRequest :: Request req a => req a -> Either SomeException a -> GenHaxl u w () Source #

Similar to cacheRequest but doesn't throw an exception if the key already exists in the cache. If this function is called twice to cache the same Haxl request, the first value will be discarded and overwritten with the second value. Useful e.g. for unit tests

cacheResult :: Request r a => r a -> IO a -> GenHaxl u w a Source #

Transparently provides caching. Useful for datasources that can return immediately, but also caches values. Exceptions thrown by the IO operation (except for asynchronous exceptions) are propagated into the Haxl monad and can be caught by catch and try.

cacheResultWithShow :: (Eq (r a), Hashable (r a), Typeable (r a)) => ShowReq r a -> r a -> IO a -> GenHaxl u w a Source #

Transparently provides caching in the same way as cacheResult, but uses the given functions to show requests and their results.

cachedComputation :: forall req u w a. (Eq (req a), Hashable (req a), Typeable (req a)) => req a -> GenHaxl u w a -> GenHaxl u w a Source #

cachedComputation memoizes a Haxl computation. The key is a request.

Note: These cached computations will not be included in the output of dumpCacheAsHaskell.

preCacheComputation :: forall req u w a. (Eq (req a), Hashable (req a), Typeable (req a)) => req a -> GenHaxl u w a -> GenHaxl u w a Source #

Like cachedComputation, but fails if the cache is already populated.

Memoization can be (ab)used to "mock" a cached computation, by pre-populating the cache with an alternative implementation. In that case we don't want the operation to populate the cache to silently succeed if the cache is already populated.

dumpCacheAsHaskell :: GenHaxl u w String Source #

Dump the contents of the cache as Haskell code that, when compiled and run, will recreate the same cache contents. For example, the generated code looks something like this:

loadCache :: GenHaxl u w ()
loadCache = do
  cacheRequest (ListWombats 3) (Right ([1,2,3]))
  cacheRequest (CountAardvarks "abcabc") (Right (2))

Memoization

newMemo :: GenHaxl u w (MemoVar u w a) Source #

Create a new MemoVar for storing a memoized computation. The created MemoVar is initially empty, not tied to any specific computation. Running this memo (with runMemo) without preparing it first (with prepareMemo) will result in an exception.

newMemoWith :: GenHaxl u w a -> GenHaxl u w (MemoVar u w a) Source #

Convenience function, combines newMemo and prepareMemo.

prepareMemo :: MemoVar u w a -> GenHaxl u w a -> GenHaxl u w () Source #

Store a computation within a supplied MemoVar. Any memo stored within the MemoVar already (regardless of completion) will be discarded, in favor of the supplied computation. A MemoVar must be prepared before it is run.

runMemo :: MemoVar u w a -> GenHaxl u w a Source #

Continue the memoized computation within a given MemoVar. Notes:

  1. If the memo contains a complete result, return that result.
  2. If the memo contains an in-progress computation, continue it as far as possible for this round.
  3. If the memo is empty (it was not prepared), throw an error.

For example, to memoize the computation one given by:

one :: Haxl Int
one = return 1

use:

do
  oneMemo <- newMemoWith one
  let memoizedOne = runMemo aMemo one
  oneResult <- memoizedOne

To memoize mutually dependent computations such as in:

h :: Haxl Int
h = do
  a <- f
  b <- g
  return (a + b)
 where
  f = return 42
  g = succ <$> f

without needing to reorder them, use:

h :: Haxl Int
h = do
  fMemoRef <- newMemo
  gMemoRef <- newMemo

  let f = runMemo fMemoRef
      g = runMemo gMemoRef

  prepareMemo fMemoRef $ return 42
  prepareMemo gMemoRef $ succ <$> f

  a <- f
  b <- g
  return (a + b)

memo :: (Typeable a, Typeable k, Hashable k, Eq k) => k -> GenHaxl u w a -> GenHaxl u w a Source #

Memoize a computation using an arbitrary key. The result will be calculated once; the second and subsequent time it will be returned immediately. It is the caller's responsibility to ensure that for every two calls memo key haxl, if they have the same key then they compute the same result.

memoUnique :: (Typeable a, Typeable k, Hashable k, Eq k) => MemoFingerprintKey a -> Text -> k -> GenHaxl u w a -> GenHaxl u w a Source #

Memoize a computation using its location and a Fingerprint. This ensures uniqueness across computations.

memoize :: GenHaxl u w a -> GenHaxl u w (GenHaxl u w a) Source #

Transform a Haxl computation into a memoized version of itself.

Given a Haxl computation, memoize creates a version which stores its result in a MemoVar (which memoize creates), and returns the stored result on subsequent invocations. This permits the creation of local memos, whose lifetimes are scoped to the current function, rather than the entire request.

memoize1 :: (Eq a, Hashable a) => (a -> GenHaxl u w b) -> GenHaxl u w (a -> GenHaxl u w b) Source #

Transform a 1-argument function returning a Haxl computation into a memoized version of itself.

Given a function f of type a -> GenHaxl u w b, memoize1 creates a version which memoizes the results of f in a table keyed by its argument, and returns stored results on subsequent invocations with the same argument.

e.g.:

allFriends :: [Int] -> GenHaxl u w [Int]
allFriends ids = do
  memoizedFriendsOf <- memoize1 friendsOf
  concat <$> mapM memoizeFriendsOf ids

The above implementation will not invoke the underlying friendsOf repeatedly for duplicate values in ids.

memoize2 :: (Eq a, Hashable a, Eq b, Hashable b) => (a -> b -> GenHaxl u w c) -> GenHaxl u w (a -> b -> GenHaxl u w c) Source #

Transform a 2-argument function returning a Haxl computation, into a memoized version of itself.

The 2-ary version of memoize1, see its documentation for details.

data MemoFingerprintKey a where Source #

A memo key derived from a 128-bit MD5 hash. Do not use this directly, it is for use by automatically-generated memoization.

Instances

Instances details
Eq (MemoFingerprintKey a) Source # 
Instance details

Defined in Haxl.Core.Memo

Hashable (MemoFingerprintKey a) Source # 
Instance details

Defined in Haxl.Core.Memo

Conditionals

pAnd :: GenHaxl u w Bool -> GenHaxl u w Bool -> GenHaxl u w Bool infixr 5 Source #

Parallel version of (.&&). Both arguments are evaluated in parallel, and if either returns False then the other is not evaluated any further.

WARNING: exceptions may be unpredictable when using pAnd. If one argument returns False before the other completes, then pAnd returns False immediately, ignoring a possible exception that the other argument may have produced if it had been allowed to complete.

pOr :: GenHaxl u w Bool -> GenHaxl u w Bool -> GenHaxl u w Bool infixr 4 Source #

Parallel version of (.||). Both arguments are evaluated in parallel, and if either returns True then the other is not evaluated any further.

WARNING: exceptions may be unpredictable when using pOr. If one argument returns True before the other completes, then pOr returns True immediately, ignoring a possible exception that the other argument may have produced if it had been allowed to complete.

unsafeChooseFirst :: GenHaxl u w a -> GenHaxl u w b -> GenHaxl u w (Either a b) Source #

This function takes two haxl computations as input, and returns the output of whichever computation finished first. This is clearly non-deterministic in its output and exception behavior, be careful when using it.

Statistics

newtype Stats Source #

Stats that we collect along the way.

Constructors

Stats [FetchStats] 

Instances

Instances details
Show Stats Source # 
Instance details

Defined in Haxl.Core.Stats

Methods

showsPrec :: Int -> Stats -> ShowS #

show :: Stats -> String #

showList :: [Stats] -> ShowS #

Semigroup Stats Source # 
Instance details

Defined in Haxl.Core.Stats

Methods

(<>) :: Stats -> Stats -> Stats #

sconcat :: NonEmpty Stats -> Stats #

stimes :: Integral b => b -> Stats -> Stats #

Monoid Stats Source # 
Instance details

Defined in Haxl.Core.Stats

Methods

mempty :: Stats #

mappend :: Stats -> Stats -> Stats #

mconcat :: [Stats] -> Stats #

ToJSON Stats Source # 
Instance details

Defined in Haxl.Core.Stats

data FetchStats Source #

Maps data source name to the number of requests made in that round. The map only contains entries for sources that made requests in that round.

Constructors

FetchStats

Timing stats for a (batched) data fetch

FetchCall

The stack trace of a call to dataFetch. These are collected only when profiling and reportLevel is 5 or greater.

MemoCall 

Fields

FetchWait 

Fields

FetchDataSourceStats 

Instances

Instances details
Eq FetchStats Source # 
Instance details

Defined in Haxl.Core.Stats

Show FetchStats Source # 
Instance details

Defined in Haxl.Core.Stats

ToJSON FetchStats Source # 
Instance details

Defined in Haxl.Core.Stats

ppStats :: Stats -> String Source #

Pretty-print Stats.

ppFetchStats :: FetchStats -> String Source #

Pretty-print RoundStats.

aggregateFetchBatches :: ([FetchStats] -> a) -> Stats -> [a] Source #

Aggregate stats merging FetchStats from the same dispatched batch into one.

data Profile Source #

Constructors

Profile 

Fields

data ProfileMemo Source #

Instances

Instances details
Eq ProfileMemo Source # 
Instance details

Defined in Haxl.Core.Stats

Show ProfileMemo Source # 
Instance details

Defined in Haxl.Core.Stats

data ProfileData Source #

Constructors

ProfileData 

Fields

Instances

Instances details
Show ProfileData Source # 
Instance details

Defined in Haxl.Core.Stats

Report flags

data ReportFlag Source #

Constructors

ReportOutgoneFetches

outgone fetches, for debugging eg: timeouts

ReportFetchStats

data fetch stats & errors

ReportProfiling

enabling label stack and profiling

ReportExceptionLabelStack

include label stack in HaxlException

ReportFetchStack

log cost-center stack traces of dataFetch calls

data ReportFlags Source #

Instances

Instances details
Show ReportFlags Source # 
Instance details

Defined in Haxl.Core.Flags

Flags

data Flags Source #

Flags that control the operation of the engine.

Constructors

Flags 

Fields

  • trace :: !Int

    Tracing level (0 = quiet, 3 = very verbose).

  • report :: !ReportFlags

    Report flags

  • caching :: !Int

    Non-zero if caching is enabled. If caching is disabled, then we still do batching and de-duplication, but do not cache results.

  • recording :: !Int

    Non-zero if recording is enabled. This allows tests to record cache calls for datasources by making uncachedRequest behave like dataFetch

ifTrace :: Monad m => Flags -> Int -> m a -> m () Source #

Runs an action if the tracing level is above the given threshold.

ifReport :: Monad m => Flags -> ReportFlag -> m a -> m () Source #

Runs an action if the ReportFlag is set.

ifProfiling :: Monad m => Flags -> m a -> m () Source #

Building data sources

class (DataSourceName req, StateKey req, ShowP req) => DataSource u req where Source #

The class of data sources, parameterised over the request type for that data source. Every data source must implement this class.

A data source keeps track of its state by creating an instance of StateKey to map the request type to its state. In this case, the type of the state should probably be a reference type of some kind, such as IORef.

For a complete example data source, see Examples.

Minimal complete definition

fetch

Methods

fetch Source #

Arguments

:: State req

Current state.

-> Flags

Tracing flags.

-> u

User environment.

-> PerformFetch req

Fetch the data; see PerformFetch.

Issues a list of fetches to this DataSource. The BlockedFetch objects contain both the request and the ResultVars into which to put the results.

schedulerHint :: u -> SchedulerHint req Source #

classifyFailure :: u -> req a -> SomeException -> FailureClassification Source #

class ShowP f where Source #

A class of type constructors for which we can show all parameterizations.

Methods

showp :: f a -> String Source #

class DataSourceName (req :: Type -> Type) where Source #

Methods

dataSourceName :: Proxy req -> Text Source #

The name of this DataSource, used in tracing and stats. Must take a dummy request.

Instances

Instances details
Typeable tag => DataSourceName (ConcurrentIOReq tag) Source # 
Instance details

Defined in Haxl.DataSource.ConcurrentIO

type Request req a = (Eq (req a), Hashable (req a), Typeable (req a), Show (req a), Show a) Source #

A convenience only: package up Eq, Hashable, Typeable, and Show for requests into a single constraint.

data BlockedFetch r Source #

A BlockedFetch is a pair of

  • The request to fetch (with result type a)
  • A ResultVar to store either the result or an error

We often want to collect together multiple requests, but they return different types, and the type system wouldn't let us put them together in a list because all the elements of the list must have the same type. So we wrap up these types inside the BlockedFetch type, so that they all look the same and we can put them in a list.

When we unpack the BlockedFetch and get the request and the ResultVar out, the type system knows that the result type of the request matches the type parameter of the ResultVar, so it will let us take the result of the request and store it in the ResultVar.

Constructors

forall a. BlockedFetch (r a) (ResultVar a) 

data PerformFetch req Source #

A data source can fetch data in one of four ways.

Constructors

SyncFetch ([BlockedFetch req] -> IO ())

Fully synchronous, returns only when all the data is fetched. See syncFetch for an example.

AsyncFetch ([BlockedFetch req] -> IO () -> IO ())

Asynchronous; performs an arbitrary IO action while the data is being fetched, but only returns when all the data is fetched. See asyncFetch for an example.

BackgroundFetch ([BlockedFetch req] -> IO ())

Fetches the data in the background, calling putResult at any time in the future. This is the best kind of fetch, because it provides the most concurrency.

class Typeable f => StateKey (f :: Type -> Type) where Source #

StateKey maps one type to another type. A type that is an instance of StateKey can store and retrieve information from a StateStore.

Minimal complete definition

Nothing

Associated Types

data State f Source #

Methods

getStateType :: Proxy f -> TypeRep Source #

We default this to typeOf1, but if f is itself a complex type that is already applied to some paramaters, we want to be able to use the same state by using typeOf2, etc

Instances

Instances details
Typeable tag => StateKey (ConcurrentIOReq tag) Source # 
Instance details

Defined in Haxl.DataSource.ConcurrentIO

Associated Types

data State (ConcurrentIOReq tag) Source #

data SchedulerHint (req :: Type -> Type) Source #

Hints to the scheduler about this data source

Constructors

TryToBatch

Hold data-source requests while we execute as much as we can, so that we can hopefully collect more requests to batch.

SubmitImmediately

Submit a request via fetch as soon as we have one, don't try to batch multiple requests. This is really only useful if the data source returns BackgroundFetch, otherwise requests to this data source will be performed synchronously, one at a time.

data FailureClassification Source #

Hints to the stats module about how to deal with these failures

Result variables

newtype ResultVar a Source #

A sink for the result of a data fetch in BlockedFetch

Constructors

ResultVar (Either SomeException a -> Bool -> Maybe DataSourceStats -> IO ()) 

putFailure :: Exception e => ResultVar a -> e -> IO () Source #

putSuccess :: ResultVar a -> a -> IO () Source #

putResultFromChildThread :: ResultVar a -> Either SomeException a -> IO () Source #

Like putResult, but used to get correct accounting when work is being done in child threads. This is particularly important for data sources that are using BackgroundFetch, The allocation performed in the child thread up to this point will be propagated back to the thread that called runHaxl.

Note: if you're doing multiple putResult calls in the same thread ensure that only the last one is putResultFromChildThread. If you make multiple putResultFromChildThread calls, the allocation will be counted multiple times.

If you are reusing a thread for multiple fetches, you should call System.Mem.setAllocationCounter 0 after putResultFromChildThread, so that allocation is not counted multiple times.

data DataSourceStats Source #

Constructors

forall a.(Typeable a, Show a, Eq a, ToJSON a) => DataSourceStats a 

Instances

Instances details
Eq DataSourceStats Source # 
Instance details

Defined in Haxl.Core.Stats

Show DataSourceStats Source # 
Instance details

Defined in Haxl.Core.Stats

Default fetch implementations

asyncFetch Source #

Arguments

:: ((service -> IO ()) -> IO ())

Wrapper to perform an action in the context of a service.

-> (service -> IO ())

Dispatch all the pending requests and wait for the results

-> (forall a. service -> request a -> IO (IO (Either SomeException a)))

Submits an individual request to the service.

-> State request

Currently unused.

-> Flags

Currently unused.

-> u

Currently unused.

-> PerformFetch request 

asyncFetchWithDispatch Source #

Arguments

:: ((service -> IO ()) -> IO ())

Wrapper to perform an action in the context of a service.

-> (service -> IO ())

Dispatch all the pending requests

-> (service -> IO ())

Wait for the results

-> (forall a. service -> request a -> IO (IO (Either SomeException a)))

Enqueue an individual request to the service.

-> State request

Currently unused.

-> Flags

Currently unused.

-> u

Currently unused.

-> PerformFetch request 

Common implementation templates for fetch of DataSource.

Example usage:

fetch = syncFetch MyDS.withService MyDS.retrieve
  $ \service request -> case request of
    This x -> MyDS.fetchThis service x
    That y -> MyDS.fetchThat service y

asyncFetchAcquireRelease Source #

Arguments

:: IO service

Resource acquisition for this datasource

-> (service -> IO ())

Resource release

-> (service -> IO ())

Dispatch all the pending requests and wait for the results

-> (service -> IO ())

Wait for the results

-> (forall a. service -> request a -> IO (IO (Either SomeException a)))

Submits an individual request to the service.

-> State request

Currently unused.

-> Flags

Currently unused.

-> u

Currently unused.

-> PerformFetch request 

A version of asyncFetch (actually asyncFetchWithDispatch) that handles exceptions correctly. You should use this instead of asyncFetch or asyncFetchWithDispatch. The danger with asyncFetch is that if an exception is thrown by withService, the inner action won't be executed, and we'll drop some data-fetches in the same round.

asyncFetchAcquireRelease behaves like the following:

asyncFetchAcquireRelease acquire release dispatch wait enqueue =
  AsyncFetch $ \requests inner ->
    bracket acquire release $ \service -> do
      getResults <- mapM (submitFetch service enqueue) requests
      dispatch service
      inner
      wait service
      sequence_ getResults

except that inner is run even if acquire, enqueue, or dispatch throws, unless an async exception is received.

backgroundFetchSeq Source #

Arguments

:: (forall a. request a -> IO (Either SomeException a))

Run one request, will be run in a background thread

-> State request

Currently unused.

-> Flags

Currently unused.

-> u

Currently unused.

-> PerformFetch request 

backgroundFetchPar Source #

Arguments

:: (forall a. request a -> IO (Either SomeException a))

Run one request, will be run in a background thread

-> State request

Currently unused.

-> Flags

Currently unused.

-> u

Currently unused.

-> PerformFetch request 

backgroundFetchAcquireRelease Source #

Arguments

:: IO service

Resource acquisition for this datasource

-> (service -> IO ())

Resource release

-> (service -> Int -> StablePtr PrimMVar -> IO ())

Dispatch all the pending requests and when ready trigger the given mvar

-> (service -> IO ())

Process all requests

-> (forall a. service -> request a -> IO (IO (Either SomeException a)))

Submits an individual request to the service.

-> State request

Currently unused.

-> Flags

Currently unused.

-> u

Currently unused.

-> PerformFetch request 

A version of backgroundFetchAcquireReleaseMVar where the dispatch function is given a 'StablePtr PrimMVar' which is more useful for C based APIs.

backgroundFetchAcquireReleaseMVar Source #

Arguments

:: IO service

Resource acquisition for this datasource

-> (service -> IO ())

Resource release

-> (service -> Int -> MVar () -> IO ())

Dispatch all the pending requests and when ready trigger the given mvar

-> (service -> IO ())

Process all requests

-> (forall a. service -> request a -> IO (IO (Either SomeException a)))

Submits an individual request to the service.

-> State request

Currently unused.

-> Flags

Currently unused.

-> u

Currently unused.

-> PerformFetch request 

stubFetch :: Exception e => (forall a. r a -> e) -> State r -> Flags -> u -> PerformFetch r Source #

syncFetch Source #

Arguments

:: ((service -> IO ()) -> IO ())

Wrapper to perform an action in the context of a service.

-> (service -> IO ())

Dispatch all the pending requests and wait for the results

-> (forall a. service -> request a -> IO (IO (Either SomeException a)))

Submits an individual request to the service.

-> State request

Currently unused.

-> Flags

Currently unused.

-> u

Currently unused.

-> PerformFetch request 

Utilities

setError :: Exception e => (forall a. r a -> e) -> BlockedFetch r -> IO () Source #

Function for easily setting a fetch to a particular exception

Exceptions

Recording the function callgraph