A lib for monitoring runtime goroutine stack

Overview

A lib for monitoring runtime goroutine stack. Such as wait for goroutines to exit, leak detection, etc.

Features

  • context.Context first design
  • Concurrent leak detection
  • No dependencies and 100% test coverage
  • Provides handy low-level APIs to extend the lib

Guides

Check the examples and godoc for detailed usage.

Let's get started with a typical async producer-consumer model, can you find the leaking issue of the code below? If we visit the port 3000 again and again the service will leak memory.

package main

import (
    "fmt"
    "net/http"
)

func main() {
    _ = http.ListenAndServe(":3000", http.HandlerFunc(handle))
}

func handle(_ http.ResponseWriter, _ *http.Request) {
    c := make(chan int)
    go produce(c)
    go consume(c)
}

func produce(c chan int) {
    for i := range "..." {
        c <- i
    }
}

func consume(c chan int) {
    for i := range c {
        fmt.Println(i)
    }
}

We can use gotrace to locate the leak point, let's create a main_test.go file:

package main

import (
    "testing"

    "github.com/ysmood/gotrace"
)

func TestHandle(t *testing.T) {
    // Just add one line before the standard test case
    gotrace.CheckLeak(t, 0)

    handle(nil, nil)
}

Run GODEBUG="tracebackancestors=10" go test, wait for 3 seconds you will see the below output:

--- FAIL: TestHandle (3.00s)
    main_test.go:10: leaking goroutines: goroutine 6 [chan receive]:
        play.consume(0x0)
            /Users/ys/repos/play/default/main.go:25 +0x74

The range on line 25 get stuck, the reason is chan receive, it keeps waiting for new messages from c, but the produce function has already exited, it will not send messages to c anymore.

We have several ways to fix it, such as change the produce to:

func produce(c chan int) {
    for i := range "..." {
        c <- i
    }
    close(c)
}

Run go test again, the test should exit immediately without errors.

Similar Resources

Provides some convenient API, includes Goid(), AllGoid(), and LocalStorage, which is a goroutine's local storage, just like ThreadLocal in other languages.

routine 中文版 routine encapsulates and provides some easy-to-use, high-performance goroutine context access interfaces, which can help you access corout

Dec 30, 2022

Queue is a Golang library for spawning and managing a Goroutine pool

Queue is a Golang library for spawning and managing a Goroutine pool, Alloowing you to create multiple worker according to limit CPU number of machine.

Jan 9, 2023

Queue is a Golang library for spawning and managing a Goroutine pool

Queue is a Golang library for spawning and managing a Goroutine pool, Alloowing you to create multiple worker according to limit CPU number of machine.

Jan 2, 2023

A universal mechanism to manage goroutine lifecycles

A universal mechanism to manage goroutine lifecycles

Dec 29, 2022

goroutine pool in golang

goroutine pool in golang

Nov 1, 2021

A cross goroutine storage tool with very simple implementation and function.

Simple-goroutine-local is a cross goroutine storage tool with very simple implementation and function (the concept is similar to Java ThreadLocal). Ge

Jan 13, 2022

Goroutine Leak Detector

Leaktest Refactored, tested variant of the goroutine leak detector found in both net/http tests and the cockroachdb source tree. Takes a snapshot of r

Dec 26, 2022

A safe way to execute functions asynchronously, recovering them in case of panic. It also provides an error stack aiming to facilitate fail causes discovery.

Async Provides a safe way to execute functions asynchronously, recovering them in case of panic. It also provides an error stack aiming to facilitate

Dec 20, 2022

:speedboat: a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation

Package pool Package pool implements a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation. Features

Jan 1, 2023

🐜🐜🐜 ants is a high-performance and low-cost goroutine pool in Go, inspired by fasthttp./ ants 是一个高性能且低损耗的 goroutine 池。

🐜🐜🐜 ants is a high-performance and low-cost goroutine pool in Go, inspired by fasthttp./ ants 是一个高性能且低损耗的 goroutine 池。

A goroutine pool for Go English | 🇨🇳 中文 📖 Introduction Library ants implements a goroutine pool with fixed capacity, managing and recycling a massi

Jan 2, 2023

An golang log lib, supports tracking and level, wrap by standard log lib

Logex An golang log lib, supports tracing and level, wrap by standard log lib How To Get shell go get gopkg.in/logex.v1 source code import "gopkg.in/

Nov 27, 2022

cpuworker - A Customized Goroutine Scheduler over Golang Runtime

cpuworker - A Customized Goroutine Scheduler over Golang Runtime

cpuworker Status Working in process. Run the Demo Make sure the GOMAXPROCS is bigger than 1 and there is at least GOMAXPROCS physical OS threads avail

Dec 6, 2022

This is an concurrent-queue and concurrent-stack lib for the go.

This is an concurrent-queue and concurrent-stack lib for the go.

This is an concurrent-queue and concurrent-stack lib for the go. Getting Started Pull in the dependency go get github.com/boobusy/vector Add the impor

Jan 2, 2022

Package gostackparse parses goroutines stack traces as produced by panic() or debug.Stack() at ~300 MiB/s.

gostackparse Package gostackparse parses goroutines stack traces as produced by panic() or debug.Stack() at ~300 MiB/s. Parsing this data can be usefu

Dec 1, 2022

Elastic Stack Docker + Sample Go AppElastic Stack Docker + Sample Go App

Elastic Stack Docker + Sample Go AppElastic Stack Docker + Sample Go App

📶 Elastic Stack Docker + Sample Go App Test Elastic Stack which includes Elasticsearch, Kibana, Filebeat and Metricbeat. It comes with a very simple

Jan 14, 2022

Go runtime metrics stack on your local computer.

Go runtime metrics stack on your local computer.

Goal This repository provides a docker-compose for Go runtime metrics stack on your local computer. It is using influxdb and grafana, it based on the

Oct 8, 2021

Identify containers at runtime and observe them. No container runtime required. Read only access to the kernel.

Linux Telemetry The Double Slit Experiment Taken from an interesting physics anomaly where the behavior of a physical system mutates simply by being o

Sep 18, 2022

golang-runtime-di is a framework for runtime dependency injection in go

golang-runtime-di description golang-runtime-di is a framework for runtime dependency injection in go. usage quickstart add it to your go.mod: go get

Aug 1, 2022

Monitoring stack app for golang

Monitoring Application This is a simple monitoring application taken from the following repositor: https://github.com/AnaisUrlichs/observe-argo-rollou

May 26, 2022
Comments
  • Research pprof label for tracking ancestor goroutines

    Research pprof label for tracking ancestor goroutines

    Maybe we can take advantage of it to analyze the goroutine tree.

    https://www.reddit.com/r/golang/comments/ttllrr/comment/i33p4mf/?utm_source=share&utm_medium=web2x&context=3

🐜🐜🐜 ants is a high-performance and low-cost goroutine pool in Go, inspired by fasthttp./ ants 是一个高性能且低损耗的 goroutine 池。
🐜🐜🐜 ants is a high-performance and low-cost goroutine pool in Go, inspired by fasthttp./ ants 是一个高性能且低损耗的 goroutine 池。

A goroutine pool for Go English | ???? 中文 ?? Introduction Library ants implements a goroutine pool with fixed capacity, managing and recycling a massi

Jan 2, 2023
🐝 A Highly Performant and easy to use goroutine pool for Go
🐝 A Highly Performant and easy to use goroutine pool for Go

gohive Package gohive implements a simple and easy to use goroutine pool for Go Features Pool can be created with a specific size as per the requireme

Sep 26, 2022
golang worker pool , Concurrency limiting goroutine pool

golang worker pool 中文说明 Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks su

Dec 19, 2022
Lightweight Goroutine pool

grpool Lightweight Goroutine pool Clients can submit jobs. Dispatcher takes job, and sends it to first available worker. When worker is done with proc

Dec 6, 2022
errgroup with goroutine worker limits

neilotoole/errgroup neilotoole/errgroup is a drop-in alternative to Go's wonderful sync/errgroup but limited to N goroutines. This is useful for inter

Dec 15, 2022
Minimalistic and High-performance goroutine worker pool written in Go

pond Minimalistic and High-performance goroutine worker pool written in Go Motivation This library is meant to provide a simple way to limit concurren

Dec 22, 2022
A goroutine pool for Go
A goroutine pool for Go

Tunny is a Golang library for spawning and managing a goroutine pool, allowing you to limit work coming from any number of goroutines with a synchrono

Dec 31, 2022
Concurrency limiting goroutine pool

workerpool Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks submitting task

Dec 28, 2022
Waiting group for collecting goroutine information.

在go语言waitGroup和errGroup都是用来控制goroutine的并发的方式,前者只能等待所有goroutine执行完成之后再执行Wait()函数后面的代码并且不

Dec 3, 2021
A simple and useful goroutine concurrent library.

Taskgroup A simple and useful goroutine concurrent library. Installation go get github.com/anthhub/taskgroup

May 19, 2021