Browse Source

simplify done api

Darien Raymond 7 năm trước cách đây
mục cha
commit
d6dc88860b

+ 2 - 2
app/commander/outbound.go

@@ -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

+ 3 - 3
app/proxyman/inbound/worker.go

@@ -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()

+ 1 - 1
app/proxyman/mux/mux.go

@@ -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()

+ 1 - 1
common/log/logger.go

@@ -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())

+ 3 - 8
common/signal/done.go

@@ -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()

+ 1 - 1
transport/internet/http/hub.go

@@ -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) {