run

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 24 Imported by: 17

README

Run

Package run provides tools for running solvers.

See godocs for package docs.

Documentation

Overview

Package run provides tools for running solvers.

Index

Constants

View Source
const Data data = "data"

Data is the key for additional data of the run.

View Source
const Start start = "start"

Start is the key for the start time of the run.

Variables

This section is empty.

Functions

func Encode added in v0.20.4

func Encode[
	RunnerConfig, Input, Option, Solution any,
](e Encoder[Solution, Option]) func(
	Runner[RunnerConfig, Input, Option, Solution],
)

Encode sets the encoder of a runner.

func FlagParser added in v0.20.10

func FlagParser[Option, RunnerCfg any]() (
	runnerConfig RunnerCfg, option Option, err error,
)

FlagParser parses flags and env vars and returns a runner config and options.

func IOProduce added in v0.20.10

func IOProduce[
	RunnerConfig, Input, Option, Solution any,
](i IOProducer[RunnerConfig]) func(
	Runner[RunnerConfig, Input, Option, Solution],
)

IOProduce sets the IOProducer of a runner.

func InputDecode added in v0.20.10

func InputDecode[
	RunnerConfig, Input, Option, Solution any,
](i Decoder[Input]) func(
	Runner[RunnerConfig, Input, Option, Solution],
)

InputDecode sets the input decoder of a runner.

func InputValidate added in v0.26.0

func InputValidate[
	RunnerConfig, Input, Option, Solution any,
](v Validator[Input]) func(
	Runner[RunnerConfig, Input, Option, Solution],
)

InputValidate sets the input validator of a runner.

func NoopOptionsDecoder added in v0.20.10

func NoopOptionsDecoder[Option any](
	_ context.Context, _ any,
) (option Option, err error)

NoopOptionsDecoder is a Decoder that returns the option as is.

func OptionDecode added in v0.20.10

func OptionDecode[
	RunnerConfig, Input, Option, Solution any,
](o Decoder[Option]) func(
	Runner[RunnerConfig, Input, Option, Solution],
)

OptionDecode sets the options decoder of a runner.

func QueryParamDecoder added in v0.20.10

func QueryParamDecoder[Option any](
	_ context.Context, reader any,
) (option Option, err error)

QueryParamDecoder is a Decoder that returns option from query params.

func SetAddr added in v0.20.10

func SetAddr[Input, Option, Solution any](addr string) func(
	*httpRunner[Input, Option, Solution],
)

SetAddr sets the address the http server listens on.

func SetHTTPRequestHandler added in v0.20.10

func SetHTTPRequestHandler[Input, Option, Solution any](
	f HTTPRequestHandler) func(*httpRunner[Input, Option, Solution],
)

SetHTTPRequestHandler sets the function that handles the http request.

func SetHTTPServer added in v0.20.10

func SetHTTPServer[Input, Option, Solution any](
	s *http.Server) func(*httpRunner[Input, Option, Solution],
)

SetHTTPServer sets the http server. Note that if you want to set the address or the logger of the http server you are setting through this option and you want to make use of SetAddr and SetLogger, you should pass them after passing this option. Alternatively you can fully configure the http server and just pass it to SetHTTPServer.

func SetLogger added in v0.20.10

func SetLogger[Input, Option, Solution any](l *log.Logger) func(
	*httpRunner[Input, Option, Solution],
)

SetLogger sets the logger of the http server.

func SetMaxParallel added in v0.20.10

func SetMaxParallel[Input, Option, Solution any](maxParallel int) func(
	*httpRunner[Input, Option, Solution],
)

SetMaxParallel sets the maximum number of parallel requests.

func SetRunnerOption added in v0.20.10

func SetRunnerOption[Input, Option, Solution any](
	option RunnerOption[HTTPRunnerConfig, Input, Option, Solution],
) func(
	*httpRunner[Input, Option, Solution],
)

SetRunnerOption sets a runner option on the underlying runner.

func SyncHTTPRequestHandler added in v0.20.10

func SyncHTTPRequestHandler(
	w http.ResponseWriter, req *http.Request,
) (Callback, IOProducer[HTTPRunnerConfig], error)

SyncHTTPRequestHandler allows the input and option to be sent as body and query parameters. The output is written synchronously to the response writer.

Types

type Algorithm added in v0.20.10

type Algorithm[Input, Option, Solution any] func(
	context.Context, Input, Option, chan<- Solution,
) error

Algorithm is a function that runs an algorithm.

type AsyncHTTPRequestHandlerOption added in v0.20.10

type AsyncHTTPRequestHandlerOption func(*asyncHTTPHandler)

AsyncHTTPRequestHandlerOption configures an AsyncHTTPRequestHandler.

func CallbackURL added in v0.20.10

func CallbackURL(url string) AsyncHTTPRequestHandlerOption

CallbackURL sets a default callback url. This is used to send the result of the algorithm to another service.

func RequestOverride added in v0.20.10

func RequestOverride(allow bool) AsyncHTTPRequestHandlerOption

RequestOverride sets whether to allow the callback url to be overridden by the request header (callback_url).

type CLIRunnerConfig added in v0.20.10

type CLIRunnerConfig struct {
	Runner struct {
		Input struct {
			Path string `usage:"The input file path"`
		}
		Profile struct {
			CPU    string `usage:"The CPU profile file path"`
			Memory string `usage:"The memory profile file path"`
		}
		Output struct {
			Path      string `usage:"The output file path"`
			Solutions string `default:"last" usage:"{all, last}"`
		}
	}
}

CLIRunnerConfig is the configuration of the CliRunner.

func (CLIRunnerConfig) CPUProfilePath added in v0.20.10

func (c CLIRunnerConfig) CPUProfilePath() string

CPUProfilePath returns the CPU profile path.

func (CLIRunnerConfig) MemoryProfilePath added in v0.20.10

func (c CLIRunnerConfig) MemoryProfilePath() string

MemoryProfilePath returns the memory profile path.

func (CLIRunnerConfig) OutputPath added in v0.20.10

func (c CLIRunnerConfig) OutputPath() string

OutputPath returns the output path.

func (CLIRunnerConfig) Solutions added in v0.20.10

func (c CLIRunnerConfig) Solutions() (Solutions, error)

Solutions returns the configured solutions.

type CPUProfiler added in v0.20.10

type CPUProfiler interface {
	CPUProfilePath() string
}

CPUProfiler is the interface a runner configuration can implement to return the CPU profile path.

type Callback added in v0.20.10

type Callback func(requestID string, contentType string) error

Callback is a function that is called after the request is processed. It is used to send the result asynchronously to some other service. The first argument is the request id. The second argument is the contentType, e.g. application/json.

type ContentTyper added in v0.20.10

type ContentTyper interface {
	ContentType() string
}

ContentTyper is an interface which defines the ContentType function that describes the content type of the encoder.

type Decoder added in v0.20.10

type Decoder[Input any] func(context.Context, any) (Input, error)

Decoder is a function that decodes a reader into a struct.

func GenericDecoder added in v0.20.10

func GenericDecoder[Input any](
	decoder decode.Decoder,
) Decoder[Input]

GenericDecoder returns a new generic decoder.

type Encoder added in v0.20.10

type Encoder[Solution, Option any] interface {
	Encode(context.Context, <-chan Solution, any, any, Option) error
}

Encoder is an interface which defines the Encode function that encodes a struct into a writer.

func GenericEncoder added in v0.20.10

func GenericEncoder[Solution, Options any](
	encoder encode.Encoder,
) Encoder[Solution, Options]

GenericEncoder returns a new Encoder that encodes the solution using the given encoder.

type HTTPRequestHandler added in v0.20.10

type HTTPRequestHandler func(
	w http.ResponseWriter, req *http.Request,
) (Callback, IOProducer[HTTPRunnerConfig], error)

HTTPRequestHandler is a function that handles an http request.

func AsyncHTTPRequestHandler added in v0.20.10

func AsyncHTTPRequestHandler(
	options ...AsyncHTTPRequestHandlerOption,
) HTTPRequestHandler

AsyncHTTPRequestHandler creates a new asynchronous HTTPRequestHandler. The given options are used to configure the handler.

type HTTPRunner added in v0.20.10

type HTTPRunner[RunnerConfig, Input, Option, Solution any] interface {
	Runner[RunnerConfig, Input, Option, Solution]
	// ActiveRuns returns the number of currently active runs.
	ActiveRuns() int
}

HTTPRunner is a runner that runs an algorithm as an http server.

func HTTP added in v0.20.10

func HTTP[Input, Option, Output any](solver func(
	ctx context.Context, input Input, option Option) (solutions Output, err error),
	options ...HTTPRunnerOption[Input, Option, Output],
) HTTPRunner[HTTPRunnerConfig, Input, Option, Output]

HTTP instantiates an HTTPRunner and runs it. The default port is 9000 and protocol is HTTP. Pass HTTPRunnerOptions to change these settings.

func NewHTTPRunner added in v0.20.10

func NewHTTPRunner[Input, Option, Solution any](
	algorithm Algorithm[Input, Option, Solution],
	options ...HTTPRunnerOption[Input, Option, Solution],
) HTTPRunner[HTTPRunnerConfig, Input, Option, Solution]

NewHTTPRunner creates a new NewHTTPRunner.

type HTTPRunnerConfig added in v0.20.10

type HTTPRunnerConfig struct {
	Runner struct {
		Log    *log.Logger
		Output struct {
			Solutions string `default:"last" usage:"Return all or last solution"`
		}
		HTTP struct {
			Address           string        `default:":9000" usage:"The host address"`
			Certificate       string        `usage:"The certificate file path"`
			Key               string        `usage:"The key file path"`
			ReadHeaderTimeout time.Duration `default:"60s" usage:"The maximum duration for reading the request headers"`
			MaxParallel       int           `default:"1" usage:"The max number of requests"`
		}
	}
}

HTTPRunnerConfig defines the configuration of the HTTPRunner.

func (HTTPRunnerConfig) Solutions added in v0.20.10

func (c HTTPRunnerConfig) Solutions() (Solutions, error)

Solutions returns the configured solutions.

type HTTPRunnerOption added in v0.20.10

type HTTPRunnerOption[Input, Option, Solution any] func(
	*httpRunner[Input, Option, Solution],
)

HTTPRunnerOption configures a HTTPRunner.

type IOData added in v0.20.10

type IOData interface {
	Input() any
	Option() any
	Writer() any
}

IOData describes the data that is used in the IOProducer. The input is the source of the input data. The option is the source of the option data. The writer is the destination of the output data.

func CliIOProducer added in v0.20.10

func CliIOProducer(_ context.Context, cfg CLIRunnerConfig) (IOData, error)

CliIOProducer is the IOProducer for the CliRunner. The input and output paths are used to configure the input and output readers and writers. If the paths are empty, os.Stdin and os.Stdout are used.

func NewIOData added in v0.20.10

func NewIOData(input any, option any, writer any) (data IOData, err error)

NewIOData creates a new IOData.

type IOProducer added in v0.20.10

type IOProducer[RunnerConfig any] func(
	context.Context, RunnerConfig,
) (IOData, error)

IOProducer is a function that produces the input, option and writer.

type MemoryProfiler added in v0.20.10

type MemoryProfiler interface {
	MemoryProfilePath() string
}

MemoryProfiler is the interface a runner configuration can implement to return the memory profile path.

type OutputPather added in v0.20.10

type OutputPather interface {
	OutputPath() string
}

OutputPather is the interface a runner configuration can implement to return the output path.

type Runner added in v0.20.4

type Runner[RunnerConfig, Input, Option, Solution any] interface {
	// Run runs the runner.
	Run(context.Context) error
	// SetIOProducer sets the ioProducer of a runner.
	SetIOProducer(IOProducer[RunnerConfig])
	// SetInputDecoder sets the inputDecoder of a runner.
	SetInputDecoder(Decoder[Input])
	// SetInputValidator sets the inputValidator of a runner.
	SetInputValidator(Validator[Input])
	// SetOptionDecoder sets the optionDecoder of a runner.
	SetOptionDecoder(Decoder[Option])
	// SetAlgorithm sets the algorithm of a runner.
	SetAlgorithm(Algorithm[Input, Option, Solution])
	// SetEncoder sets the encoder of a runner.
	SetEncoder(Encoder[Solution, Option])
	// GetEncoder returns the encoder of a runner.
	GetEncoder() Encoder[Solution, Option]
	// RunnerConfig returns the runnerConfig of a runner.
	RunnerConfig() RunnerConfig
}

Runner defines the interface of the runner.

func CLI added in v0.20.10

func CLI[Input, Option, Output any](solver func(
	ctx context.Context, input Input, option Option) (solutions Output, err error),
	options ...RunnerOption[CLIRunnerConfig, Input, Option, Output],
) Runner[CLIRunnerConfig, Input, Option, Output]

CLI instantiates a CLIRunner and runs it. This is a wrapper function that allows for simple usage of the CLIRunner.

func GenericRunner added in v0.20.10

func GenericRunner[RunnerConfig, Input, Option, Solution any](
	ioHandler IOProducer[RunnerConfig],
	inputDecoder Decoder[Input],
	inputValidator Validator[Input],
	optionDecoder Decoder[Option],
	handler Algorithm[Input, Option, Solution],
	encoder Encoder[Solution, Option],
) Runner[RunnerConfig, Input, Option, Solution]

GenericRunner creates a new runner from the given components.

func NewCLIRunner added in v0.20.10

func NewCLIRunner[Input, Option, Solution any](
	algorithm Algorithm[Input, Option, Solution],
	options ...RunnerOption[CLIRunnerConfig, Input, Option, Solution],
) Runner[CLIRunnerConfig, Input, Option, Solution]

NewCLIRunner is the default CLI runner. It reads the input from stdin or a file, writes output to stdout or a file, decodes the input using the JSON decoder, accepts options from the command line, and encodes the solution using the JSON encoder.

type RunnerOption added in v0.20.10

type RunnerOption[RunnerConfig, Input, Option, Solution any] func(
	Runner[RunnerConfig, Input, Option, Solution],
)

RunnerOption configures a Runner.

type SolutionLimiter added in v0.20.10

type SolutionLimiter interface {
	Solutions() (Solutions, error)
}

SolutionLimiter is the interface a runner configuration can implement to control whether all or only the last solution is returned.

type Solutions added in v0.20.10

type Solutions int

Solutions can be all or last.

const (
	All Solutions = iota
	Last
)

Constants for setting all or last solution output.

func ParseSolutions added in v0.20.10

func ParseSolutions(s string) (Solutions, error)

ParseSolutions converts "all" to All and "last" to Last.

func (Solutions) String added in v0.20.10

func (s Solutions) String() string

type Validator added in v0.26.0

type Validator[Input any] func(context.Context, any) error

Validator is a function that validates the input.

Directories

Path Synopsis
Package cli provides additional features for CLIRunner.
Package cli provides additional features for CLIRunner.
Package decode provides decoders for output of a runner.
Package decode provides decoders for output of a runner.
Package encode provides encoders for input of a runner.
Package encode provides encoders for input of a runner.
Package http provides additional features for HTTPRunner.
Package http provides additional features for HTTPRunner.
Package schema provides the schema for the output.
Package schema provides the schema for the output.
Package statistics provides the schema for the statistics section of the output.
Package statistics provides the schema for the statistics section of the output.
tests
http/async
package main holds the implementation of a simple runner example.
package main holds the implementation of a simple runner example.
http/error_log
package main holds the implementation of a simple runner example.
package main holds the implementation of a simple runner example.
http/sync
package main holds the implementation of a simple runner example.
package main holds the implementation of a simple runner example.
http/too_many_requests
package main holds the implementation of a simple runner example.
package main holds the implementation of a simple runner example.
simple
package main holds the implementation of a simple runner example.
package main holds the implementation of a simple runner example.
Package validate contains Validator implementations.
Package validate contains Validator implementations.

Jump to

Keyboard shortcuts

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