diff options
author | joe <joe@jerkface.net> | 2018-06-19 16:34:03 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-19 16:34:03 -0400 |
commit | a564eb632153b7e194c7b09fe646817d621c8f40 (patch) | |
tree | 2467c8beb405143e65060f40c344cd172af3e743 | |
parent | c2df7b1192801862112a0f741014e27d490d12b3 (diff) |
Slower onion queries improve performance.
-rw-r--r-- | src/Network/BitTorrent/MainlineDHT.hs | 2 | ||||
-rw-r--r-- | src/Network/QueryResponse.hs | 18 | ||||
-rw-r--r-- | src/Network/Tox.hs | 16 |
3 files changed, 31 insertions, 5 deletions
diff --git a/src/Network/BitTorrent/MainlineDHT.hs b/src/Network/BitTorrent/MainlineDHT.hs index 63e67ad3..991473a9 100644 --- a/src/Network/BitTorrent/MainlineDHT.hs +++ b/src/Network/BitTorrent/MainlineDHT.hs | |||
@@ -629,6 +629,8 @@ newClient swarms addr = do | |||
629 | _ -> routing4 routing | 629 | _ -> routing4 routing |
630 | R.thisNode <$> readTVar var | 630 | R.thisNode <$> readTVar var |
631 | , clientResponseId = return | 631 | , clientResponseId = return |
632 | , clientEnterQuery = \_ -> return () | ||
633 | , clientLeaveQuery = \_ _ -> return () | ||
632 | } | 634 | } |
633 | 635 | ||
634 | -- TODO: Provide some means of shutting down these five auxillary threads: | 636 | -- TODO: Provide some means of shutting down these five auxillary threads: |
diff --git a/src/Network/QueryResponse.hs b/src/Network/QueryResponse.hs index 4f65b886..70c35542 100644 --- a/src/Network/QueryResponse.hs +++ b/src/Network/QueryResponse.hs | |||
@@ -31,7 +31,6 @@ import qualified Data.Word64Map as W64Map | |||
31 | ;import Data.Word64Map (Word64Map) | 31 | ;import Data.Word64Map (Word64Map) |
32 | import Data.Word | 32 | import Data.Word |
33 | import Data.Maybe | 33 | import Data.Maybe |
34 | import Data.Typeable | ||
35 | import Network.Socket | 34 | import Network.Socket |
36 | import Network.Socket.ByteString as B | 35 | import Network.Socket.ByteString as B |
37 | import System.Endian | 36 | import System.Endian |
@@ -180,7 +179,7 @@ sendQuery :: | |||
180 | -> a -- ^ The outbound query. | 179 | -> a -- ^ The outbound query. |
181 | -> addr -- ^ Destination address of query. | 180 | -> addr -- ^ Destination address of query. |
182 | -> IO (Maybe b) -- ^ The response, or 'Nothing' if it timed out. | 181 | -> IO (Maybe b) -- ^ The response, or 'Nothing' if it timed out. |
183 | sendQuery (Client net d err pending whoami _) meth q addr0 = do | 182 | sendQuery (Client net d err pending whoami _ enterQuery leaveQuery) meth q addr0 = do |
184 | mvar <- newEmptyMVar | 183 | mvar <- newEmptyMVar |
185 | (tid,addr,expiry) <- atomically $ do | 184 | (tid,addr,expiry) <- atomically $ do |
186 | tbl <- readTVar pending | 185 | tbl <- readTVar pending |
@@ -189,9 +188,11 @@ sendQuery (Client net d err pending whoami _) meth q addr0 = do | |||
189 | writeTVar pending tbl' | 188 | writeTVar pending tbl' |
190 | return (tid,addr,expiry) | 189 | return (tid,addr,expiry) |
191 | self <- whoami (Just addr) | 190 | self <- whoami (Just addr) |
191 | enterQuery tid | ||
192 | mres <- do sendMessage net addr (wrapQuery meth tid self addr q) | 192 | mres <- do sendMessage net addr (wrapQuery meth tid self addr q) |
193 | timeout expiry $ takeMVar mvar | 193 | timeout expiry $ takeMVar mvar |
194 | `catchIOError` (\e -> return Nothing) | 194 | `catchIOError` (\e -> return Nothing) |
195 | leaveQuery tid (isJust mres) | ||
195 | case mres of | 196 | case mres of |
196 | Just x -> return $ Just $ unwrapResponse meth x | 197 | Just x -> return $ Just $ unwrapResponse meth x |
197 | Nothing -> do | 198 | Nothing -> do |
@@ -224,6 +225,10 @@ data Client err meth tid addr x = forall tbl. Client | |||
224 | -- /tid/ includes a unique cryptographic nonce, then it should be | 225 | -- /tid/ includes a unique cryptographic nonce, then it should be |
225 | -- generated here. | 226 | -- generated here. |
226 | , clientResponseId :: tid -> IO tid | 227 | , clientResponseId :: tid -> IO tid |
228 | -- | The enter/leave methods are no-ops by default. They are useful for | ||
229 | -- serializing all queries for debugging purposes. | ||
230 | , clientEnterQuery :: tid -> IO () | ||
231 | , clientLeaveQuery :: tid -> Bool -> IO () | ||
227 | } | 232 | } |
228 | 233 | ||
229 | -- | An incoming message can be classified into three cases. | 234 | -- | An incoming message can be classified into three cases. |
@@ -449,7 +454,7 @@ handleMessage :: | |||
449 | -> addr | 454 | -> addr |
450 | -> x | 455 | -> x |
451 | -> IO (Maybe (x -> x)) | 456 | -> IO (Maybe (x -> x)) |
452 | handleMessage (Client net d err pending whoami responseID) addr plain = do | 457 | handleMessage (Client net d err pending whoami responseID _ _) addr plain = do |
453 | -- Just (Left e) -> do reportParseError err e | 458 | -- Just (Left e) -> do reportParseError err e |
454 | -- return $! Just id | 459 | -- return $! Just id |
455 | -- Just (Right (plain, addr)) -> do | 460 | -- Just (Right (plain, addr)) -> do |
@@ -553,3 +558,10 @@ udpTransport' bind_address = do | |||
553 | , closeTransport = close sock | 558 | , closeTransport = close sock |
554 | } | 559 | } |
555 | return (tr, sock) | 560 | return (tr, sock) |
561 | |||
562 | serializeClient :: Client err meth tid addr x -> IO (Client err meth tid addr x) | ||
563 | serializeClient c = do | ||
564 | mvar <- newMVar () | ||
565 | return $ c { clientEnterQuery = \tid -> takeMVar mvar | ||
566 | , clientLeaveQuery = \tid didRespond -> putMVar mvar () | ||
567 | } | ||
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs index ff42fb0d..4cff4f74 100644 --- a/src/Network/Tox.hs +++ b/src/Network/Tox.hs | |||
@@ -119,7 +119,6 @@ import Text.XXD | |||
119 | import qualified Data.HashMap.Strict as HashMap | 119 | import qualified Data.HashMap.Strict as HashMap |
120 | import Data.HashMap.Strict (HashMap) | 120 | import Data.HashMap.Strict (HashMap) |
121 | import qualified Data.Map.Strict as Map | 121 | import qualified Data.Map.Strict as Map |
122 | import Control.Concurrent (threadDelay) | ||
123 | import DPut | 122 | import DPut |
124 | import Network.Tox.Avahi | 123 | import Network.Tox.Avahi |
125 | 124 | ||
@@ -239,6 +238,8 @@ newClient drg net classify selfAddr handlers modifytbl modifynet = do | |||
239 | , clientPending = var | 238 | , clientPending = var |
240 | , clientAddress = selfAddr | 239 | , clientAddress = selfAddr |
241 | , clientResponseId = genNonce24 var | 240 | , clientResponseId = genNonce24 var |
241 | , clientEnterQuery = \_ -> return () | ||
242 | , clientLeaveQuery = \_ _ -> return () | ||
242 | } | 243 | } |
243 | in client | 244 | in client |
244 | return $ either mkclient mkclient tblvar handlers | 245 | return $ either mkclient mkclient tblvar handlers |
@@ -407,6 +408,14 @@ getOnionAlias crypto dhtself remoteNode = atomically $ do | |||
407 | return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing | 408 | return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing |
408 | 409 | ||
409 | 410 | ||
411 | retardSend micros client = do | ||
412 | mvar <- newMVar () :: IO (MVar ()) | ||
413 | return client { clientEnterQuery = \tid -> do | ||
414 | takeMVar mvar | ||
415 | threadDelay micros | ||
416 | putMVar mvar () | ||
417 | } | ||
418 | |||
410 | newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. | 419 | newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. |
411 | -> SockAddr -- ^ Bind-address to listen on. | 420 | -> SockAddr -- ^ Bind-address to listen on. |
412 | -> Maybe NetCryptoSessions -- ^ State of all one-on-one Tox links. | 421 | -> Maybe NetCryptoSessions -- ^ State of all one-on-one Tox links. |
@@ -484,9 +493,12 @@ newTox keydb addr mbSessionsState suppliedDHTKey = do | |||
484 | (hookQueries orouter' DHT.transactionKey) | 493 | (hookQueries orouter' DHT.transactionKey) |
485 | (const id) | 494 | (const id) |
486 | 495 | ||
496 | -- onionclientSerialized <- serializeClient onionclient | ||
497 | onionRetarded <- retardSend 200000 onionclient | ||
498 | |||
487 | return Tox | 499 | return Tox |
488 | { toxDHT = dhtclient | 500 | { toxDHT = dhtclient |
489 | , toxOnion = onionclient | 501 | , toxOnion = onionRetarded |
490 | , toxToRoute = onInbound (updateContactInfo roster) dtacrypt | 502 | , toxToRoute = onInbound (updateContactInfo roster) dtacrypt |
491 | , toxCrypto = addHandler (hPutStrLn stderr) (sessionPacketH sessionsState) cryptonet | 503 | , toxCrypto = addHandler (hPutStrLn stderr) (sessionPacketH sessionsState) cryptonet |
492 | , toxHandshakes = addHandler (hPutStrLn stderr) (handshakeH sessionsState) handshakes | 504 | , toxHandshakes = addHandler (hPutStrLn stderr) (handshakeH sessionsState) handshakes |