github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/perf/gopool/config.go (about) 1 // Copyright 2021 ByteDance Inc. 2 // Copyright 2023 Shawn Wang <jxskiss@126.com>. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package gopool 17 18 import ( 19 "context" 20 "log" 21 22 "github.com/jxskiss/gopkg/v2/internal" 23 ) 24 25 func defaultPanicHandler(_ context.Context, exc any) { 26 location, frames := internal.IdentifyPanic(1) 27 log.Printf("[ERROR] gopool: catch panic: %v\nlocation: %v\n%s\n", exc, location, internal.FormatFrames(frames)) 28 } 29 30 // Config is used to config a Pool instance. 31 type Config struct { 32 33 // Name optionally specifies the name of a pool instance. 34 Name string 35 36 // New goroutine will be created if len(queuedTasks) >= ScaleThreshold, 37 // it defaults to 1, which means always start a new adhoc worker before 38 // reaching the limit of total adhoc worker number. 39 ScaleThreshold int 40 41 // PermanentWorkerNum specifies the number of permanent workers to spawn 42 // when creating a Pool, it defaults to 0 (no permanent worker). 43 // Note that a permanent worker's goroutine stack is reused, 44 // the memory won't be freed in the entire program lifetime. 45 // 46 // Generally you may want to set this to zero for common workloads, 47 // tweak it for special workloads which benefits from reusing goroutine stacks. 48 PermanentWorkerNum int 49 50 // AdhocWorkerLimit specifies the initial limit of adhoc workers, 51 // 0 or negative value means no limit. 52 // 53 // The limit of adhoc worker number can be changed by calling 54 // SetAdhocWorkerLimit. 55 AdhocWorkerLimit int 56 57 // PanicHandler specifies a handler when panic occurs. 58 // By default, a panic message with stack information is logged. 59 PanicHandler func(context.Context, any) 60 } 61 62 // NewConfig creates a default Config. 63 func NewConfig() *Config { 64 c := &Config{ 65 ScaleThreshold: 1, 66 } 67 c.checkAndSetDefaults() 68 return c 69 } 70 71 func (c *Config) checkAndSetDefaults() { 72 if c.PanicHandler == nil { 73 c.PanicHandler = defaultPanicHandler 74 } 75 }