go-floc.v2: gopkg.in/workanator/go-floc.v2 Index | Files | Directories

package floc

import "gopkg.in/workanator/go-floc.v2"

Package floc allows to orchestrate goroutines with ease. The goal of the project is to make the process of running goroutines in parallel and synchronizing them easy.

Floc follows for objectives:

-- Easy to use functional interface.

-- Better control over execution with one entry point and one exit point.

-- Simple parallelism and synchronization of jobs.

-- As little overhead, in comparison to direct use of goroutines and sync primitives, as possible.

The package categorizes middleware used for architect flows in subpackages.

-- `guard` contains middleware which help protect flow from falling into panic or unpredicted behavior.

-- `pred` contains some basic predicates for AND, OR, NOT logic.

-- `run` provides middleware for designing flow, i.e. for running job sequentially, in parallel, in background and so on.

-- `errors` all error types used in the package.

Here is a quick example of what the package capable of.

// The flow computes something complex and does writing results in
// background.
flow := run.Sequence(
  run.Background(WriteToDisk),
  run.While(pred.Not(TestComputed), run.Sequence(
    run.Parallel(
      ComputeSomething,
      ComputeSomethingElse,
      guard.Panic(ComputeDangerousThing),
    ),
    run.Parallel(
      PrepareForWrite,
      UpdateComputedFlag,
    ),
  )),
  CompleteWithSuccess,
)

// The entry point: produce the result.
result, data, err := floc.Run(flow)

// The exit point: consume the result.
if err != nil {
  fmt.Println(err)
} else if result.IsCompleted() {
  fmt.Println(data)
} else {
  fmt.Printf("Finished with result %s and data %v", result.String(), data)
}

Index

Package Files

context.go context_impl.go control.go control_impl.go doc.go job.go predicate.go releaser.go result.go result_mask.go run.go

type Context Uses

type Context interface {
    Releaser

    // Ctx returns the underlying context.
    Ctx() context.Context

    // UpdateCtx sets the new underlying context.
    UpdateCtx(ctx context.Context)

    // Done returns a channel that's closed when the flow done.
    // Successive calls to Done return the same value.
    Done() <-chan struct{}

    // Value returns the value associated with this context for key,
    // or nil if no value is associated with key.
    Value(key interface{}) (value interface{})

    // Create a new context with value and make it the current.
    // This is an equivalent to.
    //
    //    oldCtx := ctx.Ctx()
    //    newCtx := context.WithValue(oldCtx, key, value)
    //    ctx.UpdateCtx(newCtx)
    //
    AddValue(key, value interface{})
}

Context provides safe access to the underlying context. For more details on the underlying context see https://golang.org/pkg/context/#Context.

func BorrowContext Uses

func BorrowContext(ctx context.Context) Context

BorrowContext constructs new instance from the context given. The function panics if the context given is nil.

func NewContext Uses

func NewContext() Context

NewContext constructs new instance of TODO context.

type Control Uses

type Control interface {
    Releaser

    // Complete finishes the flow with success status and stops
    // execution of further jobs if any.
    Complete(data interface{})

    // Cancel cancels the execution of the flow.
    Cancel(data interface{})

    // Fail cancels the execution of the flow with error.
    Fail(data interface{}, err error)

    // IsFinished tests if execution of the flow is either completed or canceled.
    IsFinished() bool

    // Result returns the result code and the result data of the flow. The call
    // to the function is effective only if the flow is finished.
    Result() (result Result, data interface{}, err error)
}

Control allows to control execution of the flow.

func NewControl Uses

func NewControl(ctx Context) Control

NewControl constructs Control instance from context given. The function panics if the context given is nil.

type Job Uses

type Job func(ctx Context, ctrl Control) error

Job is the small piece of flow.

type Predicate Uses

type Predicate func(ctx Context) bool

Predicate is the function which calculates the result of some condition.

type Releaser Uses

type Releaser interface {
    // Release should be called once when the object is not needed anymore.
    Release()
}

Releaser is responsible for releasing underlying resources.

type Result Uses

type Result int32

Result identifies the result of execution.

const (
    None      Result = 1
    Completed Result = 2
    Canceled  Result = 4
    Failed    Result = 8
)

Possible results.

func Run Uses

func Run(job Job) (result Result, data interface{}, err error)

Run creates a new Context and Control and runs the flow.

func RunWith Uses

func RunWith(ctx Context, ctrl Control, job Job) (result Result, data interface{}, err error)

RunWith runs the flow with the Context and Control given.

func (Result) IsCanceled Uses

func (result Result) IsCanceled() bool

IsCanceled tests if the result is Canceled.

func (Result) IsCompleted Uses

func (result Result) IsCompleted() bool

IsCompleted tests if the result is Completed.

func (Result) IsFailed Uses

func (result Result) IsFailed() bool

IsFailed tests if the result is Failed.

func (Result) IsFinished Uses

func (result Result) IsFinished() bool

IsFinished tests if the result is either Completed or Canceled or Failed.

func (Result) IsNone Uses

func (result Result) IsNone() bool

IsNone tests if the result is None.

func (Result) IsValid Uses

func (result Result) IsValid() bool

IsValid tests if the result is a valid value.

func (Result) Mask Uses

func (result Result) Mask() ResultMask

Mask constructs ResultMask with only one result masked.

func (Result) String Uses

func (result Result) String() string

type ResultMask Uses

type ResultMask Result

ResultMask is the mask of possible results.

func EmptyResultMask Uses

func EmptyResultMask() ResultMask

EmptyResultMask returns empty result set.

func NewResultMask Uses

func NewResultMask(mask Result) ResultMask

NewResultMask constructs new instance from the mask given.

func (ResultMask) IsEmpty Uses

func (mask ResultMask) IsEmpty() bool

IsEmpty returns true if no result is masked.

func (ResultMask) IsMasked Uses

func (mask ResultMask) IsMasked(result Result) bool

IsMasked tests if the result is masked.

func (ResultMask) String Uses

func (mask ResultMask) String() string

Directories

PathSynopsis
errorsPackage errors defines error types used in the package.
guardPackage guard contains middleware capable to protect execution of flow from crashing or from unpredicted behavior.
predPackage pred provides predicates for basic logic operations.
runPackage run is the collection of flow architectural blocks.

Package floc imports 6 packages (graph) and is imported by 6 packages. Updated 2019-01-04. Refresh now. Tools for package owners.