doResolv
  :: HasLogFunc e
  => Galaxy
  -> (Time.Wen, Maybe SockAddr)
  -> [Turf]
  -> (Text -> RIO e ())
  -> RIO e (Maybe SockAddr, Time.Wen)
doResolv gal (prevWen, prevIP) turfs stderr = do
  current <- io $ Time.now
  if (Time.gap current prevWen ^. Time.secs) < 300
    then pure (prevIP, prevWen)
    else do
      tim <- io (Time.now)
      io (resolv gal turfs) >>= \case
        Nothing -> do
          stderr $ "ames: czar at " ++ galStr ++ ": not found"
          logInfo $ displayShow ("(ames) Failed to lookup IP for ", gal)
          pure (prevIP, tim)
        Just (turf, host, port, addr) -> do
          when (Just addr /= prevIP) (printCzar addr)
          logInfo $ displayShow ("(ames) Looked up ", host, port, turf, addr)
          pure (Just addr, tim)
 where
  galStr = renderGalaxy gal
  printCzar addr = stderr $ "ames: czar " ++ galStr ++ ": ip " ++ tshow addr
doResolv
  :: HasLogFunc e
  => Galaxy
  -> (Time.Wen, Maybe SockAddr)
  -> [Turf]
  -> (Text -> RIO e ())
  -> RIO e (Maybe SockAddr, Time.Wen)
doResolv gal (prevWen, prevIP) turfs stderr = do
  current <- io $ Time.now
  if (Time.gap current prevWen ^. Time.secs) < 300
    then pure (prevIP, prevWen)
    else do
      tim <- io (Time.now)
      io (resolv gal turfs) >>= \case
        Nothing -> do
          stderr $ "ames: czar at " ++ galStr ++ ": not found"
          logInfo $ displayShow ("(ames) Failed to lookup IP for ", gal)
          pure (prevIP, tim)
        Just (turf, host, port, addr) -> do
          when (Just addr /= prevIP) (printCzar addr)
          logInfo $ displayShow ("(ames) Looked up ", host, port, turf, addr)
          pure (Just addr, tim)
 where
  galStr = renderGalaxy gal
  printCzar addr = stderr $ "ames: czar " ++ galStr ++ ": ip " ++ tshow addr
doResolv gal (prevWen, prevIP) turfs stderr = do
  current <- io $ Time.now
  if (Time.gap current prevWen ^. Time.secs) < 300
    then pure (prevIP, prevWen)
    else do
      tim <- io (Time.now)
      io (resolv gal turfs) >>= \case
        Nothing -> do
          stderr $ "ames: czar at " ++ galStr ++ ": not found"
          logInfo $ displayShow ("(ames) Failed to lookup IP for ", gal)
          pure (prevIP, tim)
        Just (turf, host, port, addr) -> do
          when (Just addr /= prevIP) (printCzar addr)
          logInfo $ displayShow ("(ames) Looked up ", host, port, turf, addr)
          pure (Just addr, tim)
 where
  galStr = renderGalaxy gal
  printCzar addr = stderr $ "ames: czar " ++ galStr ++ ": ip " ++ tshow addr