closer

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: MIT Imports: 4 Imported by: 2

README

closer

Build Status Go Report Card GoDoc

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add added in v0.1.0

func Add(f ...func() error)

Add add closed func.

func AddByPriority added in v0.1.0

func AddByPriority(priority uint8, f ...func() error)

AddByPriority add close by priority 255 its close first 0 - last.

func AddFirst added in v0.1.0

func AddFirst(f ...func() error)

AddFirst add closer which execute at the begin.

func AddLast added in v0.1.0

func AddLast(f ...func() error)

AddLast add closer which execute at the end.

func Close added in v0.1.0

func Close() error

Close all func. nolint: wrapcheck

Example
package main

import (
	"time"

	"gitoa.ru/go-4devs/closer"
	"gitoa.ru/go-4devs/closer/priority"
)

func main() {
	closer.Add(func() error {
		// normal stop.

		return nil
	}, func() error {
		time.Sleep(time.Millisecond)
		// long normal stop.

		return nil
	})

	closer.AddFirst(func() error {
		// first stop.

		return nil
	})
	closer.AddLast(func() error {
		// last stop.

		return nil
	})

	closer.AddByPriority(priority.First+1, func() error {
		// run before first.

		return nil
	})

	closer.AddByPriority(priority.Normal-1, func() error {
		// run after normal.

		return nil
	})
	closer.Close()
}
Output:

func SetErrHandler added in v0.1.0

func SetErrHandler(eh func(error))

SetErrHandler before close func.

Example
package main

import (
	"log"

	"gitoa.ru/go-4devs/closer"
	"gitoa.ru/go-4devs/closer/test"
)

func main() {
	closer.SetErrHandler(func(err error) {
		log.Print("logged err:", err.Error())
	})

	closer.Add(func() error {
		return test.ErrClose
	})

	closer.Close()
}
Output:

func SetTimeout added in v0.1.0

func SetTimeout(t time.Duration)

SetTimeout before close func.

func Shutdown added in v0.1.1

func Shutdown(fnc func(context.Context) error, timeout time.Duration) func() error

func Wait added in v0.1.0

func Wait(ctx context.Context, sig ...os.Signal)

Wait cancel ctx or signal.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"gitoa.ru/go-4devs/closer"
	"gitoa.ru/go-4devs/closer/priority"
	"gitoa.ru/go-4devs/closer/test"
)

func main() {
	ctx, cancel := context.WithTimeout(context.TODO(), time.Microsecond)
	defer cancel()

	closer.SetErrHandler(func(err error) {
		fmt.Print("\nlogged err:", err.Error())
	})

	closer.Add(func() error {
		fmt.Print("do some close. ")

		return nil
	})

	closer.AddFirst(func() error {
		fmt.Print("close http server for example. ")

		return nil
	})

	closer.AddLast(func() error {
		fmt.Print("close db for example.")

		return nil
	})

	closer.AddByPriority(priority.Last-1, func() error {
		return test.ErrClose
	})

	closer.Wait(ctx)
}
Output:

close http server for example. do some close. close db for example.
logged err:some error
Example (CancelContext)
package main

import (
	"context"
	"time"

	"gitoa.ru/go-4devs/closer"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond)
	defer cancel()

	closer.Add(func() error {
		// do some close with cancel context

		return nil
	})

	closer.Wait(ctx)
}
Output:

Example (Syscall)
package main

import (
	"context"
	"syscall"
	"time"

	"gitoa.ru/go-4devs/closer"
)

func main() {
	time.AfterFunc(time.Millisecond, func() {
		_ = syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
	})

	closer.Add(func() error {
		// do some close with SIGTERM

		return nil
	})

	closer.Wait(context.TODO(), syscall.SIGTERM)
}
Output:

Types

This section is empty.

Directories

Path Synopsis
routine module

Jump to

Keyboard shortcuts

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