memcache-0.5.1.0: Database/Memcache/ElastiCacheClient.hs
{-# LANGUAGE OverloadedStrings #-}
{-|
Module : Database.Memcache.ElastiCacheClient
Description : Amazon ElastiCache service discovery
Copyright : (c) David Terei, 2016
License : BSD
Maintainer : code@davidterei.com
Stability : stable
Portability : GHC
Client creation and automatic node discovery for Amazon ElastiCache clusters.
-}
module Database.Memcache.ElastiCacheClient
( ConfigurationEndpoint
, parseConfigurationEndpoint,
newClient,
)
where
import Control.Concurrent (forkIO, forkIOWithUnmask, killThread, threadDelay)
import Control.Concurrent.MVar (MVar, mkWeakMVar, modifyMVar, newMVar)
import Control.Monad (guard, void, when)
import Data.Bifunctor (first)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as C
import Data.Char (isAscii, isSpace)
import Data.IORef (IORef, atomicWriteIORef, newIORef, readIORef)
import Data.List (sort)
import qualified Data.List.NonEmpty as NE
import Data.Maybe (isJust)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Vector as V
import Data.Version (Version, makeVersion)
import qualified Data.Version as Version
import Data.Word (Word64)
import Database.Memcache.Client (Client, optsServerSpecsToServers, optsServerOptions, optsServerTimeout)
import qualified Database.Memcache.Client as Client
import Database.Memcache.Cluster (Options, getServers, setServers)
import Database.Memcache.Errors (ClientError (Timeout), MemcacheError (ClientError))
import Database.Memcache.Server (Server, closeAll, withSocket)
import Database.Memcache.Socket (Connection, crlf, recvRawUntil, sendRaw, versionRequest)
import Database.Memcache.Types.Authentication (Authentication (NoAuth))
import Database.Memcache.Types.Cluster (Cluster (cStopRefresh))
import Database.Memcache.Types.ServerSpec (ServerSpec (..), parseServerSpec)
import Database.Memcache.Util (note, readDecimal, withDetail)
import Network.Socket (HostName, ServiceName)
import System.Mem.Weak (Weak, deRefWeak)
import System.Timeout (timeout)
import Text.ParserCombinators.ReadP (readP_to_S)
import UnliftIO.Exception (bracket, finally, mask_, throwIO, throwString, tryAny, uninterruptibleMask_)
-- | Parse an ElastiCache configuration endpoint.
--
-- A /Configuration Endpoint/ will always contain /.cfg/ in its address:
--
-- https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/AutoDiscovery.Using.html
--
-- The rest is read as 'parseServerSpec' reads a server, so the endpoint must
-- begin @memcached:\/\/@, as in
-- @memcached:\/\/mycluster.abc123.cfg.use1.cache.amazonaws.com:11211@.
parseConfigurationEndpoint :: String -> Either String ConfigurationEndpoint
parseConfigurationEndpoint url = do
note "URI does not contain '.cfg'" $ guard $ ".cfg" `T.isInfixOf` T.pack url
ConfigurationEndpoint <$> parseServerSpec url
-- Please see:
-- https://github.com/memcached/memcached/blob/77709d04dd4fc7d59f59425802cb9645b2cfe0f8/doc/protocol.txt#L1805-L1812
-- ByteString format: "VERSION <version>\r\n"
--
-- The version only chooses the request for the cluster configuration, and is
-- read leniently, with 'Data.Version.parseVersion': the line's first 8 bytes
-- aren't checked, and a number too large for an 'Int' wraps around. But a
-- version longer than 'maxVersionLength' bytes is an error before it is
-- parsed: 'readP_to_S' gives every partial parse of a dotted version, so the
-- parse takes time that grows faster than the square of the version's
-- length. The endpoint can send a line of up to 1 MiB, and the time limit on
-- discovery can't interrupt the parse while the caller has asynchronous
-- exceptions masked.
parseVersion :: ByteString -> Either String Version
parseVersion line = note ("Unrecognized version reply" `withDetail` C.unpack text) $ do
let version = C.drop (C.length "VERSION ") text
guard $ C.length version <= maxVersionLength
-- The longest parse comes last, and must take the whole version.
results <- NE.nonEmpty $ readP_to_S Version.parseVersion (C.unpack version)
case NE.last results of
(parsed, "") -> pure parsed
_ -> Nothing
where
text = C.dropEnd (C.length crlf) line
-- | The longest version @parseVersion@ reads, in bytes. memcached's versions,
-- such as @1.6.12@, are a few bytes long, so this leaves plenty of room for
-- tags.
maxVersionLength :: Int
maxVersionLength = 64
-- | A validated ElastiCache configuration endpoint.
--
-- Use the smart constructor @parseConfigurationEndpoint@ to create one.
newtype ConfigurationEndpoint
= ConfigurationEndpoint
{ unConfigurationEndpoint :: ServerSpec
}
-- | Create a new client using service discovery.
--
-- This function discovers all the nodes in a cluster and sets up a new thread to run autodiscovery on an interval.
-- The thread runs with asynchronous exceptions unmasked, even if this is
-- called with them masked, as in the acquire action of a @bracket@.
--
-- The first discovery must finish within 'optsServerTimeout' (connecting,
-- which asks for the version, then asking for it again and for the cluster
-- configuration), or this throws v'ClientError' 'Timeout'. If the endpoint
-- sends an error reply, such as the @ERROR@ that plain memcached sends as it
-- has no @config@ command, this throws a @StringException@ from
-- "UnliftIO.Exception", as it does for a reply that can't be parsed, a
-- version of more than 64 bytes, and a configuration whose version can't be
-- read, or that lists no nodes, more than 1,000, an empty node, the same host
-- and port twice, or a node that isn't a plain host and port: one whose host
-- or port is empty, or whose host has a character, such as @\/@, @?@ or @\@@,
-- that would change the host or port, add credentials or make the node a Unix
-- socket on this machine. A host given as an IPv6 address in brackets is
-- rejected too. If a reply goes on for more than 1 MiB without the line end
-- or the @END@ line that discovery is waiting for, this throws
-- v'Database.Memcache.Errors.ProtocolError'
-- 'Database.Memcache.Errors.BadLength'.
--
-- A configuration begins with its version, a number that, as AWS documents
-- it, goes up by one each time a node is added to the cluster or removed from
-- it. Discovery reads the version as a number in decimal digits, ignoring
-- ASCII whitespace around it, and can't read one with anything else in it,
-- such as a sign or a decimal point, or one too large for 64 bits. The first
-- discovery takes its configuration whatever the version. After that, a
-- refresh applies a configuration only if its version is higher than that of
-- the configuration applied last, the first discovery's included, and
-- otherwise leaves the servers as they are, whatever nodes and IP addresses
-- it lists. So a reply from an endpoint that hasn't caught up with a change
-- yet doesn't roll the servers back. But a version that goes backwards is
-- ignored until the client is made again, or until it passes the version
-- applied last. AWS's own cluster client does the same, except that its
-- refreshes don't count the configuration it was made with, so the first of
-- them to read a configuration applies it even at a lower or the same
-- version.
--
-- Servers are made by calling 'optsServerSpecsToServers' with one node at a
-- time. A refresh that applies a configuration keeps the 'Server', and so the
-- failure state, of every node still listed with the same host and port, and
-- its connections too unless the node is listed with another IP address. It
-- makes servers only for new nodes, and closes the connections of nodes no
-- longer listed: idle ones at once, and one in use as soon as its request
-- finishes. It closes the connections of a node listed with another IP
-- address the same way, as ElastiCache lists a node it has replaced, so that
-- its requests connect again, looking its name up afresh. If the name still
-- resolves to the replaced node's address then, they connect to it, and later
-- refreshes don't close those connections, as the listed address no longer
-- changes.
--
-- Each refresh connects to the endpoint afresh, looking its name up again, and
-- closes that connection when it is done, whether it worked or not: the name
-- leads to one of the nodes, so a connection kept from an earlier refresh
-- could lead to a node that ElastiCache has since replaced, and get no reply.
-- Each refresh has the same time and length limits as the first discovery. A
-- refresh that fails, for example because it runs out of time or the reply is
-- an error, is too long, can't be parsed, has a version that can't be read,
-- lists no nodes, more than 1,000, an empty node or one twice, or lists a node
-- that isn't a plain host and port, leaves the servers as they are, and with
-- them the version applied last, and discovery tries again after the next
-- interval.
--
-- The thread stops for good when the client is closed with
-- 'Database.Memcache.Client.quit'. No refresh replaces the servers once their
-- connections are closed, and a refresh in progress is abandoned, closing its
-- connection to the endpoint. The thread also stops once the client has been
-- garbage collected, as it doesn't keep the client reachable: at the end of
-- the interval it is waiting out, or, if a refresh is in progress, without
-- replacing the servers: when that refresh would replace them, or else at the
-- end of the next interval.
--
-- Discovery connects to the configuration endpoint like any other server, so
-- it fails for a cluster running a memcached older than 1.6.10, having sent
-- only the authentication exchange, when the endpoint has credentials, and
-- @version@ (see "Database.Memcache.Client"). Each refresh opens a new
-- connection to the endpoint, so against an endpoint older than that it fails
-- like any other refresh, and the connections the client opens to the nodes
-- it discovers are checked in the same way.
--
-- N.B. The interval is in the same units as 'threadDelay'.
newClient :: Options -> Int -> ConfigurationEndpoint -> IO Client
newClient options interval cfgEndpoint = bracket acquireCfgClient releaseCfgClient resolveCluster'
where
acquireCfgClient = Client.newClient [unConfigurationEndpoint cfgEndpoint] options
-- This quits the first discovery's client, not the client returned, so
-- it doesn't stop the refresh thread.
releaseCfgClient = Client.quit
resolveCluster' cfgClient = do
-- The first discovery applies its configuration whatever the version.
(version, serverSpecs) <- resolveClusterWithin options cfgClient
(nodes, _, _) <- reuseServers options [] serverSpecs
-- Prepare MVar
serversMVar <- newMVar $ nodeServers nodes
-- The refresh thread gets the MVar only through a weak pointer, so
-- that it doesn't keep the client reachable, and can stop once the
-- client has been garbage collected.
servers <- mkWeakMVar serversMVar (pure ())
stopped <- newIORef False
-- Refresh with asynchronous exceptions unmasked, whatever the caller's
-- masking state. A thread forked inside mask_, as in a bracket's
-- acquire, would otherwise refresh masked for good, and each refresh's
-- time limit could interrupt it only where it waits.
thread <- forkIOWithUnmask $ \unmask ->
unmask $ repeatAutoDiscover options interval cfgEndpoint servers stopped version nodes
-- What 'Client.quit' runs before it closes the servers' connections.
-- Once the flag is set, no refresh replaces the servers. Killing the
-- thread abandons a refresh in progress, but the kill alone can't stop
-- a swap: it can land after one, or be caught, as by a server
-- constructor that catches every exception. Another thread sends the
-- kill, so that quit doesn't wait while the refresh thread can't be
-- interrupted, as when it looks up the endpoint's name. Masked, so
-- that once the flag is set, the kill is sent.
let stop = mask_ $ do
atomicWriteIORef stopped True
void $ forkIO $ killThread thread
-- The configuration client was made with the same options, so it only
-- needs its servers replacing: cServers = Left <MVar <Vector Server>>
pure (setServers cfgClient $ Left serversMVar) { cStopRefresh = stop }
-- | Refresh the servers in the 'MVar' every interval, starting from the
-- configuration version and the nodes of the first discovery, until the flag
-- is set or the 'MVar' has been garbage collected. AWS's own cluster client
-- starts its refreshes from version -1 instead, whatever the version of the
-- configuration it was made with, so the first of them to read a
-- configuration applies it even at a lower or the same version. Starting from
-- the first discovery's version keeps that refresh from rolling the servers
-- back as well.
repeatAutoDiscover
:: Options -> Int -> ConfigurationEndpoint -> Weak (MVar (V.Vector Server)) -> IORef Bool
-> ConfigVersion -> Nodes -> IO ()
repeatAutoDiscover opts interval cfgEndpoint servers stopped initialVersion initialNodes = do
cfgClient <- Client.newClient [unConfigurationEndpoint cfgEndpoint] opts
-- Wait out the interval, then refresh, unless the client has been quit or
-- garbage collected meanwhile. The 'MVar' is dropped at once, so that the
-- thread doesn't keep it reachable while it waits.
let next version nodes = do
threadDelay interval
quitting <- readIORef stopped
reachable <- isJust <$> deRefWeak servers
when (reachable && not quitting) $ refresh version nodes
refresh version nodes = do
-- A refresh that fails or does not finish in time keeps the current
-- servers. 'tryAny' lets asynchronous exceptions, such as
-- 'killThread', through.
result <- tryAny $ do
(version', serverSpecs) <- resolveClusterWithin opts cfgClient
-- Apply only a configuration newer than the one applied last, so
-- that one from an endpoint that hasn't caught up with a change yet
-- can't roll the servers back. Make no servers for any other.
if version' > version
then do
(nodes', gone, moved) <- reuseServers opts nodes serverSpecs
pure $ Just (version', nodes', gone, moved)
else pure Nothing
-- Close the connection to the endpoint, whether the refresh worked or
-- not, so that the next refresh connects again, looking the
-- endpoint's name up afresh. The name leads to one of the nodes, and
-- once ElastiCache replaces that node, a connection made before then
-- may get no reply, so the next refresh would run out of time on it.
Client.quit cfgClient
case result of
-- The refresh failed, or its configuration is no newer.
Left _ -> next version nodes
Right Nothing -> next version nodes
Right (Just (version', nodes', gone, moved)) -> do
-- Swap in the new servers, then close the connections of nodes
-- that have gone, or whose IP address has changed. A request
-- still using one of them finishes normally, and its connection
-- is closed then instead of going back to the pool. Mask the swap
-- and the loop together, so that an asynchronous exception can't
-- arrive between them, or stop the loop part way, and leave some
-- of those connections open. Neither waits long: only this thread
-- takes the 'MVar', and 'closeAll' only ever waits briefly. Once
-- the client has been quit or garbage collected, swap nothing in,
-- and stop.
swapped <- uninterruptibleMask_ $ do
target <- deRefWeak servers
replaced <- maybe (pure False) (swapTo nodes') target
when replaced $ mapM_ closeAll (gone ++ moved)
pure replaced
when swapped $ next version' nodes'
-- 'Client.quit' sets the flag before it reads the servers to close
-- their connections, so checking the flag while holding the 'MVar'
-- means that no refresh swaps in servers that quit doesn't close.
swapTo nodes' mVar = modifyMVar mVar $ \current -> do
quitting <- readIORef stopped
pure $ if quitting then (current, False) else (nodeServers nodes', True)
-- Close the connection to the endpoint too when the thread is killed, as
-- 'Client.quit' does to a refresh in progress.
next initialVersion initialNodes `finally` Client.quit cfgClient
-- | The nodes found by discovery, by host and port, with the IP address listed
-- for each and the servers made for each.
type Nodes = [((HostName, ServiceName), (Text, [Server]))]
-- | The servers of the given nodes, in the order they were discovered.
nodeServers :: Nodes -> V.Vector Server
nodeServers = V.fromList . concatMap (snd . snd)
-- | Pair each discovered node with its servers from the given nodes, making
-- servers only for nodes not among them. Also returns the servers of the given
-- nodes that were not discovered again, and those of nodes discovered again
-- with another IP address.
reuseServers :: Options -> Nodes -> [(ServerSpec, Text)] -> IO (Nodes, [Server], [Server])
reuseServers _ old [] = pure ([], concatMap (snd . snd) old, [])
reuseServers opts old ((spec, ip) : specs) = do
let node = (ssHost spec, ssPort spec)
(servers, moved, rest) <- case break ((== node) . fst) old of
(before, (_, (oldIp, servers)) : after) -> pure (servers, oldIp /= ip, before ++ after)
(_, []) -> do
servers <- optsServerSpecsToServers opts (optsServerOptions opts) [spec]
pure (servers, False, old)
(nodes, gone, moved') <- reuseServers opts rest specs
pure ((node, (ip, servers)) : nodes, gone, if moved then servers ++ moved' else moved')
-- | Ask the endpoint for the cluster's configuration, and return its version
-- and nodes.
resolveCluster :: Client -> IO (ConfigVersion, [(ServerSpec, Text)])
resolveCluster cfgClient = do
servers <- getServers cfgClient
server <-
case V.uncons servers of
Nothing -> throwString "No servers were found"
Just (s, _) -> pure s
withSocket server $ \socket -> do
-- Get version
sendRaw socket versionRequest
rawVersion <- recvRawUntil crlf socket
version <- unTry $ parseVersion rawVersion
-- Get nodes
rawResponse <-
if version >= firstCfgCmdVersion
then resolveViaConfigCmd socket
else resolveViaAutoDiscoveryKey socket
(cfgVersion, rawNodeInfo) <- unTry $ handleConfigGetClusterResponse rawResponse
nodes <- unTry $ handleRawNodeInfo rawNodeInfo
pure (cfgVersion, nodes)
-- Run 'resolveCluster', throwing 'ClientError' 'Timeout' unless it finishes,
-- connecting and both requests included, within the options'
-- 'optsServerTimeout'.
resolveClusterWithin :: Options -> Client -> IO (ConfigVersion, [(ServerSpec, Text)])
resolveClusterWithin opts cfgClient = do
found <- timeout serverTimeout $ resolveCluster cfgClient
maybe (throwIO $ ClientError Timeout) pure found
where
-- 'optsServerTimeout' is in milliseconds; 'timeout' takes microseconds.
serverTimeout = truncate (optsServerTimeout opts * 1000)
-- Handle the raw response from /memcached/, returning the configuration's
-- version and its line of nodes. Both requests get the configuration in the
-- same form: after the line that starts the reply, a line with the version and
-- one listing the nodes.
--
-- See: https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/AutoDiscovery.AddingToYourClientLibrary.html#AutoDiscovery.AddingToYourClientLibrary.OutputFormat
handleConfigGetClusterResponse :: ByteString -> Either String (ConfigVersion, Text)
handleConfigGetClusterResponse bs = do
text <- first show $ T.decodeUtf8' bs
case T.lines text of
[_, version, nodeInfo, _, _] -> do
cfgVersion <- parseConfigVersion version
pure (cfgVersion, T.strip nodeInfo)
xs -> Left $ "Unrecognized number of lines: " <> show (length xs)
-- | A configuration's version, which ElastiCache raises each time a node is
-- added to the cluster or removed from it.
type ConfigVersion = Word64
-- | Read a configuration's version. Drop the ASCII whitespace around it
-- first: AWS documents the line as ending in LF alone, and one that ends in
-- CR LF reads the same. What is left must be a number in decimal digits alone
-- that fits in 64 bits. AWS's own cluster client for Java reads the version
-- with Java's @trim@ and @Long.parseLong@, so it also drops the control
-- characters around it, and takes a sign and digits outside ASCII, such as
-- Arabic-Indic ones, but stops at 2^63 - 1, the largest Java @long@. A
-- version comes from a counter, so this takes only ASCII digits, but any
-- number that fits in 64 bits.
parseConfigVersion :: Text -> Either String ConfigVersion
parseConfigVersion line =
note ("Unrecognized configuration version" `withDetail` T.unpack line) $
readDecimal (T.encodeUtf8 (T.dropAround isAsciiSpace line))
where
-- Not 'T.strip', which would also drop whitespace outside ASCII, such as
-- a no-break space.
isAsciiSpace c = isAscii c && isSpace c
-- Handle node information
--
-- Note that we chose to use the CNAME rather than the IP. The IP /may/ be missing. The CNAME will always be present.
-- The IP is returned only to notice a node that ElastiCache has replaced.
--
-- See: https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/AutoDiscovery.AddingToYourClientLibrary.html#AutoDiscovery.AddingToYourClientLibrary.OutputFormat
handleRawNodeInfo :: Text -> Either String [(ServerSpec, Text)]
handleRawNodeInfo nodeInfo = do
-- A line listing no nodes is empty, as it comes stripped of whitespace, and
-- would split into one empty entry.
when (T.null nodeInfo) $
Left "Configuration lists no nodes"
-- Count the nodes before parsing any of them.
let count = T.count " " nodeInfo + 1
when (count > maxNodes) $
Left $ "Configuration lists " <> show count <> " nodes, over the limit of " <> show maxNodes
nodes <- mapM parseEntry (T.splitOn " " nodeInfo)
-- A node listed twice would get a second server, at the same points on the
-- ring as the first, so that when the first is marked failed, its keys
-- would fall back to the same node rather than to another.
case listedTwice (sort [(ssHost spec, ssPort spec) | (spec, _) <- nodes]) of
(host, port) : _ -> Left $ "Node listed twice" `withDetail` (host <> ":" <> port)
[] -> pure nodes
where
listedTwice sorted = [node | (node, next) <- zip sorted (drop 1 sorted), node == next]
-- The line comes stripped of whitespace, and isn't empty, so an entry is
-- empty only where two spaces come in a row.
parseEntry t | T.null t = Left "Configuration lists an empty node"
parseEntry t =
case T.splitOn "|" t of
[host, ip, port] -> do
spec <- parseNode (T.unpack host) (T.unpack port)
pure (spec, ip)
_ -> Left $ "Unrecognized components in node info" `withDetail` T.unpack t
-- Node addresses come from the server and hold no credentials, so name
-- the one that failed.
parseNode :: HostName -> ServiceName -> Either String ServerSpec
parseNode host port = first (`withDetail` uri) $ do
-- A node is a host to reach over the network, on the port listed. The
-- URI pastes the two together, so a host such as "h/x" or "h?x" would
-- lose the port, one such as "u:p@h" would give the node credentials
-- of the endpoint's choosing, and one such as "/var/run/docker.sock?",
-- whose "?" puts the port in the URI's query, would make the node a
-- Unix socket on this machine. An empty host would be 127.0.0.1, and
-- an empty port would fail only at the first request. So the URI must
-- give back the host and port listed, neither of them empty, and no
-- credentials.
--
-- No plain host or port holds an "@", so a node whose host or port
-- does is turned down before the URI is parsed, whatever
-- 'parseServerSpec' would make of the "@": its errors, such as those
-- about credentials, are for someone who wrote the URI, which no one
-- did here.
note notPlain $ guard $ notElem '@' (host <> port)
spec <- parseServerSpec uri
note notPlain $
guard $ not (null host || null port) && spec == ServerSpec host port NoAuth
pure spec
where
uri = "memcached://" <> host <> ":" <> port
notPlain = "Node is not a plain host and port"
-- | The most nodes a configuration can list. ElastiCache allows 60 nodes in a
-- Memcached cluster unless that quota is raised, so a reply that lists more
-- than this comes from a broken or hostile endpoint. Without a limit, a reply
-- well inside the 1 MiB that discovery reads could list over 100,000 nodes of
-- a few bytes each, and discovery would make a server, with its pool of
-- connections, for each one, and the first request would wait while the ring
-- that routes keys was built, with 160 points for each node.
maxNodes :: Int
maxNodes = 1000
-- This is the first version within which the /memcache/ engine on /ElastiCache/ uses the custom /config/ command.
--
-- https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/AutoDiscovery.AddingToYourClientLibrary.html
--
firstCfgCmdVersion :: Version
firstCfgCmdVersion = makeVersion [1, 4, 14]
-- Pre-1.4.14 engines don't support meta commands, so use a plain text get.
resolveViaAutoDiscoveryKey :: Connection -> IO ByteString
resolveViaAutoDiscoveryKey socket = do
sendRaw socket ("get " <> autoDiscoveryKey <> crlf)
recvConfigReply socket
-- Before 'firstCfgCmdVersion', /ElastiCache/ stored configuration in a special-purpose key within the /Configuration Endpoint/.
--
-- https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/AutoDiscovery.AddingToYourClientLibrary.html
--
autoDiscoveryKey :: ByteString
autoDiscoveryKey = "AmazonElastiCache:cluster"
-- See: https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/AutoDiscovery.AddingToYourClientLibrary.html#AutoDiscovery.AddingToYourClientLibrary.OutputFormat
resolveViaConfigCmd :: Connection -> IO ByteString
resolveViaConfigCmd socket = do
sendRaw socket (configGetCluster <> crlf)
recvConfigReply socket
configGetCluster :: ByteString
configGetCluster = "config get cluster"
-- Receive the reply to the config command or to the get of the cluster key,
-- through the END line that ends it. An error reply is a single line with no
-- END line, and the server keeps the connection open after it, so throw on
-- one instead of waiting for END.
recvConfigReply :: Connection -> IO ByteString
recvConfigReply socket = do
line <- recvRawUntil crlf socket
let text = C.dropEnd (C.length crlf) line
when (C.takeWhile (/= ' ') text `elem` ["ERROR", "CLIENT_ERROR", "SERVER_ERROR"]) $
throwString $ "Configuration endpoint replied" `withDetail` C.unpack text
-- Otherwise read through the first END. This line can end with it: when
-- the cluster key is missing, the reply is the END line alone.
if end `C.isSuffixOf` line
then pure line
else (line <>) <$> recvRawUntil end socket
where
end = "END" <> crlf
unTry :: Either String a -> IO a
unTry = either throwString pure