func (v *Valve) Stop() <-chan struct{} {
	return v.stopCh
}
Stop returns a channel that will be closed once the system is supposed to
be stopped. It mimics the behaviou of the ctx.Done() method in "context".
package valve
import (
	"context"
	"errors"
	"net/http"
	"sync"
	"time"
)
var (
	ValveCtxKey     = &contextKey{"Valve"}
	ErrTimedout     = errors.New("valve: shutdown timed out")
	ErrShuttingdown = errors.New("valve: shutdown in progress")
	ErrOff          = errors.New("valve: valve already shutdown")
)
type Valve struct {
	stopCh chan struct{}
	wg     sync.WaitGroup
	shutdown bool
	mu       sync.Mutex
}
type LeverControl interface {
	Stop() <-chan struct{}
	Add(delta int) error
	Done()
	Open() error
	Close()
}
func New() *Valve {
	return &Valve{
		stopCh: make(chan struct{}, 0),
	}
}
func (v *Valve) Context() context.Context {
	return context.WithValue(context.Background(), ValveCtxKey, LeverControl(v))
}
func Lever(ctx context.Context) LeverControl {
	valveCtx, ok := ctx.Value(ValveCtxKey).(LeverControl)
	if !ok {
		panic("valve: ValveCtxKey has not been set on the context.")
	}
	return valveCtx
}
func (v *Valve) Shutdown(timeout time.Duration) error {
	v.mu.Lock()
	defer v.mu.Unlock()
	if v.shutdown {
		return ErrOff
	}
	close(v.stopCh)
	v.shutdown = true
	if timeout == 0 {
		v.wg.Wait()
	} else {
		tc := make(chan struct{})
		go func() {
			defer close(tc)
			v.wg.Wait()
		}()
		select {
		case <-tc:
			return nil
		case <-time.After(timeout):
			return ErrTimedout
		}
	}
	return nil
}