github.com/matrixorigin/matrixone@v0.7.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 "sync" 19 20 "github.com/matrixorigin/matrixone/pkg/logutil" 21 iops "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/ops/base" 22 ops "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/worker" 23 "github.com/panjf2000/ants/v2" 24 ) 25 26 var ( 27 poolHandlerName = "PoolHandler" 28 ) 29 30 type poolHandler struct { 31 BaseTaskHandler 32 opExec ops.OpExecFunc 33 pool *ants.Pool 34 wg *sync.WaitGroup 35 } 36 37 func NewPoolHandler(num int) *poolHandler { 38 pool, err := ants.NewPool(num) 39 if err != nil { 40 panic(err) 41 } 42 h := &poolHandler{ 43 BaseTaskHandler: *NewBaseEventHandler(poolHandlerName), 44 pool: pool, 45 wg: &sync.WaitGroup{}, 46 } 47 h.opExec = h.ExecFunc 48 h.ExecFunc = h.doHandle 49 return h 50 } 51 52 func (h *poolHandler) Execute(task Task) { 53 h.opExec(task) 54 } 55 56 func (h *poolHandler) doHandle(op iops.IOp) { 57 closure := func(o iops.IOp, wg *sync.WaitGroup) func() { 58 return func() { 59 h.opExec(o) 60 wg.Done() 61 } 62 } 63 h.wg.Add(1) 64 err := h.pool.Submit(closure(op, h.wg)) 65 if err != nil { 66 logutil.Warnf("%v", err) 67 op.SetError(err) 68 h.wg.Done() 69 } 70 } 71 72 func (h *poolHandler) Close() error { 73 h.pool.Release() 74 h.BaseTaskHandler.Close() 75 h.wg.Wait() 76 return nil 77 }