Skip to content

Configuration

downloader.zig provides extensive configuration options to customize download behavior.

Config Struct

All options are set via the Config struct:

zig
const downloader = @import("downloader");

var config = downloader.Config.default();
config.max_retries = 5;
config.resume_downloads = true;
config.file_exists_action = .rename_with_number;

var client = try downloader.Client.init(allocator, config);

Configuration Options

Update Checking

OptionTypeDefaultDescription
enable_update_checkbooltrueAutomatically check for library updates once

By default, the library checks for updates from the GitHub repository at the start of the first download in a process. You can disable this by setting enable_update_check to false.

zig
var config = downloader.Config.default();
config.enable_update_check = false; // Disable auto-check

Retry Settings

OptionTypeDefaultDescription
max_retriesu323Maximum retry attempts for transient failures
retry_delay_msu641000Base delay between retries (ms)
max_retry_delay_msu6430000Maximum delay cap for exponential backoff
exponential_backoffbooltrueUse exponential backoff for retries
zig
var config = downloader.Config.default();
config.max_retries = 5;
config.retry_delay_ms = 500;
config.max_retry_delay_ms = 60000;
config.exponential_backoff = true;

Connection Settings

OptionTypeDefaultDescription
connect_timeout_msu6430000Connection timeout (ms)
read_timeout_msu640Read timeout (ms, 0=none)
max_redirectsu3210Maximum redirects to follow

File Handling

OptionTypeDefaultDescription
resume_downloadsboolfalseAttempt to resume partial downloads
file_exists_actionFileExistsAction.rename_with_numberHow to handle existing files
filename_strategyFilenameStrategy.use_providedHow to resolve the output filename

Buffer & Performance

OptionTypeDefaultDescription
buffer_sizeusize65536Download buffer size in bytes

Preset Configurations

Default

zig
const config = downloader.Config.default();

Sensible defaults suitable for most use cases.

Large Files

zig
const config = downloader.Config.forLargeFiles();

Optimized for large file downloads:

  • 1 MB buffer
  • Resume enabled
  • Use temporary file
  • 5 retries
  • Exponential backoff

Small Files

zig
const config = downloader.Config.forSmallFiles();

Optimized for small, quick downloads:

  • 16 KB buffer
  • 2 retries
  • Resume disabled

Complete Example

zig
const std = @import("std");
const downloader = @import("downloader");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    // Create custom configuration
    var config = downloader.Config.default();

    // Disable auto update check
    config.enable_update_check = false;

    // Retry settings
    config.max_retries = 5;
    config.exponential_backoff = true;

    // File handling
    config.resume_downloads = true;
    config.file_exists_action = .rename_with_number;

    // Performance
    config.buffer_size = 128 * 1024;

    // Create client
    var client = try downloader.Client.init(allocator, config);
    defer client.deinit();

    // Download
    _ = try client.download(
        "https://example.com/file.zip",
        "file.zip",
        null
    );
}

Released under the MIT License.