Provide a configurable progressInterval. (#15477)

This commit is contained in:
supakeen 2020-10-04 12:32:07 +02:00 • committed by GitHub
commit bbe2eb20df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 6 deletions

View file

@ -209,6 +209,9 @@
is similar to procs such as `sets.toHashSet` and `tables.toTable`. Previously, is similar to procs such as `sets.toHashSet` and `tables.toTable`. Previously,
it was necessary to create an empty IntSet and add items manually. it was necessary to create an empty IntSet and add items manually.
- Added `progressInterval` argument to `asyncftpclient.newAsyncFtpClient` to control the interval
at which progress callbacks are called.
## Language changes ## Language changes

View file

@ -59,6 +59,9 @@
## by specifying a ``onProgressChanged`` procedure to the ``store`` or ## by specifying a ``onProgressChanged`` procedure to the ``store`` or
## ``retrFile`` procedures. ## ``retrFile`` procedures.
## ##
## Procs that take an ``onProgressChanged`` callback will call this every
## ``progressInterval`` milliseconds.
##
## .. code-block::nim ## .. code-block::nim
## import asyncdispatch, asyncftpclient ## import asyncdispatch, asyncftpclient
## ##
@ -68,7 +71,7 @@
## echo("Current speed: ", speed, " kb/s") ## echo("Current speed: ", speed, " kb/s")
## ##
## proc main() {.async.} = ## proc main() {.async.} =
## var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test") ## var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test", progressInterval = 500)
## await ftp.connect() ## await ftp.connect()
## await ftp.store("file.txt", "/home/user/file.txt", onProgressChanged) ## await ftp.store("file.txt", "/home/user/file.txt", onProgressChanged)
## echo("File finished uploading") ## echo("File finished uploading")
@ -85,6 +88,7 @@ type
user*, pass*: string user*, pass*: string
address*: string address*: string
port*: Port port*: Port
progressInterval: int
jobInProgress*: bool jobInProgress*: bool
job*: FtpJob job*: FtpJob
dsockConnected*: bool dsockConnected*: bool
@ -303,7 +307,7 @@ proc getFile(ftp: AsyncFtpClient, file: File, total: BiggestInt,
assert ftp.dsockConnected assert ftp.dsockConnected
var progress = 0 var progress = 0
var progressInSecond = 0 var progressInSecond = 0
var countdownFut = sleepAsync(1000) var countdownFut = sleepAsync(ftp.progressInterval)
var dataFut = ftp.dsock.recv(BufferSize) var dataFut = ftp.dsock.recv(BufferSize)
while ftp.dsockConnected: while ftp.dsockConnected:
await dataFut or countdownFut await dataFut or countdownFut
@ -311,7 +315,7 @@ proc getFile(ftp: AsyncFtpClient, file: File, total: BiggestInt,
asyncCheck onProgressChanged(total, progress, asyncCheck onProgressChanged(total, progress,
progressInSecond.float) progressInSecond.float)
progressInSecond = 0 progressInSecond = 0
countdownFut = sleepAsync(1000) countdownFut = sleepAsync(ftp.progressInterval)
if dataFut.finished: if dataFut.finished:
let data = dataFut.read let data = dataFut.read
@ -359,7 +363,7 @@ proc doUpload(ftp: AsyncFtpClient, file: File,
var data = newString(4000) var data = newString(4000)
var progress = 0 var progress = 0
var progressInSecond = 0 var progressInSecond = 0
var countdownFut = sleepAsync(1000) var countdownFut = sleepAsync(ftp.progressInterval)
var sendFut: Future[void] = nil var sendFut: Future[void] = nil
while ftp.dsockConnected: while ftp.dsockConnected:
if sendFut == nil or sendFut.finished: if sendFut == nil or sendFut.finished:
@ -380,7 +384,7 @@ proc doUpload(ftp: AsyncFtpClient, file: File,
if countdownFut.finished: if countdownFut.finished:
asyncCheck onProgressChanged(total, progress, progressInSecond.float) asyncCheck onProgressChanged(total, progress, progressInSecond.float)
progressInSecond = 0 progressInSecond = 0
countdownFut = sleepAsync(1000) countdownFut = sleepAsync(ftp.progressInterval)
await countdownFut or sendFut await countdownFut or sendFut
@ -415,13 +419,14 @@ proc removeDir*(ftp: AsyncFtpClient, dir: string) {.async.} =
assertReply(await ftp.send("RMD " & dir), "250") assertReply(await ftp.send("RMD " & dir), "250")
proc newAsyncFtpClient*(address: string, port = Port(21), proc newAsyncFtpClient*(address: string, port = Port(21),
user, pass = ""): AsyncFtpClient = user, pass = "", progressInterval: int = 1000): AsyncFtpClient =
## Creates a new ``AsyncFtpClient`` object. ## Creates a new ``AsyncFtpClient`` object.
new result new result
result.user = user result.user = user
result.pass = pass result.pass = pass
result.address = address result.address = address
result.port = port result.port = port
result.progressInterval = progressInterval
result.dsockConnected = false result.dsockConnected = false
result.csock = newAsyncSocket() result.csock = newAsyncSocket()