diff options
Diffstat (limited to 'Presence')
-rw-r--r-- | Presence/DNSCache.hs | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/Presence/DNSCache.hs b/Presence/DNSCache.hs index 82e0cb56..aaf1a7be 100644 --- a/Presence/DNSCache.hs +++ b/Presence/DNSCache.hs | |||
@@ -33,7 +33,7 @@ import Network.Socket ( SockAddr(..), AddrInfoFlag(..), defaultHints, getAddrIn | |||
33 | import Data.Time.Clock ( UTCTime, getCurrentTime, diffUTCTime ) | 33 | import Data.Time.Clock ( UTCTime, getCurrentTime, diffUTCTime ) |
34 | import System.IO.Error ( isDoesNotExistError ) | 34 | import System.IO.Error ( isDoesNotExistError ) |
35 | import System.Endian ( fromBE32, toBE32 ) | 35 | import System.Endian ( fromBE32, toBE32 ) |
36 | import Control.Exception ( handle, ErrorCall(..) ) | 36 | import Control.Exception ( handle ) |
37 | import Data.Map ( Map ) | 37 | import Data.Map ( Map ) |
38 | import qualified Data.Map as Map | 38 | import qualified Data.Map as Map |
39 | import qualified Network.BSD as BSD | 39 | import qualified Network.BSD as BSD |
@@ -49,6 +49,7 @@ import System.IO.Error | |||
49 | import SockAddr () | 49 | import SockAddr () |
50 | import ControlMaybe ( handleIO_ ) | 50 | import ControlMaybe ( handleIO_ ) |
51 | import GetHostByAddr ( getHostByAddr ) | 51 | import GetHostByAddr ( getHostByAddr ) |
52 | import InterruptibleDelay | ||
52 | 53 | ||
53 | type TimeStamp = UTCTime | 54 | type TimeStamp = UTCTime |
54 | 55 | ||
@@ -118,16 +119,15 @@ tryForkOS action = catchIOError (forkOS action) $ \e -> do | |||
118 | -- persist for reasons I don't understand as of this writing. (See 'updateCache') | 119 | -- persist for reasons I don't understand as of this writing. (See 'updateCache') |
119 | rawForwardResolve :: | 120 | rawForwardResolve :: |
120 | DNSCache -> (Text -> IO ()) -> Int -> Text -> IO [SockAddr] | 121 | DNSCache -> (Text -> IO ()) -> Int -> Text -> IO [SockAddr] |
121 | rawForwardResolve dns fail timeout addrtext = do | 122 | rawForwardResolve dns onFail timeout addrtext = do |
122 | r <- atomically newEmptyTMVar | 123 | r <- atomically newEmptyTMVar |
123 | mvar <- atomically newEmptyTMVar | 124 | mvar <- interruptibleDelay |
124 | rt <- tryForkOS $ do | 125 | rt <- tryForkOS $ do |
125 | myThreadId >>= flip labelThread ("resolve."++show addrtext) | 126 | myThreadId >>= flip labelThread ("resolve."++show addrtext) |
126 | resolver r mvar | 127 | resolver r mvar |
127 | -- TODO: System.Timeout.timeout might be more appropriate than this | 128 | startDelay mvar timeout |
128 | -- hack involving throwTo (ErrorCall "Interrupteddelay"). | 129 | did <- atomically $ tryPutTMVar r [] |
129 | tt <- forkIO $ timer (fail addrtext) timeout r rt | 130 | when did (onFail addrtext) |
130 | atomically $ putTMVar mvar tt | ||
131 | atomically $ readTMVar r | 131 | atomically $ readTMVar r |
132 | where | 132 | where |
133 | resolver r mvar = do | 133 | resolver r mvar = do |
@@ -138,8 +138,7 @@ rawForwardResolve dns fail timeout addrtext = do | |||
138 | (Just "5269") | 138 | (Just "5269") |
139 | did <- atomically $ tryPutTMVar r xs | 139 | did <- atomically $ tryPutTMVar r xs |
140 | when did $ do | 140 | when did $ do |
141 | tt <- atomically $ readTMVar mvar | 141 | interruptDelay mvar |
142 | throwTo tt (ErrorCall "Interrupted delay") | ||
143 | utc <- getCurrentTime | 142 | utc <- getCurrentTime |
144 | atomically $ dnsObserve dns True utc $ map (addrtext,) xs | 143 | atomically $ dnsObserve dns True utc $ map (addrtext,) xs |
145 | return () | 144 | return () |
@@ -156,13 +155,6 @@ reportTimeout addrtext = do | |||
156 | hPutStrLn stderr $ "timeout resolving: "++show addrtext | 155 | hPutStrLn stderr $ "timeout resolving: "++show addrtext |
157 | -- killThread rt | 156 | -- killThread rt |
158 | 157 | ||
159 | timer :: forall t a. IO () -> Int -> TMVar [a] -> t -> IO () | ||
160 | timer fail timeout r rt = do | ||
161 | handle (\(ErrorCall _)-> return ()) $ do | ||
162 | threadDelay timeout | ||
163 | did <- atomically $ tryPutTMVar r [] | ||
164 | when did fail | ||
165 | |||
166 | unmap6mapped4 :: SockAddr -> SockAddr | 158 | unmap6mapped4 :: SockAddr -> SockAddr |
167 | unmap6mapped4 addr@(SockAddrInet6 port _ (0,0,0xFFFF,a) _) = | 159 | unmap6mapped4 addr@(SockAddrInet6 port _ (0,0,0xFFFF,a) _) = |
168 | SockAddrInet port (toBE32 a) | 160 | SockAddrInet port (toBE32 a) |
@@ -170,12 +162,13 @@ unmap6mapped4 addr = addr | |||
170 | 162 | ||
171 | rawReverseResolve :: | 163 | rawReverseResolve :: |
172 | DNSCache -> (SockAddr -> IO ()) -> Int -> SockAddr -> IO [Text] | 164 | DNSCache -> (SockAddr -> IO ()) -> Int -> SockAddr -> IO [Text] |
173 | rawReverseResolve dns fail timeout addr = do | 165 | rawReverseResolve dns onFail timeout addr = do |
174 | r <- atomically newEmptyTMVar | 166 | r <- atomically newEmptyTMVar |
175 | mvar <- atomically newEmptyTMVar | 167 | mvar <- interruptibleDelay |
176 | rt <- forkOS $ resolver r mvar | 168 | rt <- forkOS $ resolver r mvar |
177 | tt <- forkIO $ timer (fail addr) timeout r rt | 169 | startDelay mvar timeout |
178 | atomically $ putTMVar mvar tt | 170 | did <- atomically $ tryPutTMVar r [] |
171 | when did (onFail addr) | ||
179 | atomically $ readTMVar r | 172 | atomically $ readTMVar r |
180 | where | 173 | where |
181 | resolver r mvar = | 174 | resolver r mvar = |