Help us shape the future of Go! Take the 2026 Go Developer Survey

multicloser

package module
v0.0.0-...-3645f72 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2020 License: MIT Imports: 3 Imported by: 0

README

Multicloser

Provides an io.Closer for closing multiple things at once.

Documentation

Overview

Package multicloser is for easily closing groups of io.Closer types in a safe and ergonomic way that reduces code repetition and reduces the risk of forgetting to handle the errors returned by Close methods.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MultiCloser

type MultiCloser interface {
	io.Closer
	Add(io.Closer, error) io.Closer
}

MultiCloser provides a simple API around io.Closer types for ergonomically allowing lots of things that return io.Closers to be closed together. It's designed for resources that are always only constructed on startup and need to be collectively shut down gracefully.

It embeds the io.Closer interface and declares a single additional method, Add, which is designed to wrap functions that construct types that satisfy the io.Closer interface and also may return an error. It will add the closer to its internal list and close it when its Close method is called.

For example, say you have a bunch of APIs that look like this:

NewDatabaseThing(dsn string, opts Options) (DatabaseThinger, error)

Because this returns a paid of some type that implements io.Closer and an error, it can be passed directly to the Add method:

m.Add(NewDatabaseThing(dsn, opts))

Also note that to remain generic, the Add method returns an io.Closer, the same as the input. So to use your own type, you need to do a simply cast. No need to worry about checking the conversion though since you know exactly what type was passed in:

db := m.Add(NewDatabaseThing(dsn, opts)).(DatabaseThinger)

If your constructors don't return errors, simply fill it with nil:

db := m.Add(NewSimpleThing(opts), nil).(SimpleThinger)

If your constructors return more than (io.Closer, error) then... well, consider simplifying them a bit!

type MultiErrorMultiCloser

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

MultiErrorMultiCloser implements a MultiCloser that uses Hashicorp's multierror library to collect the errors together in the Close method.

func (*MultiErrorMultiCloser) Add

Add implements MultiCloser

func (*MultiErrorMultiCloser) Close

func (m *MultiErrorMultiCloser) Close() (err error)

Close implements io.Closer

type SimpleMultiCloser

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

SimpleMultiCloser implements a very basic MultiCloser. It handles errors by simply appending them together in the Close method.

func (*SimpleMultiCloser) Add

func (m *SimpleMultiCloser) Add(c io.Closer, e error) io.Closer

Add implements MultiCloser

func (*SimpleMultiCloser) Close

func (m *SimpleMultiCloser) Close() (err error)

Close implements io.Closer

type WithHandler

type WithHandler struct {
	ErrorHandler func(error)
	// contains filtered or unexported fields
}

WithHandler implements a basic MultiCloser with the ability to customise how errors are handled in the `Add` method.

func (*WithHandler) Add

func (m *WithHandler) Add(c io.Closer, e error) io.Closer

Add implements MultiCloser If `e` is not nil, the handler will be called before returning nil.

func (*WithHandler) Close

func (m *WithHandler) Close() (err error)

Close implements io.Closer

Jump to

Keyboard shortcuts

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