nasync

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: Apache-2.0 Imports: 3 Imported by: 7 modules, 8 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

NASYNC

Go Report Card

a customizable async task pool for golang, (event bus, runtime)

Fetures

  • less memory
  • more effective
  • max gorutines and memory customizable
  • more safe

Simple Usage

nasync.Do(function)

Async Http Client

http.DefaultTransport.(*http.Transport).MaxIdleConns = 2000
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 1000
	
nasync.Do(func() {
    resp, err := http.Get("http://example.com")
    if err == nil {
        io.Copy(ioutil.Discard, resp.Body)
        resp.Body.Close()
    }
})

Advanced Usage

go get github.com/ti/nasync
import "github.com/ti/nasync"

func main() {
        //new a async pool in max 1000 task in max 1000 gorutines
        async := nasync.New(1000,1000)
        defer async.Close()
        async.Do(doSometing,"hello word")
}


func doSometing(msg string) string{
	return "i am done by " + msg
}


WHY

golang is something easy but fallible language, you may do this

   func yourfucntion() {
            go dosomething()  // this will got error on high load
    }

you may get "too many open files" error, when your application in High load, so you need this, you can do any thing in async by this, it is trusty。your can use this for:

  • http or file writer logging
  • improve main thread speed
  • limited background task pool

What if something callback ?

import "github.com/ti/nasync"

func main() {
        nasync.Do(func() {
        		result := doSometing("msg")
        		fmt.Println("i am call back by ",result)
        })
}

func doSometing(msg string) string{
	return "i am done by " + msg
}

Documentation

Index

Constants

View Source
const (
	//DefaultReqSize Default max goroutine created
	DefaultReqSize = 1000
	//DefaultBufSize Default task when on goroutine
	DefaultBufSize = 1000
)

Variables

This section is empty.

Functions

func Do

func Do(handler interface{}, params ...interface{})

Do use nasync do some functions

Types

type Async

type Async struct {
	// contains filtered or unexported fields
}

Async async model

var DefaultAsync *Async

DefaultAsync default instance when you run Do(...)

func New

func New(ReqSize int, BufSzie int) *Async

New custom your async

func (*Async) Close

func (a *Async) Close()

Close sends quit signal to watcher and releases all the resources. Wait for all tasks complete to close

func (*Async) Do

func (a *Async) Do(handler interface{}, params ...interface{})

Do some functions

func (*Async) Wait added in v1.0.2

func (a *Async) Wait()

Wait for all tasks complete to close

Jump to

Keyboard shortcuts

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