Documentation ¶
Overview ¶
* Package concurrent provides concurrent utility apis
Index ¶
- Constants
- Variables
- func AsyncCall[E any](f base.Supplier[E], timeout time.Duration) (future base.Supplier[E], err error)
- func AsyncCallWithEvent[E, T any](f base.Supplier[E], cancal <-chan T) (future base.Supplier[E], err error)
- func AsyncGo(f base.Call, timeout time.Duration) (ok bool, err error)
- func AsyncGoWithEvent[E any](f base.Call, cancal <-chan E) (ok bool, err error)
- func ReSetTimeWheel(slotNum uint16, interval time.Duration) error
- func SafeCloseChan[E any](c chan E) (ok bool)
- func TryRecevieChan[E any](c <-chan E, timeout time.Duration) (ok bool, v E)
- func TrySendChan[E any](v E, c chan<- E, timeout time.Duration) (ok bool)
Examples ¶
Constants ¶
const ( TIME_OUT_TW = 1 TIME_OUT_TICKER = 2 )
Variables ¶
var ( EmptyFn = func() {} TimeOutType = TIME_OUT_TW )
Functions ¶
func AsyncCall ¶
func AsyncCall[E any](f base.Supplier[E], timeout time.Duration) (future base.Supplier[E], err error)
AsyncCall execute target function by goroutine and has a generic returned parameter. if panic happened will wrap error object and return future(nil) if just time out will return future(func), err(nil)
Example ¶
get := func() (name, address string) { return "matt", "pudong" } // run call function in async way f, err := concurrent.AsyncCall(func() []string { time.Sleep(200 * time.Millisecond) name, address := get() return arrayutil.AsList(name, address) }, time.Second) fmt.Println(f(), err) // run call function in async way and ocurres timeout f, err = concurrent.AsyncCall(func() []string { time.Sleep(2 * time.Second) name, address := get() return arrayutil.AsList(name, address) }, time.Second) fmt.Println(f(), err) // run call function in async way without time wait f, err = concurrent.AsyncCall(func() []string { time.Sleep(2 * time.Second) name, address := get() return arrayutil.AsList(name, address) }, 0) fmt.Println(f(), err)
Output: [matt pudong] <nil> [matt pudong] AsyncCall execute timeout [matt pudong] <nil>
func AsyncCallWithEvent ¶ added in v1.0.8
func AsyncCallWithEvent[E, T any](f base.Supplier[E], cancal <-chan T) (future base.Supplier[E], err error)
AsyncCall execute target function by goroutine and has a generic returned parameter. if panic happened will wrap error object and return future(nil) if just time out will return future(func), err(nil)
func AsyncGo ¶
AsyncGo execute target function by goroutine. if panic happened will wrap error object and return false if just time out will return ok(false), err(nil)
Example ¶
// run function in async way f, err := concurrent.AsyncGo(func() { time.Sleep(200 * time.Millisecond) }, time.Second) fmt.Println(f, err) // run function in async way and ocurres timeout f, err = concurrent.AsyncGo(func() { time.Sleep(2 * time.Second) }, time.Second) fmt.Println(f, err) // run function in async way and panic error f, err = concurrent.AsyncGo(func() { panic("surprise") }, time.Second) fmt.Println(f, err.Error())
Output: true <nil> false <nil> false surprise
func AsyncGoWithEvent ¶ added in v1.0.8
AsyncGoWithEvent 是一个异步执行的函数,它接受一个函数f和一个事件通道toevent作为参数 f函数将在新的goroutine中执行,如果f函数执行成功,则ch通道关闭 如果在f函数执行前,toevent通道接收到事件,则函数返回false 函数返回两个值,ok表示f函数是否执行成功,err表示f函数执行时产生的错误 参数: f:要执行的函数 cancal:事件通道 返回值: ok:f函数是否执行成功 err:f函数执行时产生的错误
func SafeCloseChan ¶
SafeChanClose to close chan for safty way. if channel is closed will retrun false
func TryRecevieChan ¶
TryRecevie try to receive value from channel within target time
Example ¶
// TryReceive from nil channel var ch chan string = nil ok, _ := concurrent.TryRecevieChan(ch, 1*time.Second) fmt.Println(ok) // TryReceive from closed channel ch = make(chan string) concurrent.SafeCloseChan(ch) ok, v := concurrent.TryRecevieChan(ch, 1*time.Second) fmt.Println(ok) fmt.Println(v) // empty value // TryReceive in time ch = make(chan string, 1) ch <- "hello" ok, v = concurrent.TryRecevieChan(ch, 1*time.Second) fmt.Println(ok, v) // TryReceive from no value channel and occures time out ch = make(chan string, 1) ok, _ = concurrent.TryRecevieChan(ch, 1*time.Second) fmt.Println(ok)
Output: false true true hello false
func TrySendChan ¶
TrySend try to send value to channel within target time
Example ¶
s := "hello" // TrySend to nil channel var ch chan string = nil ok := concurrent.TrySendChan(s, ch, 1*time.Second) fmt.Println(ok) // TrySend to closed channel ch = make(chan string) concurrent.SafeCloseChan(ch) ok = concurrent.TrySendChan(s, ch, 1*time.Second) fmt.Println(ok) // TrySend to no buffered channel and occures time out ch = make(chan string) ok = concurrent.TrySendChan(s, ch, 1*time.Second) fmt.Println(ok) ch = make(chan string, 1) ok = concurrent.TrySendChan(s, ch, 1*time.Second) fmt.Println(ok)
Output: false false false true
Types ¶
This section is empty.