Skip to content

Commit

Permalink
🚮 Drop TCP timeout and unnecessary SetKeepAlive (#29)
Browse files Browse the repository at this point in the history
I checked Go's source code. Both the TCP listener and dialer sets a 15s keepalive by default. So it's unnecessary to set it in mmp-go. And since we already have TCP keepalive, the timeout is totally unnecessary, and it breaks long connections even if the connection is actively transmitting data.
  • Loading branch information
database64128 authored Sep 16, 2021
1 parent 51a94d2 commit 70739bc
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions dispatcher/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package tcp
import (
"errors"
"fmt"
"github.com/Qv2ray/mmp-go/cipher"
"github.com/Qv2ray/mmp-go/config"
"github.com/Qv2ray/mmp-go/dispatcher"
"github.com/Qv2ray/mmp-go/infra/pool"
"io"
"log"
"net"
"sync"
"time"

"github.com/Qv2ray/mmp-go/cipher"
"github.com/Qv2ray/mmp-go/config"
"github.com/Qv2ray/mmp-go/dispatcher"
"github.com/Qv2ray/mmp-go/infra/pool"
)

//[salt][encrypted payload length][length tag][encrypted payload][payload tag]
const (
DefaultTimeout = 7440 * time.Second
BasicLen = 32 + 2 + 16
BasicLen = 32 + 2 + 16
)

func init() {
Expand Down Expand Up @@ -79,7 +79,6 @@ func (d *TCP) handleConn(conn net.Conn) error {
userContext *config.UserContext
)
defer conn.Close()
_ = conn.(*net.TCPConn).SetKeepAlive(true)

var data = pool.Get(BasicLen)
defer pool.Put(data)
Expand Down Expand Up @@ -110,9 +109,7 @@ func (d *TCP) handleConn(conn net.Conn) error {
if err != nil {
return fmt.Errorf("[tcp] %s <-> %s <-x-> %s handleConn dial error: %w", conn.RemoteAddr(), conn.LocalAddr(), server.Target, err)
}
_ = rc.(*net.TCPConn).SetKeepAlive(true)

_ = rc.SetDeadline(time.Now().Add(DefaultTimeout))
_, err = rc.Write(data[:n])
if err != nil {
return fmt.Errorf("[tcp] %s <-> %s <-x-> %s handleConn write error: %w", conn.RemoteAddr(), conn.LocalAddr(), server.Target, err)
Expand Down

0 comments on commit 70739bc

Please sign in to comment.