diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-07-07 03:42:14 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-07-07 03:42:14 +0400 |
commit | 46d8706b0edbe1d3103d1a9070282857b121a619 (patch) | |
tree | 3b3888e6df1463944ec7db1619b4126ab54d680c /src | |
parent | c4b95aaa5709a326167a9301a5a5b25500e299c4 (diff) |
+ Literate thread throttling.
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/BitTorrent/Internal.lhs | 70 |
1 files changed, 39 insertions, 31 deletions
diff --git a/src/Network/BitTorrent/Internal.lhs b/src/Network/BitTorrent/Internal.lhs index 3f8f0371..078920a4 100644 --- a/src/Network/BitTorrent/Internal.lhs +++ b/src/Network/BitTorrent/Internal.lhs | |||
@@ -170,7 +170,7 @@ | |||
170 | 170 | ||
171 | 171 | ||
172 | Thread layout | 172 | Thread layout |
173 | ------------- | 173 | ------------------------------------------------------------------------ |
174 | 174 | ||
175 | When client session created 2 new threads appear: | 175 | When client session created 2 new threads appear: |
176 | 176 | ||
@@ -193,20 +193,19 @@ When client\/swarm\/peer session gets closed kill the corresponding | |||
193 | threads, but flush data to disc. (for e.g. storage block map) | 193 | threads, but flush data to disc. (for e.g. storage block map) |
194 | 194 | ||
195 | So for e.g., in order to obtain our first block we need to run at | 195 | So for e.g., in order to obtain our first block we need to run at |
196 | least 7 threads: main thread, 2 client session thread, 3 swarm session | 196 | least 7 threads: main thread, 2 client session threads, 3 swarm session |
197 | threads and PeerSession thread. | 197 | threads and PeerSession thread. |
198 | 198 | ||
199 | > {----------------------------------------------------------------------- | 199 | Thread throttling |
200 | > Client session | 200 | ------------------------------------------------------------------------ |
201 | > -----------------------------------------------------------------------} | ||
202 | 201 | ||
203 | > {- NOTE: If we will not restrict number of threads we could end up | 202 | If we will not restrict number of threads we could end up |
204 | > with thousands of connected swarm and make no particular progress. | 203 | with thousands of connected swarm and make no particular progress. |
205 | > | 204 | |
206 | > Note also we do not bound number of swarms! This is not optimal | 205 | Note also we do not bound number of swarms! This is not optimal |
207 | > strategy because each swarm might have say 1 thread and we could end | 206 | strategy because each swarm might have say 1 thread and we could end |
208 | > up bounded by the meaningless limit. Bounding global number of p2p | 207 | up bounded by the meaningless limit. Bounding global number of p2p |
209 | > sessions should work better, and simpler.-} | 208 | sessions should work better, and simpler. |
210 | 209 | ||
211 | > -- | Each client might have a limited number of threads. | 210 | > -- | Each client might have a limited number of threads. |
212 | > type ThreadCount = Int | 211 | > type ThreadCount = Int |
@@ -215,16 +214,28 @@ threads and PeerSession thread. | |||
215 | > defaultThreadCount :: ThreadCount | 214 | > defaultThreadCount :: ThreadCount |
216 | > defaultThreadCount = 1000 | 215 | > defaultThreadCount = 1000 |
217 | 216 | ||
218 | > {- PERFORMANCE NOTE: keeping torrent metafiles in memory is a _bad_ | 217 | Torrent Map |
219 | > idea: for 1TB of data we need at least 100MB of metadata. (using 256KB | 218 | ------------------------------------------------------------------------ |
220 | > piece size). This solution do not scale further. Solution with | 219 | |
221 | > TorrentLoc is much better and takes much more less space, moreover it | 220 | TorrentMap is used to keep track all known torrents for the |
222 | > depends on count of torrents but not on count of data itself. To scale | 221 | client. When some peer trying to connect to us it's necessary to |
223 | > further, in future we might add something like database (for | 222 | dispatch appropriate 'SwarmSession' (or start new one if there are |
224 | > e.g. sqlite) for this kind of things.-} | 223 | none) in the listener loop: we only know 'InfoHash' from 'Handshake' |
225 | 224 | but nothing more. So to accept new 'PeerSession' we need to lookup | |
226 | > -- | Identifies location of | 225 | torrent metainfo and content files (if there are some) by the |
226 | 'InfoHash' and only after that enter exchange loop. | ||
227 | |||
228 | *PERFORMANCE NOTE:* keeping torrent metafiles in memory is a _bad_ | ||
229 | idea: for 1TB of data we need at least 100MB of metadata. (using 256KB | ||
230 | piece size). This solution do not scale further. Solution with | ||
231 | TorrentLoc is much better and takes much more less space, moreover it | ||
232 | depends on count of torrents but not on count of data itself. To scale | ||
233 | further, in future we might add something like database (for | ||
234 | e.g. sqlite) for this kind of things. | ||
235 | |||
236 | > -- | Local identification info location about | ||
227 | > data TorrentLoc = TorrentLoc { | 237 | > data TorrentLoc = TorrentLoc { |
238 | > -- | | ||
228 | > metafilePath :: FilePath | 239 | > metafilePath :: FilePath |
229 | > , dataPath :: FilePath | 240 | > , dataPath :: FilePath |
230 | > } | 241 | > } |
@@ -232,19 +243,16 @@ threads and PeerSession thread. | |||
232 | > validateTorrent :: TorrentLoc -> IO () | 243 | > validateTorrent :: TorrentLoc -> IO () |
233 | > validateTorrent = error "validateTorrent: not implemented" | 244 | > validateTorrent = error "validateTorrent: not implemented" |
234 | 245 | ||
235 | > -- | TorrentMap is used to keep track all known torrents for the | 246 | |
236 | > -- client. When some peer trying to connect to us it's necessary to | ||
237 | > -- dispatch appropriate 'SwarmSession' (or start new one if there are | ||
238 | > -- none) in the listener loop: we only know 'InfoHash' from | ||
239 | > -- 'Handshake' but nothing more. So to accept new 'PeerSession' we | ||
240 | > -- need to lookup torrent metainfo and content files (if there are | ||
241 | > -- some) by the 'InfoHash' and only after that enter exchange loop. | ||
242 | > -- | 247 | > -- |
243 | > type TorrentMap = HashMap InfoHash TorrentLoc | 248 | > type TorrentMap = HashMap InfoHash TorrentLoc |
244 | 249 | ||
245 | > {- NOTE: basically, client session should contain options which user | 250 | Client session |
246 | > app store in configuration files. (related to the protocol) Moreover | 251 | ------------------------------------------------------------------------ |
247 | > it should contain the all client identification info. (e.g. DHT) -} | 252 | |
253 | Basically, client session should contain options which user app store | ||
254 | in configuration files. (related to the protocol) Moreover it should | ||
255 | contain the all client identification info. (e.g. DHT) | ||
248 | 256 | ||
249 | > -- | Client session is the basic unit of bittorrent network, it has: | 257 | > -- | Client session is the basic unit of bittorrent network, it has: |
250 | > -- | 258 | > -- |