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: 86

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