2014-02-15 23:02:54 -05:00
|
|
|
/*
|
2015-03-24 23:09:36 -04:00
|
|
|
listenbuffer uses the kernel's listening backlog functionality to queue
|
|
|
|
connections, allowing applications to start listening immediately and handle
|
|
|
|
connections later. This is signaled by closing the activation channel passed to
|
|
|
|
the constructor.
|
|
|
|
|
|
|
|
The maximum amount of queued connections depends on the configuration of your
|
|
|
|
kernel (typically called SOMAXXCON) and cannot be configured in Go with the
|
|
|
|
net package. See `src/net/sock_platform.go` in the Go tree or consult your
|
|
|
|
kernel's manual.
|
|
|
|
|
|
|
|
activator := make(chan struct{})
|
|
|
|
buffer, err := NewListenBuffer("tcp", "localhost:4000", activator)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// will block until activator has been closed or is sent an event
|
|
|
|
client, err := buffer.Accept()
|
|
|
|
|
|
|
|
Somewhere else in your application once it's been booted:
|
|
|
|
|
|
|
|
close(activator)
|
|
|
|
|
|
|
|
`buffer.Accept()` will return the first client in the kernel listening queue, or
|
|
|
|
continue to block until a client connects or an error occurs.
|
2014-02-15 23:02:54 -05:00
|
|
|
*/
|
2014-02-16 00:10:37 -05:00
|
|
|
package listenbuffer
|
2014-02-15 23:02:54 -05:00
|
|
|
|
2014-03-17 04:40:36 -04:00
|
|
|
import "net"
|
2014-02-15 23:02:54 -05:00
|
|
|
|
2015-03-24 23:09:36 -04:00
|
|
|
// NewListenBuffer returns a net.Listener listening on addr with the protocol
|
|
|
|
// passed. The channel passed is used to activate the listenbuffer when the
|
|
|
|
// caller is ready to accept connections.
|
2014-03-17 04:40:36 -04:00
|
|
|
func NewListenBuffer(proto, addr string, activate chan struct{}) (net.Listener, error) {
|
2014-02-15 23:02:54 -05:00
|
|
|
wrapped, err := net.Listen(proto, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &defaultListener{
|
|
|
|
wrapped: wrapped,
|
|
|
|
activate: activate,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:09:36 -04:00
|
|
|
// defaultListener is the buffered wrapper around the net.Listener
|
2014-02-15 23:02:54 -05:00
|
|
|
type defaultListener struct {
|
2015-03-24 23:09:36 -04:00
|
|
|
wrapped net.Listener // The net.Listener wrapped by listenbuffer
|
|
|
|
ready bool // Whether the listenbuffer has been activated
|
|
|
|
activate chan struct{} // Channel to control activation of the listenbuffer
|
2014-02-15 23:02:54 -05:00
|
|
|
}
|
|
|
|
|
2015-03-24 23:09:36 -04:00
|
|
|
// Close closes the wrapped socket.
|
2014-02-15 23:02:54 -05:00
|
|
|
func (l *defaultListener) Close() error {
|
|
|
|
return l.wrapped.Close()
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:09:36 -04:00
|
|
|
// Addr returns the listening address of the wrapped socket.
|
2014-02-15 23:02:54 -05:00
|
|
|
func (l *defaultListener) Addr() net.Addr {
|
|
|
|
return l.wrapped.Addr()
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:09:36 -04:00
|
|
|
// Accept returns a client connection on the wrapped socket if the listen buffer
|
|
|
|
// has been activated. To active the listenbuffer the activation channel passed
|
|
|
|
// to NewListenBuffer must have been closed or sent an event.
|
2014-02-15 23:02:54 -05:00
|
|
|
func (l *defaultListener) Accept() (net.Conn, error) {
|
|
|
|
// if the listen has been told it is ready then we can go ahead and
|
|
|
|
// start returning connections
|
|
|
|
if l.ready {
|
|
|
|
return l.wrapped.Accept()
|
|
|
|
}
|
2014-03-17 04:40:36 -04:00
|
|
|
<-l.activate
|
|
|
|
l.ready = true
|
|
|
|
return l.Accept()
|
2014-02-15 23:02:54 -05:00
|
|
|
}
|