mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 17:14:02 +03:00
REPOSITORY / ScuroNeko/mtg
Compare commits
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8268620615 | ||
|
|
753639beb8 | ||
|
|
1ee34fd502 |
@@ -6,7 +6,7 @@ VENDOR_FILES := $(shell find "$(ROOT_DIR)/vendor" 2>/dev/null || echo -n "vendor
|
||||
CC_BINARIES := $(shell bash -c "echo -n $(APP_NAME)-{linux,freebsd,openbsd}-{386,amd64} $(APP_NAME)-linux-{arm,arm64}")
|
||||
APP_DEPS := version.go $(VENDOR_FILES)
|
||||
|
||||
GOLANGCI_LINT_VERSION := v1.9.1
|
||||
GOLANGCI_LINT_VERSION := v1.9.2
|
||||
|
||||
COMMON_BUILD_FLAGS := -ldflags="-s -w"
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ func (p *Proxy) accept(conn net.Conn) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
defer func() {
|
||||
cancel()
|
||||
conn.Close() // nolint: errcheck
|
||||
|
||||
if err := recover(); err != nil {
|
||||
@@ -74,6 +75,12 @@ func (p *Proxy) accept(conn net.Conn) {
|
||||
}
|
||||
defer serverConn.(io.Closer).Close() // nolint: errcheck
|
||||
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
serverConn.(io.Closer).Close()
|
||||
clientConn.(io.Closer).Close()
|
||||
}()
|
||||
|
||||
wait := &sync.WaitGroup{}
|
||||
wait.Add(2)
|
||||
|
||||
|
||||
+35
-10
@@ -38,6 +38,13 @@ const (
|
||||
connTimeoutWrite = 2 * time.Minute
|
||||
)
|
||||
|
||||
type ioResult struct {
|
||||
n int
|
||||
err error
|
||||
}
|
||||
|
||||
type ioFunc func([]byte) (int, error)
|
||||
|
||||
// Conn is a basic wrapper for net.Conn providing the most low-level
|
||||
// logic and management as possible.
|
||||
type Conn struct {
|
||||
@@ -56,11 +63,7 @@ func (c *Conn) Write(p []byte) (int, error) {
|
||||
case <-c.ctx.Done():
|
||||
return 0, errors.Annotate(c.ctx.Err(), "Cannot write because context was closed")
|
||||
default:
|
||||
c.conn.SetWriteDeadline(time.Now().Add(connTimeoutWrite)) // nolint: errcheck
|
||||
n, err := c.conn.Write(p)
|
||||
if err != nil {
|
||||
c.cancel()
|
||||
}
|
||||
n, err := c.doIO(c.conn.Write, p, connTimeoutWrite)
|
||||
|
||||
c.logger.Debugw("Write to stream", "bytes", n, "error", err)
|
||||
stats.EgressTraffic(n)
|
||||
@@ -74,11 +77,7 @@ func (c *Conn) Read(p []byte) (int, error) {
|
||||
case <-c.ctx.Done():
|
||||
return 0, errors.Annotate(c.ctx.Err(), "Cannot read because context was closed")
|
||||
default:
|
||||
c.conn.SetReadDeadline(time.Now().Add(connTimeoutRead)) // nolint: errcheck
|
||||
n, err := c.conn.Read(p)
|
||||
if err != nil {
|
||||
c.cancel()
|
||||
}
|
||||
n, err := c.doIO(c.conn.Read, p, connTimeoutRead)
|
||||
|
||||
c.logger.Debugw("Read from stream", "bytes", n, "error", err)
|
||||
stats.IngressTraffic(n)
|
||||
@@ -87,6 +86,32 @@ func (c *Conn) Read(p []byte) (int, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Conn) doIO(callback ioFunc, p []byte, timeout time.Duration) (int, error) {
|
||||
resChan := make(chan ioResult, 1)
|
||||
timer := time.NewTimer(timeout)
|
||||
|
||||
go func() {
|
||||
n, err := callback(p)
|
||||
resChan <- ioResult{n: n, err: err}
|
||||
}()
|
||||
|
||||
select {
|
||||
case res := <-resChan:
|
||||
timer.Stop()
|
||||
if res.err != nil {
|
||||
c.Close()
|
||||
}
|
||||
return res.n, res.err
|
||||
case <-c.ctx.Done():
|
||||
timer.Stop()
|
||||
c.Close()
|
||||
return 0, errors.Annotate(c.ctx.Err(), "Cannot do IO because context is closed")
|
||||
case <-timer.C:
|
||||
c.Close()
|
||||
return 0, errors.Annotate(c.ctx.Err(), "Timeout on IO operation")
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes underlying net.Conn instance.
|
||||
func (c *Conn) Close() error {
|
||||
defer c.logger.Debugw("Close connection")
|
||||
|
||||
Reference in New Issue
Block a user