Property Segments
Segments
How many parts of one file DownloadFileAsync(string, string, SftpTransferOptions?, CancellationToken) fetches, or UploadFileAsync(string, string, SftpTransferOptions?, CancellationToken) writes,
at once, each over its own SFTP session on the same connection (default 1: one session). Each session is an SSH channel with its
own flow-control window, and on OpenSSH its own sftp-server process, so on a link with high latency several segments move
more than one session can: one session carries at most its 2 MiB window per round trip. It does not help where the bandwidth, or
the server's single connection, is the limit. Files smaller than 1 MiB per segment use fewer segments; the server may allow fewer
sessions (sshd's MaxSessions, 10 by default), and then fewer are used. Ignored with Resume (only a sequential
transfer leaves a prefix to resume from) and by DownloadAsync(string, Stream, SftpTransferOptions?, CancellationToken) and UploadAsync(Stream, string, SftpTransferOptions?, CancellationToken),
which use a stream in order.
public int Segments { get; init; }
Property Value
Remarks
A download: each extra session opens the file by path, so a file whose size or modification time has changed by then (replaced, or still being written) fails the download, which leaves no local file.
Uploads take it since 1.1 (in 1.0 it applied to downloads only): UploadFileAsync(string, string, SftpTransferOptions?, CancellationToken), and UploadDirectoryAsync(string, string, SftpTransferOptions?, CancellationToken) for each file of at least 1 MiB per segment. Set it to 1 for uploads to keep 1.0's behavior.
An upload: the first session creates (or truncates) the remote file and each extra session opens it for writing too, without truncating it; the ranges are written at their offsets, and nothing is allocated in advance (a range past the current end extends the file). A server that will not open a file for a second writer gets fewer sessions, down to one; a server that refuses a write out of order (one that stores files as objects, written from the start) has the rest of the file written in order over the first session, from as far as it holds the file without a gap.
A failed or cancelled upload cleans up: it waits for the answers to the writes already sent, then cuts the remote file to its unbroken start, so Resume can continue it; a server that cannot cut a file has it deleted instead. Each of those waits lasts at most OperationTimeout when it is set, otherwise 5 s. When the cleanup cannot finish in that time (the server stopped answering, or the session ended), the upload still throws its own exception (a cancellation stays an OperationCanceledException), with UploadCleanupIncompleteKey set to true in its Data: the remote file may then have gaps, so do not continue it with Resume; delete it or upload it again. Unlike a segmented upload, a sequential one (one segment) keeps what was written, and is cut back only when a write landed past one that failed (see Resume).