closer

package module
v2.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2018 License: GPL-3.0 Imports: 3 Imported by: 2

README

Closer - A simple thread-safe closer

GoDoc Go Report Card

type Server struct {
    closer.Closer // Embedded
}

func New() *Server {
    return &Server {
        Closer: closer.New(),
    }
}

func main() {
    s := New()

    // Do something on close...
    go func() {
        <-s.CloseChan
        // ...
    } ()

    // ...
    s.Close()
}
type Server struct {
    closer.Closer // Embedded
    conn net.Conn
}

func New() *Server {
    // ...
    s := &Server {
        conn: conn,
    }
    s.Closer = closer.New(s.onClose)
    return s
}

func (s *server) onClose() error {
    return s.conn.Close()
}

func main() {
    s := New()
    // ...

    // The s.onClose function will be called only once.
    s.Close()
    s.Close()
}

Documentation

Overview

Package closer offers a simple thread-safe closer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseFunc

type CloseFunc func() error

CloseFunc defines the general close function.

type Closer

type Closer interface {
	// Close in a thread-safe manner. implements the io.Closer interface.
	// This method returns always the close error, regardless of how often
	// this method is called. Close blocks until all close functions are done,
	// no matter which goroutine called this method.
	// Returns a hashicorp multierror.
	Close() error

	// CloseChan channel which is closed as
	// soon as the closer is closed.
	CloseChan() <-chan struct{}

	// CloserWaitGroup returns the wait group for this closer.
	// Use this wait group to wait before calling the close functions.
	CloserWaitGroup() *sync.WaitGroup

	// IsClosed returns a boolean indicating
	// if this instance was closed.
	IsClosed() bool

	// Calls the close function on close.
	// Errors are appended to the Close() multi error.
	// Close functions are called in LIFO order.
	OnClose(f CloseFunc)
}

A Closer is a thread-safe helper for common close actions.

func New

func New(f ...CloseFunc) Closer

New creates a new closer. Optional pass functions which are called only once during close. Close function are called in LIFO order.

Jump to

Keyboard shortcuts

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