项目作者: Focinfi

项目描述 :
Batch processing in golang
高级语言: Go
项目地址: git://github.com/Focinfi/membatch.git
创建时间: 2018-12-28T10:30:43Z
项目社区:https://github.com/Focinfi/membatch

开源协议:

下载


membatch

Batch processing in golang, inspired by gobatch.

Install

  1. go get github.com/Focinfi/membatch

Demo

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "time"
  6. "github.com/Focinfi/membatch"
  7. )
  8. func main() {
  9. // 1. options
  10. ctx := context.TODO()
  11. // tasks: task_id => params
  12. // resp: task_id => data
  13. echoFunc := func(ctx context.Context, tasks map[string]map[string]interface{}) (resp map[string]interface{}, err error) {
  14. resp = make(map[string]interface{})
  15. for taskID := range tasks {
  16. resp[taskID] = taskID
  17. }
  18. return
  19. }
  20. flushMaxSize := 100 // process threshold for the count of waiting tasks
  21. flushMaxWait := time.Second // process threshold for waiting duration
  22. workerNum := 2 // the number of gorountine to handling task
  23. // 2. build a new batch
  24. batch := membatch.New(ctx, echoFunc, flushMaxSize, flushMaxWait, workerNum)
  25. // 3. one task args
  26. respChan := make(chan *membatch.TaskResp)
  27. taskArgs := membatch.TaskArgs{
  28. ID: "1",
  29. RespChan: respChan,
  30. }
  31. // 4. insert one task
  32. if err := batch.Insert(taskArgs); err != nil {
  33. log.Fatal(err)
  34. }
  35. // 5. wait for response
  36. resp := <-respChan
  37. // 6. println 1 after one second
  38. log.Println(resp.Data)
  39. }