aop

package
v0.0.0-...-c340371 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2018 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package aop is Aspect Oriented Programming.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delay

func Delay(work func(), d time.Duration)

Delay will call work after time of d

func Parallel

func Parallel(funcs ...interface{})

Parallel can make works in parallel, and wait for all complete.

Example:

aop.Parallel(func(complete chan bool) {
	t.Log(1)
	complete <- true
}, func(complete chan bool) {
	t.Log(2)
	complete <- true
}, func(complete chan bool) {
	t.Log(3)
	complete <- true
}, func(complete chan bool) {
	t.Log(4)
	complete <- true
}, func(complete chan bool) {
	t.Log(5)
	complete <- true
})

func Recover

func Recover(work func(), onErr func(interface{}))

Recover just like try...catch to other languages

func Repeat

func Repeat(work func(), times int)

Repeat will call work in times

func Retry

func Retry(work func(), times int)

Retry will call work while work cause panic in times

func Sequence

func Sequence(funcs ...interface{})

Sequence can make parallel work in sequence, and 2nd func can have input from 1st func's return value, and so on.

Example:

aop.Sequence(func(next chan bool, exit chan bool) int {
	fmt.Println(1, "=>", 1)
	next <- true
	return 1
}, func(next chan bool, exit chan bool, a int) int {
	fmt.Println(2, "=>", a)
	next <- true
	return a + 1
}, func(next chan bool, exit chan bool, a int) int {
	fmt.Println(3, "=>", a)
	next <- true
	return a + 1
}, func(next chan bool, exit chan bool, a int) int {
	fmt.Println(4, "=>", a)
	exit <- true
	return a + 1
}, func(next chan bool, exit chan bool, a int) {
	fmt.Println(5, "=>", a)
	next <- true
})

Types

type Aspect

type Aspect interface {
	Recover(err func(interface{})) Aspect
	Retry(times int) Aspect
	Repeat(times int) Aspect
	Delay(d time.Duration) Aspect
	Do(work func())
}

Aspect can do calls like chain

NewAspect().
    Retry(3).
    Delay(10 * time.Second).
    Repeat(5).
    Do(func() {
        fmt.Println("Test")
})

Means print Test line 5 times, after 10 seconds, if panic happen retry 3 times.

func NewAspect

func NewAspect() Aspect

NewAspect is constructor of Aspect

Jump to

Keyboard shortcuts

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