Add structured logging and better error handling
This commit is contained in:
1 parent
db4e3c9611
commit
68c7dd76f2
31 files changed
+640
-109
No files matched your search
@@ -3,6 +3,7 @@ package fetch
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -34,6 +35,14 @@ type Request struct {
|
||||
State *State
|
||||
Progress Progress
|
||||
Checkpoint func(State)
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
func (request Request) logger() *slog.Logger {
|
||||
if request.Logger != nil {
|
||||
return request.Logger
|
||||
}
|
||||
return slog.Default()
|
||||
}
|
||||
|
||||
type Downloader struct {
|
||||
@@ -90,11 +99,19 @@ func (downloader *Downloader) Download(ctx context.Context, request Request) (st
|
||||
}
|
||||
|
||||
func (downloader *Downloader) prepare(ctx context.Context, source *remoteFile, request Request, partPath string) (State, *http.Response, error) {
|
||||
if request.State != nil && request.State.valid() && fileExists(partPath) {
|
||||
state := *request.State
|
||||
source.validator = state.Validator
|
||||
source.totalBytes = state.TotalBytes
|
||||
return state, nil, nil
|
||||
if request.State != nil {
|
||||
if request.State.valid() && fileExists(partPath) {
|
||||
state := *request.State
|
||||
source.validator = state.Validator
|
||||
source.totalBytes = state.TotalBytes
|
||||
request.logger().Info("resuming from saved progress",
|
||||
"bytes_done", state.DoneBytes(),
|
||||
"bytes_total", state.TotalBytes,
|
||||
"parts", len(state.Segments),
|
||||
)
|
||||
return state, nil, nil
|
||||
}
|
||||
request.logger().Warn("saved progress is unusable, starting over", "part_file_exists", fileExists(partPath))
|
||||
}
|
||||
|
||||
probe, err := downloader.open(ctx, source, 0, 0)
|
||||
@@ -113,6 +130,13 @@ func (downloader *Downloader) prepare(ctx context.Context, source *remoteFile, r
|
||||
TotalBytes: source.totalBytes,
|
||||
Segments: planSegments(source.totalBytes, connections),
|
||||
}
|
||||
request.logger().Info("connected to server",
|
||||
"file", state.FileName,
|
||||
"bytes_total", state.TotalBytes,
|
||||
"supports_ranges", probe.Header.Get("Accept-Ranges") == "bytes",
|
||||
"has_validator", state.Validator != "",
|
||||
"parts", len(state.Segments),
|
||||
)
|
||||
return state, probe, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user