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" "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 << 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, 1 << 10, 2 << 10, 4 << 10, 8 << 10, 16 << 10, 32 << 10, 64 << 10,
} }
var bufPools = [...]sync.Pool{ var bufPools = initPools(bufSizes)
{New: func() interface{} { return make([]byte, 1<<0) }},
{New: func() interface{} { return make([]byte, 1<<1) }}, func initPools(sizes []int) []sync.Pool {
{New: func() interface{} { return make([]byte, 1<<2) }}, pools := make([]sync.Pool, len(sizes))
{New: func() interface{} { return make([]byte, 1<<3) }}, for k := range pools {
{New: func() interface{} { return make([]byte, 1<<4) }}, pools[k].New = func() interface{} {
{New: func() interface{} { return make([]byte, 1<<5) }}, return make([]byte, sizes[k])
{New: func() interface{} { return make([]byte, 1<<6) }}, }
{New: func() interface{} { return make([]byte, 1<<7) }}, }
{New: func() interface{} { return make([]byte, 1<<8) }}, return pools
{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) }},
} }
func GetBuffer(size int) []byte { 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.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.CheckInterval, "checkinterval", 30, "proxy check interval(seconds)")
flag.IntVar(&conf.StrategyConfig.CheckTimeout, "checktimeout", 10, "proxy check timeout(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.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") flag.StringVar(&conf.StrategyConfig.IntFace, "interface", "", "source ip or source interface")

View File

@ -158,9 +158,12 @@ checkwebsite=www.apple.com
# check interval(seconds) # check interval(seconds)
checkinterval=30 checkinterval=30
# check timeout(seconds) # timeout to set a forwarder to be disabled(seconds)
checktimeout=10 checktimeout=10
# check disabled fowarders only
checkdisabledonly=false
# DNS FORWARDING SERVER # DNS FORWARDING SERVER
# ---------------- # ----------------
# we can specify different upstream dns server in rule file for different destinations # 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 { func generateClientKey() string {
p := make([]byte, 16) p := pool.GetBuffer(16)
defer pool.PutBuffer(p)
rand.Read(p) rand.Read(p)
return base64.StdEncoding.EncodeToString(p) return base64.StdEncoding.EncodeToString(p)
} }

View File

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