2014-02-16 04:02:54 +00:00
|
|
|
/*
|
|
|
|
Package to allow go applications to immediately start
|
|
|
|
listening on a socket, unix, tcp, udp but hold connections
|
|
|
|
until the application has booted and is ready to accept them
|
|
|
|
*/
|
2014-02-16 05:10:37 +00:00
|
|
|
package listenbuffer
|
2014-02-16 04:02:54 +00:00
|
|
|
|
2014-03-17 08:40:36 +00:00
|
|
|
import "net"
|
2014-02-16 04:02:54 +00:00
|
|
|
|
2014-03-17 08:40:36 +00:00
|
|
|
// NewListenBuffer returns a listener listening on addr with the protocol.
|
|
|
|
func NewListenBuffer(proto, addr string, activate chan struct{}) (net.Listener, error) {
|
2014-02-16 04:02:54 +00:00
|
|
|
wrapped, err := net.Listen(proto, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &defaultListener{
|
|
|
|
wrapped: wrapped,
|
|
|
|
activate: activate,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type defaultListener struct {
|
|
|
|
wrapped net.Listener // the real listener to wrap
|
|
|
|
ready bool // is the listner ready to start accpeting connections
|
|
|
|
activate chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *defaultListener) Close() error {
|
|
|
|
return l.wrapped.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *defaultListener) Addr() net.Addr {
|
|
|
|
return l.wrapped.Addr()
|
|
|
|
}
|
|
|
|
|
|
|
|
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 08:40:36 +00:00
|
|
|
<-l.activate
|
|
|
|
l.ready = true
|
|
|
|
return l.Accept()
|
2014-02-16 04:02:54 +00:00
|
|
|
}
|