executor.go 777 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package ctrl
  2. import (
  3. "context"
  4. "reflect"
  5. "runtime"
  6. "sync"
  7. "go-common/library/log"
  8. )
  9. func NewUnboundedExecutor() *UnboundedExecutor {
  10. ctx := context.Background()
  11. return &UnboundedExecutor{
  12. ctx: ctx,
  13. }
  14. }
  15. type UnboundedExecutor struct {
  16. wg sync.WaitGroup
  17. // for future extension
  18. ctx context.Context
  19. }
  20. type Executor interface {
  21. Submit(bizFunc ...func(ctx context.Context))
  22. }
  23. func (executor *UnboundedExecutor) Submit(bizFunc ...func(c context.Context)) {
  24. for _, biz := range bizFunc {
  25. pc := reflect.ValueOf(biz).Pointer()
  26. funcName := runtime.FuncForPC(pc).Name()
  27. executor.wg.Add(1)
  28. go func(funcName string, biz func(ctx context.Context)) {
  29. defer executor.wg.Done()
  30. log.Info("Exec Task %s", funcName)
  31. biz(executor.ctx)
  32. }(funcName, biz)
  33. }
  34. }