simplify done api

pull/1086/head
Darien Raymond 2018-04-15 20:40:47 +02:00
parent 9d8922f523
commit d6dc88860b
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
6 changed files with 11 additions and 16 deletions

View File

@ -19,7 +19,7 @@ type OutboundListener struct {
func (l *OutboundListener) add(conn net.Conn) {
select {
case l.buffer <- conn:
case <-l.done.C():
case <-l.done.Wait():
common.Ignore(conn.Close(), "We can do nothing if Close() returns error.")
default:
common.Ignore(conn.Close(), "We can do nothing if Close() returns error.")
@ -29,7 +29,7 @@ func (l *OutboundListener) add(conn net.Conn) {
// Accept implements net.Listener.
func (l *OutboundListener) Accept() (net.Conn, error) {
select {
case <-l.done.C():
case <-l.done.Wait():
return nil, newError("listen closed")
case c := <-l.buffer:
return c, nil

View File

@ -134,7 +134,7 @@ func (c *udpConn) Read(buf []byte) (int, error) {
c.uplink.Add(int64(nBytes))
}
return nBytes, nil
case <-c.done.C():
case <-c.done.Wait():
return 0, io.EOF
}
}
@ -239,7 +239,7 @@ func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest
conn, existing := w.getConnection(id)
select {
case conn.input <- b:
case <-conn.done.C():
case <-conn.done.Wait():
b.Release()
default:
b.Release()
@ -308,7 +308,7 @@ func (w *udpWorker) monitor() {
for {
select {
case <-w.done.C():
case <-w.done.Wait():
return
case <-timer.C:
nowSec := time.Now().Unix()

View File

@ -125,7 +125,7 @@ func (m *Client) monitor() {
for {
select {
case <-m.done.C():
case <-m.done.Wait():
m.sessionManager.Close()
m.inboundRay.InboundInput().Close()
m.inboundRay.InboundOutput().CloseError()

View File

@ -51,7 +51,7 @@ func (l *generalLogger) run() {
for {
select {
case <-l.done.C():
case <-l.done.Wait():
return
case msg := <-l.buffer:
logger.Write(msg.String() + platform.LineSeparator())

View File

@ -21,23 +21,18 @@ func NewDone() *Done {
// Done returns true if Close() is called.
func (d *Done) Done() bool {
select {
case <-d.c:
case <-d.Wait():
return true
default:
return false
}
}
// C returns a channel for waiting for done.
func (d *Done) C() chan struct{} {
// Wait returns a channel for waiting for done.
func (d *Done) Wait() <-chan struct{} {
return d.c
}
// Wait blocks until Close() is called.
func (d *Done) Wait() {
<-d.c
}
// Close marks this Done 'done'. This method may be called multiple times. All calls after first call will have no effect on its status.
func (d *Done) Close() error {
d.access.Lock()

View File

@ -71,7 +71,7 @@ func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request)
Local: l.Addr(),
Remote: l.Addr(),
})
<-done.C()
<-done.Wait()
}
func Listen(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) {