From b74880ae7af452554824d0deb373f6d9bad441d5 Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Tue, 21 Apr 2020 12:17:14 +0800 Subject: [PATCH] pool: added a function to init bufPools --- common/pool/buffer.go | 30 +++++++++++------------------- conf.go | 2 +- config/glider.conf.example | 5 ++++- proxy/ws/client.go | 3 ++- strategy/strategy.go | 16 ++++++++-------- 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/common/pool/buffer.go b/common/pool/buffer.go index d224e8b..999f356 100644 --- a/common/pool/buffer.go +++ b/common/pool/buffer.go @@ -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 { diff --git a/conf.go b/conf.go index d013795..87914c3 100644 --- a/conf.go +++ b/conf.go @@ -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") diff --git a/config/glider.conf.example b/config/glider.conf.example index 9d9dfb5..e84f0df 100644 --- a/config/glider.conf.example +++ b/config/glider.conf.example @@ -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 diff --git a/proxy/ws/client.go b/proxy/ws/client.go index d4c369d..cdb5013 100644 --- a/proxy/ws/client.go +++ b/proxy/ws/client.go @@ -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) } diff --git a/strategy/strategy.go b/strategy/strategy.go index dceea12..3ed71ba 100644 --- a/strategy/strategy.go +++ b/strategy/strategy.go @@ -17,13 +17,13 @@ import ( // Config is strategy config struct. type Config struct { - Strategy string - CheckWebSite string - CheckInterval int - CheckTimeout int - CheckFailedOnly bool - MaxFailures int - IntFace string + Strategy string + CheckWebSite string + CheckInterval int + CheckTimeout int + CheckDisabledOnly bool + MaxFailures int + IntFace string } // forwarder slice orderd by priority @@ -204,7 +204,7 @@ func (p *Proxy) check(f *Forwarder) { continue } - if f.Enabled() && p.config.CheckFailedOnly { + if f.Enabled() && p.config.CheckDisabledOnly { continue }