pool: added a function to init bufPools

This commit is contained in:
nadoo 2020-04-21 12:17:14 +08:00
parent 0b0611a0dc
commit b74880ae7a
5 changed files with 26 additions and 30 deletions

View File

@ -4,29 +4,21 @@ import (
"sync"
)
var bufSizes = [...]int{
var bufSizes = []int{
1 << 0, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7, 1 << 8, 1 << 9,
1 << 10, 2 << 10, 4 << 10, 8 << 10, 16 << 10, 32 << 10, 64 << 10,
}
var bufPools = [...]sync.Pool{
{New: func() interface{} { return make([]byte, 1<<0) }},
{New: func() interface{} { return make([]byte, 1<<1) }},
{New: func() interface{} { return make([]byte, 1<<2) }},
{New: func() interface{} { return make([]byte, 1<<3) }},
{New: func() interface{} { return make([]byte, 1<<4) }},
{New: func() interface{} { return make([]byte, 1<<5) }},
{New: func() interface{} { return make([]byte, 1<<6) }},
{New: func() interface{} { return make([]byte, 1<<7) }},
{New: func() interface{} { return make([]byte, 1<<8) }},
{New: func() interface{} { return make([]byte, 1<<9) }},
{New: func() interface{} { return make([]byte, 1<<10) }},
{New: func() interface{} { return make([]byte, 2<<10) }},
{New: func() interface{} { return make([]byte, 4<<10) }},
{New: func() interface{} { return make([]byte, 8<<10) }},
{New: func() interface{} { return make([]byte, 16<<10) }},
{New: func() interface{} { return make([]byte, 32<<10) }},
{New: func() interface{} { return make([]byte, 64<<10) }},
var bufPools = initPools(bufSizes)
func initPools(sizes []int) []sync.Pool {
pools := make([]sync.Pool, len(sizes))
for k := range pools {
pools[k].New = func() interface{} {
return make([]byte, sizes[k])
}
}
return pools
}
func GetBuffer(size int) []byte {

View File

@ -43,7 +43,7 @@ func confInit() {
flag.StringVar(&conf.StrategyConfig.CheckWebSite, "checkwebsite", "www.apple.com", "proxy check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80")
flag.IntVar(&conf.StrategyConfig.CheckInterval, "checkinterval", 30, "proxy check interval(seconds)")
flag.IntVar(&conf.StrategyConfig.CheckTimeout, "checktimeout", 10, "proxy check timeout(seconds)")
flag.BoolVar(&conf.StrategyConfig.CheckFailedOnly, "checkfailedonly", false, "check failed fowarder only")
flag.BoolVar(&conf.StrategyConfig.CheckDisabledOnly, "checkdisabledonly", false, "check disabled fowarders only")
flag.IntVar(&conf.StrategyConfig.MaxFailures, "maxfailures", 3, "max failures to change forwarder status to disabled")
flag.StringVar(&conf.StrategyConfig.IntFace, "interface", "", "source ip or source interface")

View File

@ -158,9 +158,12 @@ checkwebsite=www.apple.com
# check interval(seconds)
checkinterval=30
# check timeout(seconds)
# timeout to set a forwarder to be disabled(seconds)
checktimeout=10
# check disabled fowarders only
checkdisabledonly=false
# DNS FORWARDING SERVER
# ----------------
# we can specify different upstream dns server in rule file for different destinations

View File

@ -118,7 +118,8 @@ func parseFirstLine(line string) (r1, r2, r3 string, ok bool) {
}
func generateClientKey() string {
p := make([]byte, 16)
p := pool.GetBuffer(16)
defer pool.PutBuffer(p)
rand.Read(p)
return base64.StdEncoding.EncodeToString(p)
}

View File

@ -21,7 +21,7 @@ type Config struct {
CheckWebSite string
CheckInterval int
CheckTimeout int
CheckFailedOnly bool
CheckDisabledOnly bool
MaxFailures int
IntFace string
}
@ -204,7 +204,7 @@ func (p *Proxy) check(f *Forwarder) {
continue
}
if f.Enabled() && p.config.CheckFailedOnly {
if f.Enabled() && p.config.CheckDisabledOnly {
continue
}