runner

package
v0.0.0-...-73d934c Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2020 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"

	"eagain.net/go/securityblanket/internal/runner"
	"golang.org/x/sync/errgroup"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// this is just here to make the example reproducible
	ch := make(chan struct{})
	fn := func() error {
		fmt.Println("run")
		ch <- struct{}{}
		return nil
	}
	r := runner.New(ctx, fn, nil)
	var g errgroup.Group
	fmt.Println("before")
	g.Go(r.Loop)
	<-ch
	r.Wakeup()
	<-ch
	cancel()
	if err := g.Wait(); err != nil && err != context.Canceled {
		panic(err)
	}
	fmt.Println("after")

}
Output:

before
run
run
after

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner is a helper that makes it easy to write a loop that only wakes up when it's signaled, and terminates on context cancellation.

func New

func New(ctx context.Context, fn func() error, log *zap.Logger) *Runner

func (*Runner) Loop

func (r *Runner) Loop() error

func (*Runner) Wakeup

func (r *Runner) Wakeup()

Jump to

Keyboard shortcuts

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