Skip to content

A Golang context with support for wait.

License

Notifications You must be signed in to change notification settings

zaininfo/context-wait

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Waitable context

GitHub Workflow Status

The wait module introduces two semantics for contexts to support waiting for a goroutine, which complements the existing functionality to cancel a goroutine.

Due to the parent-child hierarchy of contexts, a waitable context will be unblocked if any one of the child contexts marks it as completed.

Therefore, the best use-case for a waitable context is when there is one essential goroutine and one or more non-essential goroutines.

Functions

  • func WithWait(parent context.Context) (ctx context.Context, waitFunc func())

This function returns a new context and a wait function that can be used to wait for completion of the new context. In case, the parent context is already a waitable context, this function returns it as it is.

  • func Done(ctx context.Context)

Done function marks the passed context as completed. This function is idempotent for the same context.

Usage

package main

import (
	"context"
	"time"

	wContext "github.com/zaininfo/context-wait/wait"
)

func main() {
	ctx, wait := wContext.WithWait(context.Background())

	go someFunc(ctx)

	// wait for context completion
	wait()
}

func someFunc(ctx context.Context) {
	time.Sleep(time.Minute)

	// mark context as completed
	wContext.Done(ctx)
}

The test file contains a more realistic use-case i.e. waiting for an important goroutine while cancelling unimportant ones.

About

A Golang context with support for wait.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages