refctx

package
v0.0.0-...-0bfd988 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2018 License: MIT Imports: 3 Imported by: ? modules, 0 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

refctx

Package refctx provides contextualized reference counting. It exports a context whose lifetime is bound to a RefCtr instance. The RefCtr keeps track of how many references to a context are held and cancels this context when the refcount reaches zero.

refctx works similarly to sync.WaitGroup.

Examples

package main

import "github.com/SentimensRG/ctx/refctx"

func main() {
    ctx, ctr := refctx.WithRefCount(context.Background())

    for i := 0; i < 5; i++ {
        ctr.Incr()
        go func() {
            defer ctr.Decr()

            time.Sleep(time.Second * i)
        }()
    }

    <-ctx.Done()  // fires when refcount falls back to zero
}

A common use-case for refctx is to manage timeouts. Consider the following example using github.com/gorilla/websocket.

import (
    "time"

    "github.com/SentimensRG/ctx/refctx"
    "github.com/SentimensRG/ctx/sigctx"

    "github.com/gorilla/websocket"
)

const (
    pingDeadline = time.Second * 1
    pongDeadline = pingDeadline * 2
)

func main() {

    conn := openWebsocketConnection()

    ctx, ctr := refctx.WithRefCount(sigctx.New())  // good place for sigctx
    rc.Incr()  // start with one refcount

    go func() {
        for range time.NewTicker(heartbeatInterval).C {
            select {
            case <-c.Done():
                // c.Done fires either when the process receives an OS signal, or
                // when the peer took too long to respond to a ping.
                return
            default:
                deadline := time.Now().Add(pingDeadline)
                _ = conn.WriteControl(websocket.PingMessage, nil, deadline)
                go func() {
                    <-time.After(pongDeadline)
                    rc.Decr()
                }()
            }
        }
    }()

    conn.SetPongHandler(func(_ string) (_ error) {
        rc.Incr()
        return
    })

    businessLogic(c, conn)
    <-c.Done()
}

Documentation

Overview

Package refctx provides a context tied to a reference-counter.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RefCtr

type RefCtr struct {
	// contains filtered or unexported fields
}

RefCtr cancels a context when no references are held

func ContextWithRefCount

func ContextWithRefCount(c context.Context) (context.Context, *RefCtr)

ContextWithRefCount derives a context that will be cancelled when all references are freed.

func WithRefCount

func WithRefCount(d ctx.Doner) (ctx.C, *RefCtr)

WithRefCount derives a ctx.C that will be cancelled when all references are freed

func (*RefCtr) Add

func (r *RefCtr) Add(i uint32)

Add i refcounts

func (*RefCtr) Decr

func (r *RefCtr) Decr()

Decr decrements the refcount

func (*RefCtr) Incr

func (r *RefCtr) Incr()

Incr increments the refcount

Jump to

Keyboard shortcuts

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