Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion docs/updating.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ err = update.Install(update.InstallOptions{
}, application.Quit)
```

A download that stops part way returns an error wrapping
`update.ErrInterrupted`: the connection failed, nothing arrived for a minute,
the host answered with a temporary status (408, 429 or 5xx), or `ctx` ended (the
error then wraps `context.Canceled` or `context.DeadlineExceeded` as well). The
bytes that arrived stay in `downloadDir`, and calling `Download` again with the
same directory sends a `Range` request for the rest; a host that ignores it
sends the whole file, which is fine. When the directory already holds the
verified file, `Download` returns it without a request. Keep `downloadDir` per
release and remove it once the release is installed or skipped. Two processes
must not download the same asset into the same directory at once.
`DownloadPatch` behaves the same way, so a cut-off patch is worth resuming
before falling back to the full archive.

`Install` returns only after it has started the helper that replaces the
application; the helper waits for this process to exit. If `Install` returns an
error nothing has been changed and `quit` was not called.
Expand Down Expand Up @@ -294,7 +307,10 @@ installer are per-user by default and need no elevation.
another operating system or architecture.
- **Exact bytes.** A download is written while it is hashed and is kept only
when both its length and SHA-256 match the feed (`ErrSize`, `ErrChecksum`).
A refused download leaves no file behind. `Install` hashes the file again
A refused download leaves no file behind. An interrupted one keeps its bytes
in a hidden file named by the feed's digest, so they are only ever continued
with the rest of the same file, and the whole file is hashed again before it
is kept. `Install` hashes the file again
immediately before handing it to the platform, because the file sits on disk
between the two calls.
- **Bounded.** `latest.json` is limited to 1 MiB, its signature to 4 KiB, an
Expand Down
15 changes: 13 additions & 2 deletions update/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ var (
// ErrChecksum reports a download whose SHA-256 is not the one the feed
// declared.
ErrChecksum = errors.New("update: download does not match its checksum")
// ErrInterrupted reports a download that stopped before the file was
// complete: the connection failed, nothing arrived for a minute, the host
// answered with a temporary error, or the caller's context ended (the error
// then wraps the context's error too). The bytes that arrived stay in the
// directory, and calling Download or DownloadPatch again with the same
// directory asks the host only for the rest.
ErrInterrupted = errors.New("update: download interrupted")
)

// Config configures a Client. FeedURL, PublicKeys and Current are required.
Expand All @@ -69,7 +76,8 @@ type Config struct {
// HTTP overrides the client used for the feed and its signature. Nil uses a
// client with a 30 second timeout. The archive download always uses a client
// with no total timeout, because a large installer may take longer than any
// sensible request timeout; the context passed to Download cancels it.
// sensible request timeout; the context passed to Download cancels it, and
// a connection that delivers nothing for a minute ends with ErrInterrupted.
HTTP *http.Client
// MaxAssetBytes bounds Download. Zero means DefaultMaxAssetBytes.
MaxAssetBytes int64
Expand All @@ -91,7 +99,9 @@ type Client struct {
assetHTTP *http.Client
maxAsset int64
maxManifest int64
now func() time.Time
// stall ends a download that has received nothing for this long.
stall time.Duration
now func() time.Time
}

// New validates config and returns a Client.
Expand Down Expand Up @@ -147,6 +157,7 @@ func New(config Config) (*Client, error) {
assetHTTP: assetHTTP,
maxAsset: maxAsset,
maxManifest: DefaultMaxManifestBytes,
stall: stallTimeout,
now: now,
}, nil
}
Expand Down
6 changes: 4 additions & 2 deletions update/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
// it is newer than the running version: a check that finds nothing newer
// returns (nil, nil). Download streams the archive for the running platform,
// hashing as it writes, and refuses a file whose size or digest is not the one
// the feed declared. Neither function sends an identifier, telemetry or
// anything else the caller did not configure.
// the feed declared. A transfer that is cut off, goes silent for a minute or is
// cancelled reports ErrInterrupted and keeps what arrived, so calling Download
// again with the same directory asks only for the rest. Neither function sends
// an identifier, telemetry or anything else the caller did not configure.
//
// # What this package does not do
//
Expand Down
Loading
Loading