summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-09 15:59:22 -0400
committerjoe <joe@jerkface.net>2018-06-09 15:59:22 -0400
commite4e4650d004cba42bfd8897d9658bfcaec82fb6d (patch)
tree3a4476002f04b099dd3e02072010efeed2579222
parente75cc83de37b628418c313b61db8864a04763562 (diff)
bittorrent: Expire old peer announcements.
-rw-r--r--src/Network/BitTorrent/DHT/ContactInfo.hs13
-rw-r--r--src/Network/BitTorrent/MainlineDHT.hs20
-rw-r--r--todo.txt6
3 files changed, 33 insertions, 6 deletions
diff --git a/src/Network/BitTorrent/DHT/ContactInfo.hs b/src/Network/BitTorrent/DHT/ContactInfo.hs
index c8187772..dfc93ed7 100644
--- a/src/Network/BitTorrent/DHT/ContactInfo.hs
+++ b/src/Network/BitTorrent/DHT/ContactInfo.hs
@@ -5,6 +5,7 @@ module Network.BitTorrent.DHT.ContactInfo
5 , Network.BitTorrent.DHT.ContactInfo.lookup 5 , Network.BitTorrent.DHT.ContactInfo.lookup
6 , Network.BitTorrent.DHT.ContactInfo.freshPeers 6 , Network.BitTorrent.DHT.ContactInfo.freshPeers
7 , Network.BitTorrent.DHT.ContactInfo.insertPeer 7 , Network.BitTorrent.DHT.ContactInfo.insertPeer
8 , deleteOlderThan
8 , knownSwarms 9 , knownSwarms
9 ) where 10 ) where
10 11
@@ -234,3 +235,15 @@ insertPeer !ih !name !a !(PeerStore m) = seq a' $ PeerStore (HM.insertWith swarm
234 where 235 where
235 a' = SwarmData { peers = PSQ.singleton a 0 236 a' = SwarmData { peers = PSQ.singleton a 0
236 , name = name } 237 , name = name }
238
239deleteOlderThan :: POSIXTime -> PeerStore -> PeerStore
240deleteOlderThan cutoff (PeerStore m) = PeerStore $ HM.mapMaybe gc m
241 where
242 gc :: SwarmData -> Maybe SwarmData
243 gc swarms = fmap (\ps -> swarms { peers = ps }) $ gcPSQ (peers swarms)
244
245 gcPSQ :: PSQKey a => PSQ a Timestamp -> Maybe (PSQ a Timestamp)
246 gcPSQ ps = case minView ps of
247 Nothing -> Nothing
248 Just (_ :-> tm, ps') | tm < cutoff -> gcPSQ ps'
249 Just _ -> Just ps
diff --git a/src/Network/BitTorrent/MainlineDHT.hs b/src/Network/BitTorrent/MainlineDHT.hs
index f43b070c..63e67ad3 100644
--- a/src/Network/BitTorrent/MainlineDHT.hs
+++ b/src/Network/BitTorrent/MainlineDHT.hs
@@ -47,7 +47,7 @@ import Data.Monoid
47import Data.Ord 47import Data.Ord
48import qualified Data.Serialize as S 48import qualified Data.Serialize as S
49import Data.Set (Set) 49import Data.Set (Set)
50import Data.Time.Clock.POSIX (POSIXTime) 50import Data.Time.Clock.POSIX (POSIXTime, getPOSIXTime)
51import Data.Torrent 51import Data.Torrent
52import Data.Typeable 52import Data.Typeable
53import Data.Word 53import Data.Word
@@ -631,7 +631,7 @@ newClient swarms addr = do
631 , clientResponseId = return 631 , clientResponseId = return
632 } 632 }
633 633
634 -- TODO: Provide some means of shutting down these four auxillary threads: 634 -- TODO: Provide some means of shutting down these five auxillary threads:
635 635
636 fork $ fix $ \again -> do 636 fork $ fix $ \again -> do
637 myThreadId >>= flip labelThread "addr4" 637 myThreadId >>= flip labelThread "addr4"
@@ -656,8 +656,24 @@ newClient swarms addr = do
656 refresh_thread4 <- forkPollForRefresh $ refresher4 routing 656 refresh_thread4 <- forkPollForRefresh $ refresher4 routing
657 refresh_thread6 <- forkPollForRefresh $ refresher6 routing 657 refresh_thread6 <- forkPollForRefresh $ refresher6 routing
658 658
659 forkAnnouncedInfohashesGC (contactInfo swarms)
660
659 return (client, routing, bootstrap (refresher4 routing), bootstrap (refresher6 routing)) 661 return (client, routing, bootstrap (refresher4 routing), bootstrap (refresher6 routing))
660 662
663-- Note that you should call .put() every hour for content that you want to
664-- keep alive, since nodes may discard data nodes older than 2 hours. (source:
665-- https://www.npmjs.com/package/bittorrent-dht)
666--
667-- This function will discard records between 3 and 6 hours old.
668forkAnnouncedInfohashesGC :: TVar PeerStore -> IO ThreadId
669forkAnnouncedInfohashesGC vpeers = fork $ do
670 myThreadId >>= flip labelThread "gc:bt-peers"
671 fix $ \loop -> do
672 cutoff <- getPOSIXTime
673 threadDelay 10800000000 -- 3 hours
674 atomically $ modifyTVar' vpeers $ deleteOlderThan cutoff
675 loop
676
661-- | Modifies a purely random 'NodeId' to one that is related to a given 677-- | Modifies a purely random 'NodeId' to one that is related to a given
662-- routable address in accordance with BEP 42. 678-- routable address in accordance with BEP 42.
663-- 679--
diff --git a/todo.txt b/todo.txt
index d4b2d828..94fe6cd3 100644
--- a/todo.txt
+++ b/todo.txt
@@ -1,3 +1,5 @@
1tox: XEdDSA signature algorithm and key conversion.
2
1maint: send patch to Vincent Hanquez to implement crypto_box_* 3maint: send patch to Vincent Hanquez to implement crypto_box_*
2 4
3tox: layerTransport handshake decryption: use (SecretKey,SockAddr) as the 5tox: layerTransport handshake decryption: use (SecretKey,SockAddr) as the
@@ -41,10 +43,6 @@ tox: cache diffie-helman secrets
41 43
42tox: Chat support. 44tox: Chat support.
43 45
44bt: Collect PeerStore garbage: "Note that you should call .put() every hour for
45 content that you want to keep alive, since nodes may discard data nodes
46 older than 2 hours." (source: https://www.npmjs.com/package/bittorrent-dht)
47
48bt: Limit peers in get_peers response for UDP packet size limiting (around 1k). 46bt: Limit peers in get_peers response for UDP packet size limiting (around 1k).
49 47
50bt: Use LMDB backend for peer store (and nodes too?). 48bt: Use LMDB backend for peer store (and nodes too?).