diff options
-rw-r--r-- | src/Network/BitTorrent/DHT/ContactInfo.hs | 13 | ||||
-rw-r--r-- | src/Network/BitTorrent/MainlineDHT.hs | 20 | ||||
-rw-r--r-- | todo.txt | 6 |
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 | |||
239 | deleteOlderThan :: POSIXTime -> PeerStore -> PeerStore | ||
240 | deleteOlderThan 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 | |||
47 | import Data.Ord | 47 | import Data.Ord |
48 | import qualified Data.Serialize as S | 48 | import qualified Data.Serialize as S |
49 | import Data.Set (Set) | 49 | import Data.Set (Set) |
50 | import Data.Time.Clock.POSIX (POSIXTime) | 50 | import Data.Time.Clock.POSIX (POSIXTime, getPOSIXTime) |
51 | import Data.Torrent | 51 | import Data.Torrent |
52 | import Data.Typeable | 52 | import Data.Typeable |
53 | import Data.Word | 53 | import 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. | ||
668 | forkAnnouncedInfohashesGC :: TVar PeerStore -> IO ThreadId | ||
669 | forkAnnouncedInfohashesGC 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 | -- |
@@ -1,3 +1,5 @@ | |||
1 | tox: XEdDSA signature algorithm and key conversion. | ||
2 | |||
1 | maint: send patch to Vincent Hanquez to implement crypto_box_* | 3 | maint: send patch to Vincent Hanquez to implement crypto_box_* |
2 | 4 | ||
3 | tox: layerTransport handshake decryption: use (SecretKey,SockAddr) as the | 5 | tox: layerTransport handshake decryption: use (SecretKey,SockAddr) as the |
@@ -41,10 +43,6 @@ tox: cache diffie-helman secrets | |||
41 | 43 | ||
42 | tox: Chat support. | 44 | tox: Chat support. |
43 | 45 | ||
44 | bt: 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 | |||
48 | bt: Limit peers in get_peers response for UDP packet size limiting (around 1k). | 46 | bt: Limit peers in get_peers response for UDP packet size limiting (around 1k). |
49 | 47 | ||
50 | bt: Use LMDB backend for peer store (and nodes too?). | 48 | bt: Use LMDB backend for peer store (and nodes too?). |