mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 00:34:01 +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}")
|
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)
|
APP_DEPS := version.go $(VENDOR_FILES)
|
||||||
|
|
||||||
GOLANGCI_LINT_VERSION := v1.9.1
|
GOLANGCI_LINT_VERSION := v1.9.2
|
||||||
|
|
||||||
COMMON_BUILD_FLAGS := -ldflags="-s -w"
|
COMMON_BUILD_FLAGS := -ldflags="-s -w"
|
||||||
|
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
cancel()
|
||||||
conn.Close() // nolint: errcheck
|
conn.Close() // nolint: errcheck
|
||||||
|
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
@@ -74,6 +75,12 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
defer serverConn.(io.Closer).Close() // nolint: errcheck
|
defer serverConn.(io.Closer).Close() // nolint: errcheck
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
serverConn.(io.Closer).Close()
|
||||||
|
clientConn.(io.Closer).Close()
|
||||||
|
}()
|
||||||
|
|
||||||
wait := &sync.WaitGroup{}
|
wait := &sync.WaitGroup{}
|
||||||
wait.Add(2)
|
wait.Add(2)
|
||||||
|
|
||||||
|
|||||||
+35
-10
@@ -38,6 +38,13 @@ const (
|
|||||||
connTimeoutWrite = 2 * time.Minute
|
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
|
// Conn is a basic wrapper for net.Conn providing the most low-level
|
||||||
// logic and management as possible.
|
// logic and management as possible.
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
@@ -56,11 +63,7 @@ func (c *Conn) Write(p []byte) (int, error) {
|
|||||||
case <-c.ctx.Done():
|
case <-c.ctx.Done():
|
||||||
return 0, errors.Annotate(c.ctx.Err(), "Cannot write because context was closed")
|
return 0, errors.Annotate(c.ctx.Err(), "Cannot write because context was closed")
|
||||||
default:
|
default:
|
||||||
c.conn.SetWriteDeadline(time.Now().Add(connTimeoutWrite)) // nolint: errcheck
|
n, err := c.doIO(c.conn.Write, p, connTimeoutWrite)
|
||||||
n, err := c.conn.Write(p)
|
|
||||||
if err != nil {
|
|
||||||
c.cancel()
|
|
||||||
}
|
|
||||||
|
|
||||||
c.logger.Debugw("Write to stream", "bytes", n, "error", err)
|
c.logger.Debugw("Write to stream", "bytes", n, "error", err)
|
||||||
stats.EgressTraffic(n)
|
stats.EgressTraffic(n)
|
||||||
@@ -74,11 +77,7 @@ func (c *Conn) Read(p []byte) (int, error) {
|
|||||||
case <-c.ctx.Done():
|
case <-c.ctx.Done():
|
||||||
return 0, errors.Annotate(c.ctx.Err(), "Cannot read because context was closed")
|
return 0, errors.Annotate(c.ctx.Err(), "Cannot read because context was closed")
|
||||||
default:
|
default:
|
||||||
c.conn.SetReadDeadline(time.Now().Add(connTimeoutRead)) // nolint: errcheck
|
n, err := c.doIO(c.conn.Read, p, connTimeoutRead)
|
||||||
n, err := c.conn.Read(p)
|
|
||||||
if err != nil {
|
|
||||||
c.cancel()
|
|
||||||
}
|
|
||||||
|
|
||||||
c.logger.Debugw("Read from stream", "bytes", n, "error", err)
|
c.logger.Debugw("Read from stream", "bytes", n, "error", err)
|
||||||
stats.IngressTraffic(n)
|
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.
|
// Close closes underlying net.Conn instance.
|
||||||
func (c *Conn) Close() error {
|
func (c *Conn) Close() error {
|
||||||
defer c.logger.Debugw("Close connection")
|
defer c.logger.Debugw("Close connection")
|
||||||
|
|||||||
Reference in New Issue
Block a user