Skip to content

Commit 70668e4

Browse files
authored
Merge pull request #31 from JoyStream/development
dev to master - v0.2.1
2 parents bd247e6 + 4d33308 commit 70668e4

6 files changed

Lines changed: 29 additions & 26 deletions

File tree

conan_package/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class ProtocolSessionBase(ConanFile):
55
name = "ProtocolSession"
6-
version = "0.2.0"
6+
version = "0.2.1"
77
license = "(c) JoyStream Inc. 2016-2017"
88
url = "https://github.com/JoyStream/protocol_session-cpp.git"
99
repo_ssh_url = "git@github.com:JoyStream/protocol_session-cpp.git"

sources/include/protocol_session/Session.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ namespace protocol_session {
166166
const SentPayment<ConnectionIdType> & sentPayment,
167167
const protocol_wire::BuyerTerms & terms,
168168
const TorrentPieceInformation & information,
169-
const AllSellersGone & allSellersGone) {
169+
const AllSellersGone & allSellersGone,
170+
std::chrono::duration<double> maxTimeToServicePiece) {
170171

171172
// Prepare for exiting current state
172173
switch(_mode) {
@@ -210,7 +211,8 @@ namespace protocol_session {
210211
sentPayment,
211212
terms,
212213
information,
213-
allSellersGone);
214+
allSellersGone,
215+
maxTimeToServicePiece);
214216
}
215217

216218
template <class ConnectionIdType>

sources/include/protocol_session/Session.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <protocol_session/SessionState.hpp>
1616

1717
#include <unordered_map>
18+
#include <chrono>
1819

1920
// ConnectionIdType: Type for identifying connections.
2021
// 1) must be possible to use as key in std::map
@@ -87,7 +88,8 @@ namespace detail {
8788
const SentPayment<ConnectionIdType> &,
8889
const protocol_wire::BuyerTerms &,
8990
const TorrentPieceInformation &,
90-
const AllSellersGone &);
91+
const AllSellersGone &,
92+
std::chrono::duration<double> = std::chrono::duration<double>::zero());
9193

9294
/**
9395
* Warning: Do not call any of these operations

sources/include/protocol_session/detail/Buying.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ namespace detail {
2828
const SentPayment<ConnectionIdType> & sentPayment,
2929
const protocol_wire::BuyerTerms & terms,
3030
const TorrentPieceInformation & information,
31-
const AllSellersGone & allSellersGone)
31+
const AllSellersGone & allSellersGone,
32+
std::chrono::duration<double> maxTimeToServicePiece)
3233
: _session(session)
3334
, _removedConnection(removedConnection)
3435
, _fullPieceArrived(fullPieceArrived)
@@ -37,7 +38,8 @@ namespace detail {
3738
, _terms(terms)
3839
, _numberOfMissingPieces(0)
3940
, _allSellersGone(allSellersGone)
40-
, _maxConcurrentRequests(4) {
41+
, _maxConcurrentRequests(4)
42+
, _maxTimeToServicePiece(maxTimeToServicePiece) {
4143
//, _lastStartOfSendingInvitations(0) {
4244

4345
// Setup pieces
@@ -86,20 +88,6 @@ namespace detail {
8688
removeConnection(id, DisconnectCause::client);
8789
}
8890

89-
template <class ConnectionIdType>
90-
void Buying<ConnectionIdType>::disconnectSlowSellers(const std::chrono::duration<double> & limit) {
91-
92-
for(auto mapping : _sellers) {
93-
auto seller = mapping.second;
94-
95-
if (seller.isGone()) continue;
96-
97-
if (seller.servicingPieceHasTimedOut(limit)) {
98-
removeConnection(seller.connection()->connectionId(), DisconnectCause::seller_servicing_piece_has_timed_out);
99-
}
100-
}
101-
}
102-
10391
template <class ConnectionIdType>
10492
void Buying<ConnectionIdType>::validPieceReceivedOnConnection(detail::Seller<ConnectionIdType> &seller, int index) {
10593
// Cannot happen when stopped, as there are no connections
@@ -286,8 +274,8 @@ namespace detail {
286274
// Only process if we are active
287275
if(_session->_state == SessionState::started) {
288276

277+
// Disconnect timed out sellers
289278
// Allocate pieces if we are downloading
290-
// Timeout sellers if they have not seviced a piece in time.
291279
// Reset state to allow restarting downloading after all sellers are gone
292280
if(_state == BuyingState::downloading) {
293281

@@ -296,8 +284,16 @@ namespace detail {
296284
// Reference to seller
297285
detail::Seller<ConnectionIdType> & s = mapping.second;
298286

287+
if (s.isGone()) continue;
288+
289+
// Disconnect if seller timed-out servicing request
290+
if (s.servicingPieceHasTimedOut(_maxTimeToServicePiece)) {
291+
removeConnection(s.connection()->connectionId(), DisconnectCause::seller_servicing_piece_has_timed_out);
292+
continue;
293+
}
294+
299295
// A seller may be waiting to be assigned a new piece
300-
if(!s.isGone() && s.piecesAwaitingArrival().size() == 0) {
296+
if(s.piecesAwaitingArrival().size() == 0) {
301297

302298
// This can happen when a seller has previously uploaded a valid piece,
303299
// but there were no unassigned pieces at that time,

sources/include/protocol_session/detail/Buying.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class Buying {
4848
const SentPayment<ConnectionIdType> &,
4949
const protocol_wire::BuyerTerms &,
5050
const TorrentPieceInformation &,
51-
const AllSellersGone &);
51+
const AllSellersGone &,
52+
std::chrono::duration<double> = std::chrono::duration<double>::zero());
5253

5354
//// Connection level client events
5455

@@ -58,9 +59,6 @@ class Buying {
5859
// Remove connection
5960
void removeConnection(const ConnectionIdType &);
6061

61-
// Disconnect seller connections if it has taken longer than `limit` to service next expected piece
62-
void disconnectSlowSellers(const std::chrono::duration<double> & limit);
63-
6462
// Transition to BuyingState::sending_invitations
6563
void startDownloading(const Coin::Transaction & contractTx,
6664
const PeerToStartDownloadInformationMap<ConnectionIdType> & peerToStartDownloadInformationMap);
@@ -188,6 +186,8 @@ class Buying {
188186
// Maximum number of concurrent requests to send before waiting for piece responses
189187
// The optimum value depends on many factors. It is hardcoded to 4 for now.
190188
const int _maxConcurrentRequests;
189+
190+
std::chrono::duration<double> _maxTimeToServicePiece;
191191
};
192192

193193
}

sources/include/protocol_session/detail/Seller.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ namespace detail {
127127
if(_piecesAwaitingArrival.size() == 0)
128128
return false;
129129

130+
if(timeOutLimit == std::chrono::duration<double>::zero())
131+
return false;
132+
130133
// Get current time
131134
auto now = std::chrono::high_resolution_clock::now();
132135

0 commit comments

Comments
 (0)