New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/mariuswilms/tears

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/mariuswilms/tears

  • v0.0.0-20241122112056-b272bc2cfcb1
  • Source
  • Go
  • Socket score

Version published
Created
Source

tears

Go Reference

tears provides an elegant way to register cleanup functions, which are then called when the program exits. Unlike defer, it adds handling for several cleanup approaches, such as quit channels and similar mechanisms, and it offers more robustness against deadlocks and ensures proper resource teardown. The package has no dependencies outside of the standard library.

Usage

Commonly resources used throughout the lifetime of a mid-to-smallish program are set up within the main function. When the program exits, these resources need to be cleaned up. This is where tears comes in.

func main() {
    tear, down := tears.New()

    conn, _ := net.Dial("tcp", "example.com:80")
    tear(conn.Close)

    // ...
    down(ctx)
}

Over the defer statement tears has the advantage that cleanup tasks can be registered in one method and later run in another method. This is commonly the case if you separate the start and stop/close logic with methods on a struct.

type Server struct {
    tears.Cleaner
    // ...
}

func (s *Server) Start() {
    s.Tear(lis.Close)
    // ...
}

func (s *Server) Stop() {
    s.Down(context.Background())
}

Outside of calling a function directly, tears can help in closing go routines by using socalled quit-channels.

func main() {
    tear, down := tears.New()

    quit := make(chan bool)
    tear(quit)

    go func() {
        select {
        // ...
        case <-quit:
            return
        }
    }
    
    // ...
    down(context.Background())
}

Usually cleanup functions are called in a LIFO order. If you need to break out of that, tears provides a way to prioritize cleanup functions using End().

tear(conn.Close).End()

It's also possible to hook shutdown functions of another setup process into the main one.

// main.go
func main() {
    tear, down := tears.New()

    otel, shutdown := setupOtel()
    tear(shutdown)

    // ...
    down(ctx)
}

// otel.go
func setupOtel() (tears.DownFn) {
    tear, down := tears.New()

    tear(/* ... */)

    return down
}

FAQs

Package last updated on 22 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc