types

package
v0.7.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KB = 1 << 10
	MB = 1 << 20
	GB = 1 << 30

	// IncompleteSuffix is appended to files while downloading
	IncompleteSuffix = ".surge"
)

Size constants

View Source
const (
	MinChunk     = 2 * MB // Minimum chunk size
	AlignSize    = 4 * KB // Align chunks to 4KB for filesystem
	WorkerBuffer = 512 * KB

	// Batching constants for worker updates
	WorkerBatchSize     = 1 * MB                 // Batch updates until 1MB is downloaded
	WorkerBatchInterval = 200 * time.Millisecond // Or until 200ms passes
)

Chunk size constants for concurrent downloads

View Source
const (
	DefaultMaxIdleConns          = 100
	DefaultIdleConnTimeout       = 90 * time.Second
	DefaultTLSHandshakeTimeout   = 10 * time.Second
	DefaultResponseHeaderTimeout = 15 * time.Second
	DefaultExpectContinueTimeout = 1 * time.Second
	DialTimeout                  = 10 * time.Second
	KeepAliveDuration            = 30 * time.Second
	ProbeTimeout                 = 30 * time.Second
)

HTTP Client Tuning

View Source
const (
	MaxTaskRetries = 3
	RetryBaseDelay = 200 * time.Millisecond

	// Health check constants
	HealthCheckInterval = 1 * time.Second // How often to check worker health
	SlowWorkerThreshold = 0.50            // Restart if speed < x times of mean
	SlowWorkerGrace     = 5 * time.Second // Grace period before checking speed
	StallTimeout        = 5 * time.Second // Restart if no data for x seconds
	SpeedEMAAlpha       = 0.3             // EMA smoothing factor
)
View Source
const (
	PerHostMax = 64 // Max concurrent connections per host
)

Connection limits

View Source
const (
	ProgressChannelBuffer = 100
)

Channel buffer sizes

Variables

View Source
var (
	ErrPaused = errors.New("download paused")
)

Common errors

Functions

This section is empty.

Types

type ChunkStatus

type ChunkStatus int

ChunkStatus represents the status of a visualization chunk

const (
	ChunkPending     ChunkStatus = 0 // 00
	ChunkDownloading ChunkStatus = 1 // 01
	ChunkCompleted   ChunkStatus = 2 // 10 (Bit 2 set)
)

type DownloadConfig

type DownloadConfig struct {
	URL                string
	OutputPath         string
	DestPath           string // Full destination path (for resume state lookup)
	ID                 string
	Filename           string
	IsResume           bool // True if this is explicitly a resume, not a fresh download
	ProgressCh         chan<- any
	State              *ProgressState
	SavedState         *DownloadState    // Pre-loaded state for resume optimization
	Runtime            *RuntimeConfig    // Dynamic settings from user config
	Mirrors            []string          // List of mirror URLs (including primary)
	Headers            map[string]string // Custom HTTP headers to include in download requests
	IsExplicitCategory bool              // Used to override category routing from TUI
	TotalSize          int64             // Total size in bytes of the required download
	SupportsRange      bool              // Indicates whether the server supports range requests for concurrency
}

DownloadConfig contains all parameters needed to start a download

type DownloadEntry

type DownloadEntry struct {
	ID          string   `json:"id"`       // Unique ID of the download
	URLHash     string   `json:"url_hash"` // Hash of URL only (backward compatibility)
	URL         string   `json:"url"`
	DestPath    string   `json:"dest_path"`
	Filename    string   `json:"filename"`
	Status      string   `json:"status"`       // "paused", "completed", "error"
	TotalSize   int64    `json:"total_size"`   // File size in bytes
	Downloaded  int64    `json:"downloaded"`   // Bytes downloaded
	CompletedAt int64    `json:"completed_at"` // Unix timestamp when completed
	TimeTaken   int64    `json:"time_taken"`   // Duration in milliseconds (for completed)
	AvgSpeed    float64  `json:"avg_speed"`    // Average speed in bytes/sec (for completed)
	Mirrors     []string `json:"mirrors,omitempty"`
}

DownloadEntry represents a download in the master list

type DownloadState

type DownloadState struct {
	ID         string   `json:"id"`       // Unique ID of the download
	URLHash    string   `json:"url_hash"` // Hash of URL only (for master list compatibility)
	URL        string   `json:"url"`
	DestPath   string   `json:"dest_path"`
	TotalSize  int64    `json:"total_size"`
	Downloaded int64    `json:"downloaded"`
	Tasks      []Task   `json:"tasks"` // Remaining tasks
	Filename   string   `json:"filename"`
	CreatedAt  int64    `json:"created_at"` // Unix timestamp
	PausedAt   int64    `json:"paused_at"`  // Unix timestamp
	Elapsed    int64    `json:"elapsed"`    // Elapsed time in nanoseconds
	Mirrors    []string `json:"mirrors,omitempty"`

	// Bitmap state
	ChunkBitmap     []byte `json:"chunk_bitmap,omitempty"`
	ActualChunkSize int64  `json:"actual_chunk_size,omitempty"`

	// Integrity verification
	FileHash string `json:"file_hash,omitempty"` // SHA-256 hash of the .surge file at pause time
}

DownloadState represents persisted download state for resume

type DownloadStatus

type DownloadStatus struct {
	ID          string  `json:"id"`
	URL         string  `json:"url"`
	Filename    string  `json:"filename"`
	DestPath    string  `json:"dest_path,omitempty"` // Full absolute path to file
	TotalSize   int64   `json:"total_size"`
	Downloaded  int64   `json:"downloaded"`
	Progress    float64 `json:"progress"` // Percentage 0-100
	Speed       float64 `json:"speed"`    // MB/s
	Status      string  `json:"status"`   // "queued", "paused", "downloading", "completed", "error"
	Error       string  `json:"error,omitempty"`
	ETA         int64   `json:"eta"`         // Estimated seconds remaining
	Connections int     `json:"connections"` // Active connections
	AddedAt     int64   `json:"added_at"`    // Unix timestamp when added
	TimeTaken   int64   `json:"time_taken"`  // Duration in milliseconds (completed only)
	AvgSpeed    float64 `json:"avg_speed"`   // Average speed in bytes/sec (completed only)
}

DownloadStatus represents the transient status of an active download

type MasterList

type MasterList struct {
	Downloads []DownloadEntry `json:"downloads"`
}

MasterList holds all tracked downloads

type MirrorStatus

type MirrorStatus struct {
	URL    string
	Active bool
	Error  bool
}

type ProgressState

type ProgressState struct {
	ID            string
	Downloaded    atomic.Int64
	TotalSize     int64
	DestPath      string // Initial destination path
	Filename      string // Initial filename
	URL           string // Source URL
	StartTime     time.Time
	ActiveWorkers atomic.Int32
	Done          atomic.Bool
	Error         atomic.Pointer[error]
	Paused        atomic.Bool
	Pausing       atomic.Bool // Intermediate state: Pause requested but workers not yet exited

	VerifiedProgress  atomic.Int64  // Verified bytes written to disk (for UI progress)
	SessionStartBytes int64         // SessionStartBytes tracks how many bytes were already downloaded when the current session started
	SavedElapsed      time.Duration // Time spent in previous sessions

	Mirrors []MirrorStatus // Status of each mirror

	// Chunk Visualization (Bitmap)
	ChunkBitmap     []byte  // 2 bits per chunk
	ChunkProgress   []int64 // Bytes downloaded per chunk (runtime only, not persisted)
	ActualChunkSize int64   // Size of each actual chunk in bytes
	BitmapWidth     int     // Number of chunks tracked
	// contains filtered or unexported fields
}

func NewProgressState

func NewProgressState(id string, totalSize int64) *ProgressState

func (*ProgressState) FinalizePauseSession

func (ps *ProgressState) FinalizePauseSession(downloaded int64) time.Duration

FinalizePauseSession finalizes the current session for a pause transition. It keeps timing/data frozen while paused and returns total elapsed after finalize.

func (*ProgressState) FinalizeSession

func (ps *ProgressState) FinalizeSession(downloaded int64) (time.Duration, time.Duration)

FinalizeSession closes the current session and accumulates its elapsed time into total elapsed. It returns (sessionElapsed, totalElapsedAfterFinalize).

func (*ProgressState) GetBitmap

func (ps *ProgressState) GetBitmap() ([]byte, int, int64, int64, []int64)

GetBitmap returns a copy of the bitmap and metadata

func (*ProgressState) GetBitmapSnapshot

func (ps *ProgressState) GetBitmapSnapshot(includeProgress bool) ([]byte, int, int64, int64, []int64)

GetBitmapSnapshot returns a copy of bitmap metadata and optionally chunk progress.

func (*ProgressState) GetChunkState

func (ps *ProgressState) GetChunkState(index int) ChunkStatus

GetChunkState gets the 2-bit state for a specific chunk index (thread-safe)

func (*ProgressState) GetDestPath

func (ps *ProgressState) GetDestPath() string

func (*ProgressState) GetError

func (ps *ProgressState) GetError() error

func (*ProgressState) GetFilename

func (ps *ProgressState) GetFilename() string

func (*ProgressState) GetMirrors

func (ps *ProgressState) GetMirrors() []MirrorStatus

func (*ProgressState) GetProgress

func (ps *ProgressState) GetProgress() (downloaded int64, total int64, totalElapsed time.Duration, sessionElapsed time.Duration, connections int32, sessionStartBytes int64)

func (*ProgressState) GetSavedElapsed

func (ps *ProgressState) GetSavedElapsed() time.Duration

func (*ProgressState) GetURL

func (ps *ProgressState) GetURL() string

func (*ProgressState) InitBitmap

func (ps *ProgressState) InitBitmap(totalSize int64, chunkSize int64)

InitBitmap initializes the chunk bitmap

func (*ProgressState) IsPaused

func (ps *ProgressState) IsPaused() bool

func (*ProgressState) IsPausing

func (ps *ProgressState) IsPausing() bool

func (*ProgressState) Pause

func (ps *ProgressState) Pause()

func (*ProgressState) RecalculateProgress

func (ps *ProgressState) RecalculateProgress(remainingTasks []Task)

RecalculateProgress reconstructs ChunkProgress from remaining tasks (for resume)

func (*ProgressState) RestoreBitmap

func (ps *ProgressState) RestoreBitmap(bitmap []byte, actualChunkSize int64)

RestoreBitmap restores the chunk bitmap from saved state

func (*ProgressState) Resume

func (ps *ProgressState) Resume()

func (*ProgressState) SetCancelFunc

func (ps *ProgressState) SetCancelFunc(cancel context.CancelFunc)

func (*ProgressState) SetChunkProgress

func (ps *ProgressState) SetChunkProgress(progress []int64)

SetChunkProgress updates chunk progress array from external sources (e.g. remote events).

func (*ProgressState) SetChunkState

func (ps *ProgressState) SetChunkState(index int, status ChunkStatus)

SetChunkState sets the 2-bit state for a specific chunk index (thread-safe)

func (*ProgressState) SetDestPath

func (ps *ProgressState) SetDestPath(path string)

func (*ProgressState) SetError

func (ps *ProgressState) SetError(err error)

func (*ProgressState) SetFilename

func (ps *ProgressState) SetFilename(filename string)

func (*ProgressState) SetMirrors

func (ps *ProgressState) SetMirrors(mirrors []MirrorStatus)

func (*ProgressState) SetPausing

func (ps *ProgressState) SetPausing(pausing bool)

func (*ProgressState) SetSavedElapsed

func (ps *ProgressState) SetSavedElapsed(d time.Duration)

func (*ProgressState) SetTotalSize

func (ps *ProgressState) SetTotalSize(size int64)

func (*ProgressState) SetURL

func (ps *ProgressState) SetURL(url string)

func (*ProgressState) SyncSessionStart

func (ps *ProgressState) SyncSessionStart()

func (*ProgressState) UpdateChunkStatus

func (ps *ProgressState) UpdateChunkStatus(offset, length int64, status ChunkStatus)

UpdateChunkStatus updates the bitmap based on byte range

type RuntimeConfig

type RuntimeConfig struct {
	MaxConnectionsPerHost int
	UserAgent             string
	ProxyURL              string
	SequentialDownload    bool
	MinChunkSize          int64

	WorkerBufferSize      int
	MaxTaskRetries        int
	SlowWorkerThreshold   float64
	SlowWorkerGracePeriod time.Duration
	StallTimeout          time.Duration
	SpeedEmaAlpha         float64
}

RuntimeConfig holds dynamic settings that can override defaults

func ConvertRuntimeConfig

func ConvertRuntimeConfig(rc *config.RuntimeConfig) *RuntimeConfig

ConvertRuntimeConfig converts the app-level RuntimeConfig to the engine-level RuntimeConfig.

func (*RuntimeConfig) GetMaxConnectionsPerHost

func (r *RuntimeConfig) GetMaxConnectionsPerHost() int

GetMaxConnectionsPerHost returns configured value or default

func (*RuntimeConfig) GetMaxTaskRetries

func (r *RuntimeConfig) GetMaxTaskRetries() int

GetMaxTaskRetries returns configured value or default

func (*RuntimeConfig) GetMinChunkSize

func (r *RuntimeConfig) GetMinChunkSize() int64

GetMinChunkSize returns configured value or default

func (*RuntimeConfig) GetSlowWorkerGracePeriod

func (r *RuntimeConfig) GetSlowWorkerGracePeriod() time.Duration

GetSlowWorkerGracePeriod returns configured value or default

func (*RuntimeConfig) GetSlowWorkerThreshold

func (r *RuntimeConfig) GetSlowWorkerThreshold() float64

GetSlowWorkerThreshold returns configured value or default

func (*RuntimeConfig) GetSpeedEmaAlpha

func (r *RuntimeConfig) GetSpeedEmaAlpha() float64

GetSpeedEmaAlpha returns configured value or default

func (*RuntimeConfig) GetStallTimeout

func (r *RuntimeConfig) GetStallTimeout() time.Duration

GetStallTimeout returns configured value or default

func (*RuntimeConfig) GetUserAgent

func (r *RuntimeConfig) GetUserAgent() string

GetUserAgent returns the configured user agent or the default

func (*RuntimeConfig) GetWorkerBufferSize

func (r *RuntimeConfig) GetWorkerBufferSize() int

GetWorkerBufferSize returns configured value or default

type Task

type Task struct {
	Offset          int64         `json:"offset"`
	Length          int64         `json:"length"`
	SharedMaxOffset *atomic.Int64 `json:"-"` // Used to deduplicate byte counting in hedged requests
}

Task represents a byte range to download

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL