A collection of hook functions used by a connection.
Modulehedis-0.16.4Haskell2010
Database.Redis.Hooks
Hooks for observing or wrapping Redis I/O.
Hooks are installed through connect defaultConnectInfo { connectHooks = ... }
and wrap the low-level actions used by hedis:
sendRequestHook wraps regular command execution.
sendPubSubHook wraps pub/sub command sending.
callbackHook wraps invocation of pub/sub callbacks.
sendHook wraps raw bytes sent to the server.
receiveHook wraps reply reception.
The common pattern is to start from defaultHooks and override only the hook(s) you need. Each hook receives the original action and is expected to call it after performing any extra work such as logging, tracing, metrics, or timing.
Hooks can be used to alter existing behavior, or used to add metrics or telemetry to the redis application.
Example:
import Data.IORef
import Database.Redis
data Counts = Counts
{ sendRequestCount :: Word
, sendCount :: Word
, receiveCount :: Word
}
hooks :: IORef Counts -> Hooks
hooks ref =
defaultHooks
{ sendRequestHook = \run argv -> do
modifyIORef ref $ \c -> c { sendRequestCount = sendRequestCount c + 1 }
run argv
, sendHook = \sendBytes bytes -> do
modifyIORef ref $ \c -> c { sendCount = sendCount c + 1 }
sendBytes bytes
, receiveHook = \recvReply -> do
modifyIORef ref $ \c -> c { receiveCount = receiveCount c + 1 }
recvReply
}
main :: IO ()
main = do
ref <- newIORef (Counts 0 0 0)
conn <- connect defaultConnectInfo { connectHooks = hooks ref }
_ <- runRedis conn $ set "key" "value"
readIORef ref >>= print
- 6 types
- 1 value
- Packagehedis-0.16.4
- Exports7
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceHooks.hs
A hook for sending commands to the server and receiving replies from the server.
This wraps the command-level request path used by most Redis commands.
A hook for sending pub/sub messages to the server.
A hook for invoking callbacks with pub/sub messages.
A hook for sending raw bytes to the server.
This sits below request rendering and can be used to observe the exact wire payload sent on the socket.
A hook for receiving replies from the server.
The default hooks.
Every hook is the identity function, so installing defaultHooks has no effect on behavior.