Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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") )
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) Execute ¶
func (r *DockerRunner) Execute(ctx context.Context, req *ExecutionRequest) (*ExecutionResult, error)
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:
- defer result.Duration = ... (line 57) - DECLARED FIRST, RUNS LAST
- defer os.RemoveAll(inputDir) (line 89) - runs second-to-last
- defer os.RemoveAll(outputDir) (line 96) - runs third-to-last
- defer container cleanup (if container created) - RUNS FIRST
Cleanup order (earliest to latest):
- Container cleanup (if exists) - Release Docker resources
- outputDir cleanup - Remove temporary output directory
- inputDir cleanup - Remove temporary input directory
- 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
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