joincontext

package module
v0.0.0-...-1724345 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2017 License: MIT Imports: 3 Imported by: 34 modules, 91 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

joincontext

Build Status GoDoc

Package joincontext provides a way to combine two contexts. For example it might be useful for grpc server to cancel all handlers in addition to provided handler context.

For additional info see godoc page

Example

	ctx1, cancel1 := context.WithCancel(context.Background())
	defer cancel1()
	ctx2 := context.Background()

	ctx, cancel := joincontext.Join(ctx1, ctx2)
	defer cancel()
	select {
	case <-ctx.Done():
	default:
		fmt.Println("context alive")
	}

	cancel1()

	// give some time to propagate
	time.Sleep(100 * time.Millisecond)

	select {
	case <-ctx.Done():
		fmt.Println(ctx.Err())
	default:
	}

	// Output: context alive
	// context canceled

Documentation

Overview

Package joincontext provides a way to combine two contexts. For example it might be useful for grpc server to cancel all handlers in addition to provided handler context.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Join

func Join(ctx1, ctx2 context.Context) (context.Context, context.CancelFunc)

Join returns new context which is child for two passed contexts. It starts new goroutine which tracks both contexts.

Done() channel is closed when one of parents contexts is done.

Deadline() returns earliest deadline between parent contexts.

Err() returns error from first done parent context.

Value(key) looks for key in parent contexts. First found is returned.

Example
package main

import (
	"fmt"
	"time"

	"github.com/LK4D4/joincontext"
	"golang.org/x/net/context"
)

func main() {
	ctx1, cancel1 := context.WithCancel(context.Background())
	defer cancel1()
	ctx2 := context.Background()

	ctx, cancel := joincontext.Join(ctx1, ctx2)
	defer cancel()
	select {
	case <-ctx.Done():
	default:
		fmt.Println("context alive")
	}

	cancel1()

	// give some time to propagate
	time.Sleep(100 * time.Millisecond)

	select {
	case <-ctx.Done():
		fmt.Println(ctx.Err())
	default:
	}

}
Output:
context alive
context canceled

Types

This section is empty.

Jump to

Keyboard shortcuts

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