context

package module
v0.0.0-...-7b52f91 Latest Latest
Warning

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

Go to latest
Published: May 22, 2019 License: MIT Imports: 6 Imported by: 0

README

Set of contexts for all occasions

Build Status GoDoc Go Report Card Software License

Installation

$ go get github.com/mantyr/context
WaitGroupContext returns a context with WaitGroup functions
  1. You can add an element only if the context is not closed
  2. You can delete the element at any time
  3. You can close the context at any time
  4. You can wait for the context to close
  5. You can wait for the completion of operations related to the context

Example

package main

import (
    "context"
    ct "github.com/mantyr/context"
}

func main() {
    ctx, cancel := ct.WaitGroupContext(context.Background())
    err := ctx.Add(1)
    if err != nil {
        // context closed
    }
    ctx.Add(1)
    ctx.Delete()
    ctx.Delete()
    cancel()

    select {
    case <- ctx.Done():
        // context closed
    case <- ctx.Wait():
        // context closed and all operations completed
    default:
        // context open
    }
}

Author

Oleg Shevelev

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ShouldBeDone

func ShouldBeDone(actual interface{}, expected ...interface{}) string

ShouldBeDone проверяет что контекст закрыт

func ShouldBeWait

func ShouldBeWait(actual interface{}, expected ...interface{}) string

ShouldBeWait проверяет что все задачи в контексте были завершены

func ShouldNotBeDone

func ShouldNotBeDone(actual interface{}, expected ...interface{}) string

ShouldNotBeDone проверяет что контекст не закрыт

func ShouldNotBeWait

func ShouldNotBeWait(actual interface{}, expected ...interface{}) string

ShouldNotBeWait проверяет что в контексте есть не завершённые задачи

func WaitSyscallContext

func WaitSyscallContext(
	ctx context.Context,
	signals ...os.Signal,
) (
	context.Context,
	context.CancelFunc,
)

WaitSyscallContext возвращает контекст который закрывается по сигналу

Внимание, перед использованием изучите работу signal.Notify

Types

type CanceledError

type CanceledError string

CanceledError эта ошибка говорит о том что контекст закрыт

func (CanceledError) Error

func (c CanceledError) Error() string

Error реализует интерфейс error

type WContext

type WContext interface {
	Wait() <-chan struct{}
}

WContext это интерфейс для контекстов с ожиданием

type WGContext

type WGContext interface {
	context.Context
	WContext

	Add(delta int) error
	Delete()
}

WGContext это интерфейс для WaitGroupContext

func WaitGroupContext

func WaitGroupContext(
	parent context.Context,
) (
	WGContext,
	context.CancelFunc,
)

WaitGroupContext возвращает контекст с функциями WaitGroup

  • Можно добавить элемент только если контекст не закрыт
  • Можно убрать элемент в любой момент
  • Можно закрыть контекст в любой момент
  • Можно дождаться закрытия контекста
  • Можно дождаться завершения операций связанных с контекстом

Jump to

Keyboard shortcuts

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