mkill

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2020 License: MIT Imports: 10 Imported by: 0

README

mkill

PkgGoDev Go Report Card mkill

Package mkill limits the number of threads in a Go program, without crash the whole program.

import "golang.design/x/mkill"

Quick Start

mkill.GOMAXTHREADS(10)

License

MIT © The golang.design Authors

Documentation

Overview

Package mkill limits the number of threads in a Go program, without crash the whole program.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GOMAXTHREADS

func GOMAXTHREADS(n int) int

GOMAXTHREADS sets the maximum number of system threads that allowed in a Go program and returns the previous setting. If n is lower than minimum required number of threads, it does not change the current setting. The minimum allowed number of threads of a program is runtime.NumCPU() + 2.

Example
package main

import (
	"golang.design/x/mkill"
)

func main() {
	mkill.GOMAXTHREADS(10)
}
Output:

func NumM

func NumM() int

NumM returns the number of running threads.

Example
package main

import (
	"context"
	"fmt"

	"golang.design/x/mkill"
)

func main() {
	mkill.GOMAXTHREADS(10)
	mkill.Wait(context.Background())
	fmt.Println(mkill.NumM() <= 10)
}
Output:

true

func Wait

func Wait(ctx context.Context) (ok bool)

Wait waits until the number of threads meet the GOMAXTHREADS settings. The function always returns true if the ctx is not canceled. Otherwise returns true only if the Wait is successed in the last check.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"golang.design/x/mkill"
)

func main() {
	mkill.GOMAXTHREADS(10)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*100)
	defer cancel()
	fmt.Println(mkill.Wait(ctx))
}
Output:

true

Types

This section is empty.

Jump to

Keyboard shortcuts

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