Categorygithub.com/lab259/go-rscsrv
modulepackage
0.3.2
Repository: https://github.com/lab259/go-rscsrv.git
Documentation: pkg.go.dev

# README

go-rscsrv

CircleCI codecov GoDoc Go Report Card Release

Resource/Service pattern for Go applications.

serviceStarter := rscsrv.NewServiceStarter(
	&rscsrv.ColorServiceReporter{},  // First, the reporter
	&Service1, &Service2, &Service3, // Here all services that should be started.
)

err := serviceStarter.Start()
if err != nil {
	serviceStarter.Stop(true)
}

// ... Service1, Service2 and Service3 were started

See /examples for more usage examples.

Service basics

The Service is a interface that only provides:

  • Name(): string;

But, it is the base of this library. Along side Service you must implement Startable, StartableWithContext and/or Stoppable.

Startable

Startable represent services that its start process cannot be cancelled.

  • Start(): error

StartableWithContext

StartableWithContext provides the functionality for services that can have its start process aborted.

  • StartWithContext(context.Context) error;

Retrier

The StartRetrier is a mechanism that retries starting a Service when it fails. No special Service implementation is required. The StartRetrier is a proxy that wraps the real Service.

The NewStartRetrier wraps the Service providing the desired repeatability.

Example:

serviceStarter := rscsrv.NewServiceStarter(
	&rscsrv.ColorServiceReporter{},  // First, the reporter
	rscsrv.NewStartRetrier(&Service1, rscsrv.StartRetrierOptions{
		MaxTries:          5,
		DelayBetweenTries: time.Second * 5,
		Timeout:           time.Second * 60,
	}), &Service2, &Service3, // Here all services that should be started.
)

err := serviceStarter.Start()
if err != nil {
	serviceStarter.Stop(true)
}

In this example, the Service1 is wrapped by a StartRetrier. The retrier will keep trying to start Service1 until it reaches 5 failures. Between each try, the retrier will wait 5 seconds before try again.

Retrier helpers

The way StartRetrier was designed is for opt-in, so when the library gets updated, the behaviour do not change. So a helper was designed to

Retriers will apply the StartRetrier to many services at once:

retriers := rscsrv.Retriers(rscsrv.StartRetrierOptions{
	MaxTries:          5,
	DelayBetweenTries: time.Second,
	Timeout:           time.Second * 60,
})

serviceStarter := rscsrv.NewServiceStarter(
	&rscsrv.ColorServiceReporter{},  // First, the reporter
	retriers(&Service1, &Service2, &Service3)...,
)

err := serviceStarter.Start()
if err != nil {
	serviceStarter.Stop(true)
}

# Packages

No description provided by the author

# Functions

DefaultServiceStarter returns a default ServiceStarter integrated with the ColorStarterReporter.
No description provided by the author
NewServiceStarter returns a new instace of a `ServiceStarter`.
NewStartRetrier configures.
QuietServiceStarter returns a default ServiceStarter integrated with the NopStarterReporter.
Retrier creates a 2nd order function to conveniently wrap `Service`s.
Retriers creates a 2nd order function to conveniently wrap `Service`s.
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
ErrMaxTriesExceeded is the error returned when the maximum number of failed tries is reached.
ErrServiceNotRunning is the error returned when a non started server is stopped or restarted.
ErrStartCancelled is the error returned the start process is cancelled by a `Stop` call before it gets finished.
ErrStartTimeout is the error returned when the `StartRetrier` reaches its time limit (defined by `options.FailAfter`).
ErrUnknownPanic is the error returned the original service panics and the error recovered is not an actual `error`.
ErrWrongConfigurationInformed is the error returned when a configuration object is loaded but it is not valid.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NopStarRetrierReporter is a reporter with an empty implementation.
No description provided by the author
StartRetrier is a helper that implements retrying start the service.
StartRetrierOptions defines the options for the `StartRetrier`.

# Interfaces

Configurable is an abstraction for implement loading and applying configuration.
ConfigurationLoader defines the contract to load a configuration from a repository.
ConfigurationUnmarshaler describes the unmarshaling contract of a configuration.
Service abstracts the precense of a the name in a possible `Startable` or `Configurable`.
ServiceStarter is an abtraction for service starter which is responsible for starting and stopping services.
No description provided by the author
Startable is an abstraction for implementing parts that can be started, restarted and stopped.
StartableWithContext is an abstraction for implementing services that can have its Start process cancelled.
No description provided by the author
Stoppable is an abstraction with the Stop behavior.

# Type aliases

FuncStartRetrierReporter is a wrapper for retrier reporters.