2022-04-26 11:33:45 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-02-28 14:19:47 +08:00
|
|
|
package conc
|
2022-04-25 15:57:47 +08:00
|
|
|
|
2023-02-27 18:07:48 +08:00
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
|
2023-04-06 19:14:32 +08:00
|
|
|
ants "github.com/panjf2000/ants/v2"
|
2023-02-27 18:07:48 +08:00
|
|
|
)
|
2022-04-25 15:57:47 +08:00
|
|
|
|
2022-04-26 11:33:45 +08:00
|
|
|
// A goroutine pool
|
2022-04-25 15:57:47 +08:00
|
|
|
type Pool struct {
|
|
|
|
inner *ants.Pool
|
|
|
|
}
|
|
|
|
|
2023-02-27 18:07:48 +08:00
|
|
|
// NewPool returns a goroutine pool.
|
|
|
|
// cap: the number of workers.
|
|
|
|
// This panic if provide any invalid option.
|
|
|
|
func NewPool(cap int, opts ...ants.Option) *Pool {
|
2022-04-25 15:57:47 +08:00
|
|
|
pool, err := ants.NewPool(cap, opts...)
|
|
|
|
if err != nil {
|
2023-02-27 18:07:48 +08:00
|
|
|
panic(err)
|
2022-04-25 15:57:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Pool{
|
|
|
|
inner: pool,
|
2023-02-27 18:07:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultPool returns a pool with cap of the number of logical CPU,
|
|
|
|
// and pre-alloced goroutines.
|
|
|
|
func NewDefaultPool() *Pool {
|
|
|
|
return NewPool(runtime.GOMAXPROCS(0), ants.WithPreAlloc(true))
|
2022-04-25 15:57:47 +08:00
|
|
|
}
|
|
|
|
|
2022-04-26 11:33:45 +08:00
|
|
|
// Submit a task into the pool,
|
|
|
|
// executes it asynchronously.
|
|
|
|
// This will block if the pool has finite workers and no idle worker.
|
2023-02-27 12:07:47 +08:00
|
|
|
// NOTE: As now golang doesn't support the member method being generic, we use Future[any]
|
|
|
|
func (pool *Pool) Submit(method func() (any, error)) *Future[any] {
|
|
|
|
future := newFuture[any]()
|
2022-04-25 15:57:47 +08:00
|
|
|
err := pool.inner.Submit(func() {
|
|
|
|
defer close(future.ch)
|
|
|
|
res, err := method()
|
|
|
|
if err != nil {
|
|
|
|
future.err = err
|
|
|
|
} else {
|
|
|
|
future.value = res
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
future.err = err
|
|
|
|
close(future.ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
return future
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:33:45 +08:00
|
|
|
// The number of workers
|
2022-04-25 15:57:47 +08:00
|
|
|
func (pool *Pool) Cap() int {
|
|
|
|
return pool.inner.Cap()
|
|
|
|
}
|
|
|
|
|
2022-04-26 11:33:45 +08:00
|
|
|
// The number of running workers
|
2022-04-25 15:57:47 +08:00
|
|
|
func (pool *Pool) Running() int {
|
|
|
|
return pool.inner.Running()
|
|
|
|
}
|
2022-12-28 10:19:31 +08:00
|
|
|
|
|
|
|
func (pool *Pool) Release() {
|
|
|
|
pool.inner.Release()
|
|
|
|
}
|