Bandwidth limit optimisation
- should be slightly more accurate and use slightly fewer resources
This commit is contained in:
parent
8788f2d349
commit
4086253730
|
@ -75,7 +75,7 @@ class AdvancedDispatcher(asyncore.dispatcher):
|
|||
def writable(self):
|
||||
self.uploadChunk = AdvancedDispatcher._buf_len
|
||||
if asyncore.maxUploadRate > 0:
|
||||
self.uploadChunk = asyncore.uploadBucket
|
||||
self.uploadChunk = int(asyncore.uploadBucket)
|
||||
self.uploadChunk = min(self.uploadChunk, len(self.write_buf))
|
||||
return asyncore.dispatcher.writable(self) and \
|
||||
(self.connecting or (self.connected and self.uploadChunk > 0))
|
||||
|
@ -83,7 +83,7 @@ class AdvancedDispatcher(asyncore.dispatcher):
|
|||
def readable(self):
|
||||
self.downloadChunk = AdvancedDispatcher._buf_len
|
||||
if asyncore.maxDownloadRate > 0:
|
||||
self.downloadChunk = asyncore.downloadBucket
|
||||
self.downloadChunk = int(asyncore.downloadBucket)
|
||||
try:
|
||||
if self.expectBytes > 0 and not self.fullyEstablished:
|
||||
self.downloadChunk = min(self.downloadChunk, self.expectBytes - len(self.read_buf))
|
||||
|
|
|
@ -112,6 +112,8 @@ uploadBucket = 0
|
|||
sentBytes = 0
|
||||
|
||||
def read(obj):
|
||||
if not can_receive():
|
||||
return
|
||||
try:
|
||||
obj.handle_read_event()
|
||||
except _reraised_exceptions:
|
||||
|
@ -120,6 +122,8 @@ def read(obj):
|
|||
obj.handle_error()
|
||||
|
||||
def write(obj):
|
||||
if not can_send():
|
||||
return
|
||||
try:
|
||||
obj.handle_write_event()
|
||||
except _reraised_exceptions:
|
||||
|
@ -136,12 +140,18 @@ def set_rates(download, upload):
|
|||
downloadTimestamp = time.time()
|
||||
uploadTimestamp = time.time()
|
||||
|
||||
def can_receive():
|
||||
return maxDownloadRate == 0 or downloadBucket > 0
|
||||
|
||||
def can_send():
|
||||
return maxUploadRate == 0 or uploadBucket > 0
|
||||
|
||||
def update_received(download=0):
|
||||
global receivedBytes, downloadBucket, downloadTimestamp
|
||||
currentTimestamp = time.time()
|
||||
receivedBytes += download
|
||||
if maxDownloadRate > 0:
|
||||
bucketIncrease = int(maxDownloadRate * (currentTimestamp - downloadTimestamp))
|
||||
bucketIncrease = maxDownloadRate * (currentTimestamp - downloadTimestamp)
|
||||
downloadBucket += bucketIncrease
|
||||
if downloadBucket > maxDownloadRate:
|
||||
downloadBucket = int(maxDownloadRate)
|
||||
|
@ -153,7 +163,7 @@ def update_sent(upload=0):
|
|||
currentTimestamp = time.time()
|
||||
sentBytes += upload
|
||||
if maxUploadRate > 0:
|
||||
bucketIncrease = int(maxUploadRate * (currentTimestamp - uploadTimestamp))
|
||||
bucketIncrease = maxUploadRate * (currentTimestamp - uploadTimestamp)
|
||||
uploadBucket += bucketIncrease
|
||||
if uploadBucket > maxUploadRate:
|
||||
uploadBucket = int(maxUploadRate)
|
||||
|
@ -170,9 +180,9 @@ def _exception(obj):
|
|||
|
||||
def readwrite(obj, flags):
|
||||
try:
|
||||
if flags & select.POLLIN:
|
||||
if flags & select.POLLIN and can_receive():
|
||||
obj.handle_read_event()
|
||||
if flags & select.POLLOUT:
|
||||
if flags & select.POLLOUT and can_send():
|
||||
obj.handle_write_event()
|
||||
if flags & select.POLLPRI:
|
||||
obj.handle_expt_event()
|
||||
|
|
Reference in New Issue
Block a user