diff --git a/common/pool/buffer.go b/common/pool/buffer.go index 999f356..ba0bd60 100644 --- a/common/pool/buffer.go +++ b/common/pool/buffer.go @@ -21,6 +21,7 @@ func initPools(sizes []int) []sync.Pool { return pools } +// GetBuffer returns a buffer from pool. func GetBuffer(size int) []byte { i := 0 for ; i < len(bufSizes)-1; i++ { @@ -31,6 +32,7 @@ func GetBuffer(size int) []byte { return bufPools[i].Get().([]byte)[:size] } +// PutBuffer puts a buffer into pool. func PutBuffer(p []byte) { l := len(p) for i, n := range bufSizes { diff --git a/common/pool/writebuf.go b/common/pool/writebuf.go index 93f79ae..8ba4289 100644 --- a/common/pool/writebuf.go +++ b/common/pool/writebuf.go @@ -9,10 +9,12 @@ var writeBufPool = sync.Pool{ New: func() interface{} { return &bytes.Buffer{} }, } +// GetWriteBuffer returns a bytes.buffer from pool. func GetWriteBuffer() *bytes.Buffer { return writeBufPool.Get().(*bytes.Buffer) } +// PutWriteBuffer puts a bytes.buffer into pool. func PutWriteBuffer(buf *bytes.Buffer) { if buf.Cap() > 64<<10 { return diff --git a/strategy/forward.go b/strategy/forward.go index c70f524..f732cff 100644 --- a/strategy/forward.go +++ b/strategy/forward.go @@ -114,10 +114,10 @@ func (f *Forwarder) Failures() uint32 { // IncFailures increase the failuer count by 1 func (f *Forwarder) IncFailures() { failures := atomic.AddUint32(&f.failures, 1) - log.F("[forwarder] %s recorded %d failures", f.addr, failures) + log.F("[forwarder] %s recorded %d failures, maxfailures: %d", f.addr, failures, f.MaxFailures()) - if failures >= f.MaxFailures() && f.Enabled() { - log.F("[forwarder] %s reaches maxfailures.", f.addr) + if f.MaxFailures() != 0 && failures >= f.MaxFailures() && f.Enabled() { + log.F("[forwarder] %s reaches maxfailures %d", f.addr, f.MaxFailures()) f.Disable() } } diff --git a/strategy/strategy.go b/strategy/strategy.go index 4715138..68a8914 100644 --- a/strategy/strategy.go +++ b/strategy/strategy.go @@ -130,12 +130,13 @@ func (p *Proxy) Record(dialer proxy.Dialer, success bool) { OnRecord(dialer, success) } +// OnRecord records result while using the dialer from proxy. func OnRecord(dialer proxy.Dialer, success bool) { if fwdr, ok := dialer.(*Forwarder); ok { - if success { - fwdr.Enable() - } else { + if !success { fwdr.IncFailures() + } else { + fwdr.Enable() } } }