github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tasks/poolhandler.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tasks 16 17 import ( 18 "context" 19 "sync" 20 21 "github.com/matrixorigin/matrixone/pkg/logutil" 22 iops "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/ops/base" 23 ops "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/worker" 24 "github.com/panjf2000/ants/v2" 25 ) 26 27 var ( 28 poolHandlerName = "PoolHandler" 29 ) 30 31 type poolHandler struct { 32 BaseTaskHandler 33 opExec ops.OpExecFunc 34 pool *ants.Pool 35 wg *sync.WaitGroup 36 } 37 38 func NewPoolHandler(ctx context.Context, num int) *poolHandler { 39 pool, err := ants.NewPool(num) 40 if err != nil { 41 panic(err) 42 } 43 h := &poolHandler{ 44 BaseTaskHandler: *NewBaseEventHandler(ctx, poolHandlerName), 45 pool: pool, 46 wg: &sync.WaitGroup{}, 47 } 48 h.opExec = h.ExecFunc 49 h.ExecFunc = h.doHandle 50 return h 51 } 52 53 func (h *poolHandler) Execute(task Task) { 54 h.opExec(task) 55 } 56 57 func (h *poolHandler) doHandle(op iops.IOp) { 58 closure := func(o iops.IOp, wg *sync.WaitGroup) func() { 59 return func() { 60 h.opExec(o) 61 wg.Done() 62 } 63 } 64 h.wg.Add(1) 65 err := h.pool.Submit(closure(op, h.wg)) 66 if err != nil { 67 logutil.Warnf("%v", err) 68 op.SetError(err) 69 h.wg.Done() 70 } 71 } 72 73 func (h *poolHandler) Close() error { 74 h.pool.Release() 75 h.BaseTaskHandler.Close() 76 h.wg.Wait() 77 return nil 78 }