1
// Copyright 2013 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// Package netutil provides network utility functions, complementing the more
6
// common ones in the net package.
14
// LimitListener returns a Listener that accepts at most n simultaneous
15
// connections from the provided Listener.
16
func LimitListener(l net.Listener, n int) net.Listener {
17
ch := make(chan struct{}, n)
18
for i := 0; i < n; i++ {
21
return &limitListener{l, ch}
24
type limitListener struct {
29
func (l *limitListener) Accept() (net.Conn, error) {
31
c, err := l.Listener.Accept()
35
return &limitListenerConn{Conn: c, ch: l.ch}, nil
38
type limitListenerConn struct {
44
func (l *limitListenerConn) Close() error {