Git Product home page Git Product logo

go-workqueue's Introduction

go-workqueue

原理

支持go module

任务分为顺序执行和乱序并发执行两种

任务通过channel入队

每隔一定时间开始执行任务队列

顺序执行队列, 按入队顺序执行任务, 只有前一个执行完成才会执行下一个.

乱序执行队列, 并发执行所有任务.

运行

go test -v -race

Example

import (
  "time"
  
  gow "github.com/userpro/go-workqueue"
)

gow.SetDuration(time.Second) // [该函数调用非必须] workqueue的扫描间隔(默认1s)
gow.Start() // 开始扫描
gow.SetGroupStart("testOrder", func() bool {}) // [该函数调用非必须] 第一个参数是分组名, 每个分组名对应一个任务队列; 第二个参数是一个无入参返回bool的函数, 每次先执行该函数, 由其返回值决定是否执行对应的队列

gow.DelGroup(g string) // 删除指定分组

gow.Ch <- &gow.Item{
  Type:  gow.Order, // 任务类型 分 Order(顺序执行), Rand(乱序执行)
  Group: "testOrder", // 分组名
  GroupItem: gow.GroupItem{
    Task: ...,
    Do: func(args ...interface{}) error {
      task, _ := args[0].(...)
      ... args[0] => Task, 返回error会执行retry函数 ...
      if ... {
        return errors.New("...")
      }
      return nil
    },
    Retry: func(args ...interface{}) bool {
      task, _ := args[0].(...)
      ... 重试策略函数 ...
      ... args[0] => Task, 返回true会继续尝试执行Do函数 ...
      if ... {
        return false
      }
      return true
    },
    Callback: func(args ...interface{}) {
      task, _ := args[0].(...)
      done, _ := args[1].(bool) // done true函数最终执行成功 false失败
      ...
    },
  },
}

// 建议封装成类似如下函数的形式使用
func sendEmptyTask(f func() bool) {
	gow.Ch <- &gow.Item{
		Type:  Order,
		Group: "groupName",
		GroupItem: gow.GroupItem{
			Do: func(args ...interface{}) error {
				if f() {
					return nil
				}
				return errors.New("[sendEmptyTask] retry task")
			},
			Retry: func(args ...interface{}) bool {
				return true
			},
		},
	}
}

更详细见 workqueue_test.go

go-workqueue's People

Contributors

userpro avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.