stopinformer

module
v0.0.0-...-fe38db2 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2020 License: MIT

README

Stop Informer

Stop informer provides a simple and concise way to maintain an exit-handle on a running goroutine. It also allows you to wait until the stop is acknowledged by the goroutine using either a blocking stop, or using a notification channel. When paired with stopinformermap, you can maintain named handles on multiple goroutines, stop all of them.

Installation

$ go get github.com/clarkmcc/stopinformer

Example

Single Stop Informer

For managing a single stop informer.

func main() {

    // Create a new informer
    informer := NewGenericStopInformer()

    // Start a goroutine and perform a non blocking watch operation
    // on the informer.Watch() channel
    go func() {
        loop:
        for {
            select {
            case stop := <-informer.Watch():
                defer stop.Acknowledge()
                break loop
            case <-time.After(1 * time.Second):
                continue
            }
        }
    }()
    
    // Perform a blocking stop of the goroutine
    informer.Stop()

    // Or get a notification via a channel when the goroutine has acknowledged the stop
    <- informer.StopAndNotify(0)
}
Multiple Stop Informers

For managing multiple (non-named) stop informers. This is useful for when you want to interact with multipl stop informers as a group.

func main() {
    informers := NewGenericStopInformers()
    
    informers.Create(stopinformer.NewGenericStopInformer())
    informers.Create(stopinformer.NewGenericStopInformer())

    // Pass your informer into goroutines here...

    // Block until all goroutines acknowledge the stop
    informers.StopAll()

    //Or get a notification via a channel when all goroutines acknowledged the stop
    <-informers.StopAllAndNotify(0)
}
Stop Informer Map

For managing multiple named stop informers. This is useful for when you want to interact with multiple stop informers individually.

func main() {
    informers := NewGenericStopInformerMap()
    
    informers.Create("informer1", stopinformer.NewGenericStopInformer())
    informers.Create("informer2", stopinformer.NewGenericStopInformer())

    // Pass your informer into goroutines here...

    // Stop a single goroutine by name
    informers.Get("informer1").Stop()

    // Informers that don't exist resolve immediately to prevent existence checking and error handling
    informers.Get("doesntexist").Stop()

    // Or you can check for existence
    ok := informers.Exists("informer1")

    // Block until all goroutines acknowledge the stop
    informers.StopAll()

    //Or get a notification via a channel when all goroutines acknowledged the stop
    <-informers.StopAllAndNotify(0)
}

Directories

Path Synopsis
Package goroutinemap implements a data structure for managing go routines by name.
Package goroutinemap implements a data structure for managing go routines by name.
exponentialbackoff
Package exponentialbackoff contains logic for implementing exponential backoff for GoRoutineMap and NestedPendingOperations.
Package exponentialbackoff contains logic for implementing exponential backoff for GoRoutineMap and NestedPendingOperations.

Jump to

Keyboard shortcuts

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