module Kafka.Consumer.ConsumerProperties
( module Kafka.Consumer.ConsumerProperties
, module X
)
where
import Control.Monad
import qualified Data.List as L
import Data.Map (Map)
import qualified Data.Map as M
import Data.Semigroup as Sem
import Kafka.Consumer.Types
import Kafka.Internal.Setup
import Kafka.Types
import Kafka.Consumer.Callbacks as X
data ConsumerProperties = ConsumerProperties
{ cpProps :: Map String String
, cpLogLevel :: Maybe KafkaLogLevel
, cpCallbacks :: [KafkaConf -> IO ()]
}
instance Sem.Semigroup ConsumerProperties where
(ConsumerProperties m1 ll1 cb1) <> (ConsumerProperties m2 ll2 cb2) =
ConsumerProperties (M.union m2 m1) (ll2 `mplus` ll1) (cb1 `mplus` cb2)
{-# INLINE (<>) #-}
instance Monoid ConsumerProperties where
mempty = ConsumerProperties
{ cpProps = M.empty
, cpLogLevel = Nothing
, cpCallbacks = []
}
{-# INLINE mempty #-}
mappend = (Sem.<>)
{-# INLINE mappend #-}
brokersList :: [BrokerAddress] -> ConsumerProperties
brokersList bs =
let bs' = L.intercalate "," ((\(BrokerAddress x) -> x) <$> bs)
in extraProps $ M.fromList [("bootstrap.servers", bs')]
noAutoCommit :: ConsumerProperties
noAutoCommit =
extraProps $ M.fromList [("enable.auto.commit", "false")]
noAutoOffsetStore :: ConsumerProperties
noAutoOffsetStore =
extraProps $ M.fromList [("enable.auto.offset.store", "false")]
groupId :: ConsumerGroupId -> ConsumerProperties
groupId (ConsumerGroupId cid) =
extraProps $ M.fromList [("group.id", cid)]
clientId :: ClientId -> ConsumerProperties
clientId (ClientId cid) =
extraProps $ M.fromList [("client.id", cid)]
setCallback :: (KafkaConf -> IO ()) -> ConsumerProperties
setCallback cb = mempty { cpCallbacks = [cb] }
logLevel :: KafkaLogLevel -> ConsumerProperties
logLevel ll = mempty { cpLogLevel = Just ll }
compression :: KafkaCompressionCodec -> ConsumerProperties
compression c =
extraProps $ M.singleton "compression.codec" (kafkaCompressionCodecToString c)
suppressDisconnectLogs :: ConsumerProperties
suppressDisconnectLogs =
extraProps $ M.fromList [("log.connection.close", "false")]
extraProps :: Map String String -> ConsumerProperties
extraProps m = mempty { cpProps = m }
{-# INLINE extraProps #-}
extraProp :: String -> String -> ConsumerProperties
extraProp k v = mempty { cpProps = M.singleton k v }
{-# INLINE extraProp #-}
debugOptions :: [KafkaDebug] -> ConsumerProperties
debugOptions [] = extraProps M.empty
debugOptions d =
let points = L.intercalate "," (kafkaDebugToString <$> d)
in extraProps $ M.fromList [("debug", points)]
queuedMaxMessagesKBytes :: Int -> ConsumerProperties
queuedMaxMessagesKBytes kBytes =
extraProp "queued.max.messages.kbytes" (show kBytes)
{-# INLINE queuedMaxMessagesKBytes #-}