Documentation
¶
Overview ¶
Package run provides tools for running solvers.
Index ¶
- Constants
- func Encode[RunnerConfig, Input, Option, Solution any](e Encoder[Solution, Option]) func(Runner[RunnerConfig, Input, Option, Solution])
- func FlagParser[Option, RunnerCfg any]() (runnerConfig RunnerCfg, option Option, err error)
- func IOProduce[RunnerConfig, Input, Option, Solution any](i IOProducer[RunnerConfig]) func(Runner[RunnerConfig, Input, Option, Solution])
- func InputDecode[RunnerConfig, Input, Option, Solution any](i Decoder[Input]) func(Runner[RunnerConfig, Input, Option, Solution])
- func InputValidate[RunnerConfig, Input, Option, Solution any](v Validator[Input]) func(Runner[RunnerConfig, Input, Option, Solution])
- func NoopOptionsDecoder[Option any](_ context.Context, _ any) (option Option, err error)
- func OptionDecode[RunnerConfig, Input, Option, Solution any](o Decoder[Option]) func(Runner[RunnerConfig, Input, Option, Solution])
- func QueryParamDecoder[Option any](_ context.Context, reader any) (option Option, err error)
- func SetAddr[Input, Option, Solution any](addr string) func(*httpRunner[Input, Option, Solution])
- func SetHTTPRequestHandler[Input, Option, Solution any](f HTTPRequestHandler) func(*httpRunner[Input, Option, Solution])
- func SetHTTPServer[Input, Option, Solution any](s *http.Server) func(*httpRunner[Input, Option, Solution])
- func SetLogger[Input, Option, Solution any](l *log.Logger) func(*httpRunner[Input, Option, Solution])
- func SetMaxParallel[Input, Option, Solution any](maxParallel int) func(*httpRunner[Input, Option, Solution])
- func SetRunnerOption[Input, Option, Solution any](option RunnerOption[HTTPRunnerConfig, Input, Option, Solution]) func(*httpRunner[Input, Option, Solution])
- func SyncHTTPRequestHandler(w http.ResponseWriter, req *http.Request) (Callback, IOProducer[HTTPRunnerConfig], error)
- type Algorithm
- type AsyncHTTPRequestHandlerOption
- type CLIRunnerConfig
- type CPUProfiler
- type Callback
- type ContentTyper
- type Decoder
- type Encoder
- type HTTPRequestHandler
- type HTTPRunner
- type HTTPRunnerConfig
- type HTTPRunnerOption
- type IOData
- type IOProducer
- type MemoryProfiler
- type OutputPather
- type Runner
- func CLI[Input, Option, Output any](...) Runner[CLIRunnerConfig, Input, Option, Output]
- func GenericRunner[RunnerConfig, Input, Option, Solution any](ioHandler IOProducer[RunnerConfig], inputDecoder Decoder[Input], ...) Runner[RunnerConfig, Input, Option, Solution]
- func NewCLIRunner[Input, Option, Solution any](algorithm Algorithm[Input, Option, Solution], ...) Runner[CLIRunnerConfig, Input, Option, Solution]
- type RunnerOption
- type SolutionLimiter
- type Solutions
- type Validator
Constants ¶
const Data data = "data"
Data is the key for additional data of the run.
const Start start = "start"
Start is the key for the start time of the run.
Variables ¶
This section is empty.
Functions ¶
func Encode ¶
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 ¶
FlagParser parses flags and env vars and returns a runner config and options.
func IOProduce ¶
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 ¶
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 ¶
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 ¶
NoopOptionsDecoder is a Decoder that returns the option as is.
func OptionDecode ¶
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 ¶
QueryParamDecoder is a Decoder that returns option from query params.
func SetAddr ¶
func SetAddr[Input, Option, Solution any](addr string) func( *httpRunner[Input, Option, Solution], )
SetAddr sets the address the http server listens on.
func SetHTTPRequestHandler ¶
func SetHTTPRequestHandler[Input, Option, Solution any]( f HTTPRequestHandler) func(*httpRunner[Input, Option, Solution], )
SetHTTPRequestHandler sets the function that handles the http request.
func SetHTTPServer ¶
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 ¶
func SetLogger[Input, Option, Solution any](l *log.Logger) func( *httpRunner[Input, Option, Solution], )
SetLogger sets the logger of the http server.
func SetMaxParallel ¶
func SetMaxParallel[Input, Option, Solution any](maxParallel int) func( *httpRunner[Input, Option, Solution], )
SetMaxParallel sets the maximum number of parallel requests.
func SetRunnerOption ¶
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 ¶
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 ¶
type Algorithm[Input, Option, Solution any] func( context.Context, Input, Option, chan<- Solution, ) error
Algorithm is a function that runs an algorithm.
type AsyncHTTPRequestHandlerOption ¶
type AsyncHTTPRequestHandlerOption func(*asyncHTTPHandler)
AsyncHTTPRequestHandlerOption configures an AsyncHTTPRequestHandler.
func CallbackURL ¶
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 ¶
func RequestOverride(allow bool) AsyncHTTPRequestHandlerOption
RequestOverride sets whether to allow the callback url to be overridden by the request header (callback_url).
type CLIRunnerConfig ¶
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 ¶
func (c CLIRunnerConfig) CPUProfilePath() string
CPUProfilePath returns the CPU profile path.
func (CLIRunnerConfig) MemoryProfilePath ¶
func (c CLIRunnerConfig) MemoryProfilePath() string
MemoryProfilePath returns the memory profile path.
func (CLIRunnerConfig) OutputPath ¶
func (c CLIRunnerConfig) OutputPath() string
OutputPath returns the output path.
func (CLIRunnerConfig) Solutions ¶
func (c CLIRunnerConfig) Solutions() (Solutions, error)
Solutions returns the configured solutions.
type CPUProfiler ¶
type CPUProfiler interface {
CPUProfilePath() string
}
CPUProfiler is the interface a runner configuration can implement to return the CPU profile path.
type Callback ¶
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 ¶
type ContentTyper interface {
ContentType() string
}
ContentTyper is an interface which defines the ContentType function that describes the content type of the encoder.
type Encoder ¶
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.
type HTTPRequestHandler ¶
type HTTPRequestHandler func( w http.ResponseWriter, req *http.Request, ) (Callback, IOProducer[HTTPRunnerConfig], error)
HTTPRequestHandler is a function that handles an http request.
func AsyncHTTPRequestHandler ¶
func AsyncHTTPRequestHandler( options ...AsyncHTTPRequestHandlerOption, ) HTTPRequestHandler
AsyncHTTPRequestHandler creates a new asynchronous HTTPRequestHandler. The given options are used to configure the handler.
type HTTPRunner ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (c HTTPRunnerConfig) Solutions() (Solutions, error)
Solutions returns the configured solutions.
type HTTPRunnerOption ¶
type HTTPRunnerOption[Input, Option, Solution any] func( *httpRunner[Input, Option, Solution], )
HTTPRunnerOption configures a HTTPRunner.
type IOData ¶
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 ¶
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.
type IOProducer ¶
IOProducer is a function that produces the input, option and writer.
type MemoryProfiler ¶
type MemoryProfiler interface {
MemoryProfilePath() string
}
MemoryProfiler is the interface a runner configuration can implement to return the memory profile path.
type OutputPather ¶
type OutputPather interface {
OutputPath() string
}
OutputPather is the interface a runner configuration can implement to return the output path.
type Runner ¶
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 ¶
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 ¶
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 ¶
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 ¶
type RunnerOption[RunnerConfig, Input, Option, Solution any] func( Runner[RunnerConfig, Input, Option, Solution], )
RunnerOption configures a Runner.
type SolutionLimiter ¶
SolutionLimiter is the interface a runner configuration can implement to control whether all or only the last solution is returned.
type Solutions ¶
type Solutions int
Solutions can be all or last.
func ParseSolutions ¶
ParseSolutions converts "all" to All and "last" to Last.
Source Files
¶
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. |