docs(limiter/conn): add doc comments, fix nil guards and missing Close
Add package and exported symbol doc comments across conn, generator, limiter, and wrapper packages. Fix nil receiver guard in connLimitSingleGenerator.Limiter, add missing httpLoader.Close call, return context.Background instead of nil in serverConn.Context, return explicit error on connection limit exceeded, and simplify Allow logic.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// Package wrapper provides net.Conn and net.Listener wrappers that enforce
|
||||
// connection limits.
|
||||
package wrapper
|
||||
|
||||
import (
|
||||
@@ -21,6 +23,9 @@ type serverConn struct {
|
||||
limiter limiter.Limiter
|
||||
}
|
||||
|
||||
// WrapConn wraps a net.Conn with a connection limiter. On Close, the
|
||||
// limiter's Allow(-1) is called to decrement the current count. If limiter
|
||||
// is nil, the original connection is returned unchanged.
|
||||
func WrapConn(limiter limiter.Limiter, c net.Conn) net.Conn {
|
||||
if limiter == nil {
|
||||
return c
|
||||
@@ -63,5 +68,5 @@ func (c *serverConn) Context() context.Context {
|
||||
if innerCtx, ok := c.Conn.(ctx.Context); ok {
|
||||
return innerCtx.Context()
|
||||
}
|
||||
return nil
|
||||
return context.Background()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package wrapper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
limiter "github.com/go-gost/core/limiter/conn"
|
||||
@@ -11,6 +12,10 @@ type listener struct {
|
||||
limiter limiter.ConnLimiter
|
||||
}
|
||||
|
||||
// WrapListener wraps a net.Listener with a ConnLimiter. Each accepted
|
||||
// connection is checked against the limiter using the remote IP as the key.
|
||||
// If the limit is exceeded, the connection is closed with an error. If
|
||||
// limiter is nil, the original listener is returned unchanged.
|
||||
func WrapListener(limiter limiter.ConnLimiter, ln net.Listener) net.Listener {
|
||||
if limiter == nil {
|
||||
return ln
|
||||
@@ -34,6 +39,7 @@ func (ln *listener) Accept() (net.Conn, error) {
|
||||
return WrapConn(lim, c), nil
|
||||
}
|
||||
c.Close()
|
||||
return nil, errors.New("connection limit exceeded")
|
||||
}
|
||||
|
||||
return c, nil
|
||||
|
||||
Reference in New Issue
Block a user