Closer is a Go package that provides a mechanism for managing the closing of multiple functions in a controlled and concurrency-safe manner. The package allows you to add functions that should be executed upon closing and then close them one by one or all at once.
- Add Functions: You can add functions that need to be executed upon closing using the
Addmethod. - Close All Functions: The
Closemethod allows you to close all added functions simultaneously, executing them in separate goroutines. Note: The Close method does not guarantee the order of execution. - Step-by-Step Closing: The
CloseOnemethod allows you to close functions one by one in aFIFO(First-In-First-Out) order, which can be useful in scenarios where sequential resource closing is required. - Concurrency Safety: All operations with functions are synchronized using a mutex, ensuring safety in a multi-threaded environment.
- Error Handling: If errors occur while closing functions, they are collected and returned as a single error message.
package main
import (
"context"
"fmt"
"time"
"github.com/ilKhr/closer"
)
func main() {
ctx := context.Background()
// Create an instance of Closer
var cl closer.Closer
// Add functions to be closed
cl.Add(func(ctx context.Context) error {
fmt.Println("Closing service 1")
time.Sleep(5 * time.Second)
return nil
})
cl.Add(func(ctx context.Context) error {
fmt.Println("Closing service 2")
time.Sleep(3 * time.Second)
return fmt.Errorf("error closing service 2")
})
// Close all functions
err := cl.Close(ctx)
if err != nil {
fmt.Printf("Error closing services: %v\n", err)
}
}Adds the function f to the list of functions that should be closed.
Closes all added functions simultaneously. If errors occur while closing, they are collected and returned as a single error message.
Closes one function and updates the index for the next operation. If all functions have already been closed, it returns the ErrAllServicesClosed error.
Returns the number of added functions to be closed.
The type of function that takes a context and returns an error. This type is used for adding functions to the closing list.
ErrAllServicesClosed: Returned if all functions have already been closed, and attempting to close them again is meaningless.
The package uses standard Go libraries such as context, fmt, strings, and sync.
go get github.com/ilKhr/closerThis project is licensed under the MIT License.
Khorishko Ilya
Contributions are welcome! Please create an issue or pull request on GitHub.