fix panic for channel close
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
@@ -77,6 +78,7 @@ type serverConn struct {
|
||||
w ResponseWriter
|
||||
laddr net.Addr
|
||||
closed chan struct{}
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (c *serverConn) Read(b []byte) (n int, err error) {
|
||||
@@ -100,6 +102,9 @@ func (c *serverConn) Write(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (c *serverConn) Close() error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
select {
|
||||
case <-c.closed:
|
||||
default:
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/logger"
|
||||
@@ -88,6 +89,7 @@ type conn struct {
|
||||
remoteAddr net.Addr
|
||||
closed chan struct{}
|
||||
ctx context.Context
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
@@ -135,6 +137,9 @@ func (c *conn) Write(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (c *conn) Close() error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
select {
|
||||
case <-c.closed:
|
||||
default:
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -14,6 +15,7 @@ type conn struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
closed chan struct{}
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
@@ -29,6 +31,9 @@ func (c *conn) Close() error {
|
||||
c.cancel()
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
select {
|
||||
case <-c.closed:
|
||||
default:
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -17,6 +18,7 @@ type conn struct {
|
||||
localAddr net.Addr
|
||||
closed chan struct{}
|
||||
ctx context.Context
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
@@ -28,12 +30,17 @@ func (c *conn) Write(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (c *conn) Close() (err error) {
|
||||
c.mu.Lock()
|
||||
|
||||
select {
|
||||
case <-c.closed:
|
||||
c.mu.Unlock()
|
||||
return
|
||||
default:
|
||||
close(c.closed)
|
||||
}
|
||||
c.mu.Unlock()
|
||||
|
||||
if rc, ok := c.r.(io.Closer); ok {
|
||||
err = rc.Close()
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -13,6 +14,7 @@ type conn struct {
|
||||
raddr net.Addr
|
||||
closed chan struct{}
|
||||
ctx context.Context
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
@@ -24,6 +26,9 @@ func (c *conn) Write(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (c *conn) Close() error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
select {
|
||||
case <-c.closed:
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user