mirror of https://github.com/prometheus/prometheus
Erdem Agaoglu
8 years ago
2 changed files with 54 additions and 0 deletions
@ -0,0 +1,48 @@
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package netutil provides network utility functions, complementing the more
|
||||
// common ones in the net package.
|
||||
package netutil // import "golang.org/x/net/netutil"
|
||||
|
||||
import ( |
||||
"net" |
||||
"sync" |
||||
) |
||||
|
||||
// LimitListener returns a Listener that accepts at most n simultaneous
|
||||
// connections from the provided Listener.
|
||||
func LimitListener(l net.Listener, n int) net.Listener { |
||||
return &limitListener{l, make(chan struct{}, n)} |
||||
} |
||||
|
||||
type limitListener struct { |
||||
net.Listener |
||||
sem chan struct{} |
||||
} |
||||
|
||||
func (l *limitListener) acquire() { l.sem <- struct{}{} } |
||||
func (l *limitListener) release() { <-l.sem } |
||||
|
||||
func (l *limitListener) Accept() (net.Conn, error) { |
||||
l.acquire() |
||||
c, err := l.Listener.Accept() |
||||
if err != nil { |
||||
l.release() |
||||
return nil, err |
||||
} |
||||
return &limitListenerConn{Conn: c, release: l.release}, nil |
||||
} |
||||
|
||||
type limitListenerConn struct { |
||||
net.Conn |
||||
releaseOnce sync.Once |
||||
release func() |
||||
} |
||||
|
||||
func (l *limitListenerConn) Close() error { |
||||
err := l.Conn.Close() |
||||
l.releaseOnce.Do(l.release) |
||||
return err |
||||
} |
Loading…
Reference in new issue