mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 19:04:02 +03:00
Tune socket options
This commit is contained in:
+7
-20
@@ -8,29 +8,23 @@ import (
|
||||
"github.com/juju/errors"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
const telegramKeepAlive = 30 * time.Second
|
||||
const (
|
||||
telegramDialTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
type tgDialer struct {
|
||||
net.Dialer
|
||||
|
||||
conf *config.Config
|
||||
}
|
||||
|
||||
func (t *tgDialer) dial(addr string) (net.Conn, error) {
|
||||
connRaw, err := t.Dialer.Dial("tcp", addr)
|
||||
conn, err := t.Dialer.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot connect to Telegram")
|
||||
}
|
||||
conn := connRaw.(*net.TCPConn)
|
||||
|
||||
if err = conn.SetKeepAlive(true); err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot establish keepalive connection")
|
||||
}
|
||||
if err = conn.SetKeepAlivePeriod(telegramKeepAlive); err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot set keepalive timeout")
|
||||
if err = config.SetSocketOptions(conn); err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot set socket options")
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
@@ -42,12 +36,5 @@ func (t *tgDialer) dialRWC(addr string) (io.ReadWriteCloser, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return wrappers.NewTimeoutRWC(conn, t.conf.TimeoutRead, t.conf.TimeoutWrite), nil
|
||||
}
|
||||
|
||||
func newDialer(conf *config.Config) tgDialer {
|
||||
return tgDialer{
|
||||
Dialer: net.Dialer{Timeout: conf.TimeoutRead},
|
||||
conf: conf,
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
+2
-1
@@ -2,6 +2,7 @@ package telegram
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
@@ -58,7 +59,7 @@ func (t *directTelegram) Init(connOpts *mtproto.ConnectionOpts, conn io.ReadWrit
|
||||
// to Telegram bypassing middleproxies.
|
||||
func NewDirectTelegram(conf *config.Config) Telegram {
|
||||
return &directTelegram{baseTelegram{
|
||||
dialer: newDialer(conf),
|
||||
dialer: tgDialer{net.Dialer{Timeout: telegramDialTimeout}},
|
||||
v4Addresses: directV4Addresses,
|
||||
v6Addresses: directV6Addresses,
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user