Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Callable ¶ added in v1.0.7
type Callable func() any
Callable provides a function that returns a value of type any.
type CancelCallable ¶ added in v1.0.9
type CancelCallable func(token CancelToken) any
CancelCallable provides a cancellable function that returns a value of type any.
type CancelRunnable ¶ added in v1.0.9
type CancelRunnable func(token CancelToken)
CancelRunnable provides a cancellable function without return values.
type CancelToken ¶ added in v1.0.9
type CancelToken interface {
// IsCanceled returns true if task was canceled.
IsCanceled() bool
// Cancel notifies the waiting coroutine that the task has canceled and returns stack information.
Cancel()
}
CancelToken propagates notification that operations should be canceled.
type Cloneable ¶ added in v1.0.3
type Cloneable interface {
// Clone create and returns a copy of this object.
Clone() any
}
Cloneable interface to support copy itself.
type FutureCallable ¶ added in v1.1.2
type FutureCallable func(task FutureTask) any
FutureCallable provides a future function that returns a value of type any.
type FutureTask ¶ added in v1.1.2
type FutureTask interface {
// IsDone returns true if completed in any fashion: normally, exceptionally or via cancellation.
IsDone() bool
// IsCanceled returns true if task was canceled.
IsCanceled() bool
// IsFailed returns true if completed exceptionally.
IsFailed() bool
// Complete notifies the waiting coroutine that the task has completed normally and returns the execution result.
Complete(result any)
// Cancel notifies the waiting coroutine that the task has canceled and returns stack information.
Cancel()
// Fail notifies the waiting coroutine that the task has terminated due to panic and returns stack information.
Fail(error any)
// Get return the execution result of the sub-coroutine, if there is no result, return nil.
// If task is canceled, a panic with cancellation will be raised.
// If panic is raised during the execution of the sub-coroutine, it will be raised again at this time.
Get() any
// GetWithTimeout return the execution result of the sub-coroutine, if there is no result, return nil.
// If task is canceled, a panic with cancellation will be raised.
// If panic is raised during the execution of the sub-coroutine, it will be raised again at this time.
// If the deadline is reached, a panic with timeout error will be raised.
GetWithTimeout(timeout time.Duration) any
// Run execute the task, the method can be called repeatedly, but the task will only execute once.
Run()
}
FutureTask provide a way to wait for the sub-coroutine to finish executing, get the return value of the sub-coroutine, and catch the sub-coroutine panic.
func GoWait ¶ added in v1.0.2
func GoWait(fun CancelRunnable) FutureTask
GoWait starts a new goroutine, and copy inheritableThreadLocals from current goroutine. This function will auto invoke the func and return a FutureTask instance, so we can wait by FutureTask.Get or FutureTask.GetWithTimeout method. If panic occur in goroutine, The panic will be trigger again when calling FutureTask.Get or FutureTask.GetWithTimeout method.
func GoWaitResult ¶ added in v1.0.2
func GoWaitResult(fun CancelCallable) FutureTask
GoWaitResult starts a new goroutine, and copy inheritableThreadLocals from current goroutine. This function will auto invoke the func and return a FutureTask instance, so we can wait and get result by FutureTask.Get or FutureTask.GetWithTimeout method. If panic occur in goroutine, The panic will be trigger again when calling FutureTask.Get or FutureTask.GetWithTimeout method.
func NewFutureTask ¶ added in v1.1.2
func NewFutureTask(callable FutureCallable) FutureTask
NewFutureTask Create a new instance.
func WrapTask ¶ added in v1.1.2
func WrapTask(fun Runnable) FutureTask
WrapTask create a new task and capture the inheritableThreadLocals from the current goroutine. This function returns a FutureTask instance, but the return task will not run automatically. You can run it in a sub-goroutine or goroutine-pool by FutureTask.Run method, wait by FutureTask.Get or FutureTask.GetWithTimeout method. When the returned task run panic will be caught and error stack will be printed, the panic will be trigger again when calling FutureTask.Get or FutureTask.GetWithTimeout method.
func WrapWaitResultTask ¶ added in v1.1.2
func WrapWaitResultTask(fun CancelCallable) FutureTask
WrapWaitResultTask create a new task and capture the inheritableThreadLocals from the current goroutine. This function returns a FutureTask instance, but the return task will not run automatically. You can run it in a sub-goroutine or goroutine-pool by FutureTask.Run method, wait and get result by FutureTask.Get or FutureTask.GetWithTimeout method. When the returned task run panic will be caught, the panic will be trigger again when calling FutureTask.Get or FutureTask.GetWithTimeout method.
func WrapWaitTask ¶ added in v1.1.2
func WrapWaitTask(fun CancelRunnable) FutureTask
WrapWaitTask create a new task and capture the inheritableThreadLocals from the current goroutine. This function returns a FutureTask instance, but the return task will not run automatically. You can run it in a sub-goroutine or goroutine-pool by FutureTask.Run method, wait by FutureTask.Get or FutureTask.GetWithTimeout method. When the returned task run panic will be caught, the panic will be trigger again when calling FutureTask.Get or FutureTask.GetWithTimeout method.
type Runnable ¶ added in v1.0.7
type Runnable func()
Runnable provides a function without return values.
type RuntimeError ¶ added in v1.0.8
type RuntimeError interface {
// Goid returns the goid of the coroutine that created the current error.
Goid() int64
// Gopc returns the pc of go statement that created the current error coroutine.
Gopc() uintptr
// Message returns the detail message string of this error.
Message() string
// StackTrace returns an array of stack trace elements, each representing one stack frame.
StackTrace() []uintptr
// Cause returns the cause of this error or nil if the cause is nonexistent or unknown.
Cause() RuntimeError
// Error returns a short description of this error.
Error() string
}
RuntimeError runtime error with stack info.
func NewRuntimeError ¶ added in v1.0.8
func NewRuntimeError(cause any) RuntimeError
NewRuntimeError create a new RuntimeError instance.
func NewRuntimeErrorWithMessage ¶ added in v1.0.8
func NewRuntimeErrorWithMessage(message string) RuntimeError
NewRuntimeErrorWithMessage create a new RuntimeError instance.
func NewRuntimeErrorWithMessageCause ¶ added in v1.0.8
func NewRuntimeErrorWithMessageCause(message string, cause any) RuntimeError
NewRuntimeErrorWithMessageCause create a new RuntimeError instance.
type Supplier ¶ added in v1.0.2
type Supplier func() any
Supplier provides a function that returns a value of type any.
type ThreadLocal ¶ added in v1.0.2
type ThreadLocal interface {
// Get returns the value in the current goroutine's local threadLocals or inheritableThreadLocals, if it was set before.
Get() any
// Set copy the value into the current goroutine's local threadLocals or inheritableThreadLocals.
Set(value any)
// Remove delete the value from the current goroutine's local threadLocals or inheritableThreadLocals.
Remove()
}
ThreadLocal provides goroutine-local variables.
func NewInheritableThreadLocal ¶ added in v1.0.2
func NewInheritableThreadLocal() ThreadLocal
NewInheritableThreadLocal create and return a new ThreadLocal instance. The initial value stored with nil. The value can be inherited to sub goroutines witch started by Go, GoWait, GoWaitResult methods. The value can be captured to FutureTask which created by WrapTask, WrapWaitTask, WrapWaitResultTask methods.
func NewInheritableThreadLocalWithInitial ¶ added in v1.0.2
func NewInheritableThreadLocalWithInitial(supplier Supplier) ThreadLocal
NewInheritableThreadLocalWithInitial create and return a new ThreadLocal instance. The initial value stored as the return value of the method supplier. The value can be inherited to sub goroutines witch started by Go, GoWait, GoWaitResult methods. The value can be captured to FutureTask which created by WrapTask, WrapWaitTask, WrapWaitResultTask methods.
func NewThreadLocal ¶ added in v1.0.2
func NewThreadLocal() ThreadLocal
NewThreadLocal create and return a new ThreadLocal instance. The initial value stored with nil.
func NewThreadLocalWithInitial ¶ added in v1.0.2
func NewThreadLocalWithInitial(supplier Supplier) ThreadLocal
NewThreadLocalWithInitial create and return a new ThreadLocal instance. The initial value stored as the return value of the method supplier.