docker

package
v0.0.0-...-8929951 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDockerNotAvailable is returned when Docker is not available
	ErrDockerNotAvailable = errors.New("docker is not available")

	// ErrImagePullFailed is returned when image pull fails
	ErrImagePullFailed = errors.New("failed to pull docker image")

	// ErrContainerFailed is returned when container execution fails
	ErrContainerFailed = errors.New("container execution failed")

	// ErrTimeout is returned when execution times out
	ErrTimeout = errors.New("execution timeout")

	// ErrResourceLimit is returned when resource limit is exceeded
	ErrResourceLimit = errors.New("resource limit exceeded")

	// ErrNoGeneratedFiles is returned when no files were generated
	ErrNoGeneratedFiles = errors.New("no files were generated")
)
View Source
var (
	DefaultMemoryLimit = config.DefaultDockerMemoryLimit
	DefaultCPULimit    = config.DefaultDockerCPULimit
	DefaultTimeout     = config.DefaultDockerTimeout
)

ResourceLimits defines default resource limits

Functions

This section is empty.

Types

type DockerRunner

type DockerRunner struct {
	// contains filtered or unexported fields
}

DockerRunner implements the Runner interface using Docker

func NewDockerRunner

func NewDockerRunner() (*DockerRunner, error)

NewDockerRunner creates a new Docker runner

func (*DockerRunner) Cleanup

func (r *DockerRunner) Cleanup(ctx context.Context) error

Cleanup removes stopped containers and unused images

func (*DockerRunner) Close

func (r *DockerRunner) Close() error

Close releases resources

func (*DockerRunner) Execute

Execute runs a compilation in a Docker container

Defer Execution Order

This function uses multiple defer statements for cleanup and metrics. Defers execute in LIFO (Last In, First Out) order, so the sequence is:

  1. defer result.Duration = ... (line 57) - DECLARED FIRST, RUNS LAST
  2. defer os.RemoveAll(inputDir) (line 89) - runs second-to-last
  3. defer os.RemoveAll(outputDir) (line 96) - runs third-to-last
  4. defer container cleanup (if container created) - RUNS FIRST

Cleanup order (earliest to latest):

  1. Container cleanup (if exists) - Release Docker resources
  2. outputDir cleanup - Remove temporary output directory
  3. inputDir cleanup - Remove temporary input directory
  4. Duration measurement - Record total execution time

Why this order matters:

  • Container MUST be cleaned up before directories (container may have locks)
  • Directories cleaned up before duration measurement (cleanup time included)
  • Duration measurement last ensures we capture full execution time including cleanup

If you add new defer statements, consider their position carefully:

  • Resource cleanup (files, containers) should be deferred AFTER duration measurement
  • Metric recording should be deferred BEFORE resource cleanup

func (*DockerRunner) PullImage

func (r *DockerRunner) PullImage(ctx context.Context, imageRef string) error

PullImage ensures the Docker image is available locally

type ExecutionRequest

type ExecutionRequest struct {
	// Docker configuration
	Image string
	Tag   string

	// Input files
	ProtoFiles []codegen.ProtoFile
	WorkDir    string // Working directory inside container

	// Protoc command
	ProtocFlags []string
	OutputDir   string // Output directory inside container

	// Resource limits
	MemoryLimit int64         // Memory limit in bytes (see config.DefaultDockerMemoryLimit)
	CPULimit    float64       // CPU limit (see config.DefaultDockerCPULimit)
	Timeout     time.Duration // Execution timeout (see config.DefaultDockerTimeout)

	// Environment variables
	Env map[string]string
}

ExecutionRequest represents a Docker execution request

type ExecutionResult

type ExecutionResult struct {
	Success        bool
	ExitCode       int
	Stdout         string
	Stderr         string
	Duration       time.Duration
	GeneratedFiles []codegen.GeneratedFile
	Error          error
}

ExecutionResult represents the result of a Docker execution

type Runner

type Runner interface {
	// Execute runs a compilation in a Docker container
	Execute(ctx context.Context, req *ExecutionRequest) (*ExecutionResult, error)

	// PullImage ensures the Docker image is available locally
	PullImage(ctx context.Context, image string) error

	// Cleanup removes stopped containers and unused images
	Cleanup(ctx context.Context) error

	// Close releases resources
	Close() error
}

Runner executes protoc compilation in Docker containers

Jump to

Keyboard shortcuts

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