Add timeoutreadwritecloser

This commit is contained in:
9seconds
2018-05-31 09:54:20 +03:00
parent 06a10e18c1
commit 83b46d1b80
4 changed files with 76 additions and 21 deletions
+4 -3
View File
@@ -17,13 +17,14 @@ var telegramDCIPs = [5]string{
const telegramKeepAlive = 30 * time.Second
func dialToTelegram(dcIdx int16) (net.Conn, error) {
func dialToTelegram(dcIdx int16, timeout time.Duration) (net.Conn, error) {
if dcIdx < 0 || dcIdx >= 5 {
return nil, errors.New("Incorrect DC IDX")
}
tcpAddr, _ := net.ResolveTCPAddr("tcp", telegramDCIPs[dcIdx])
conn, err := net.DialTCP("tcp", nil, tcpAddr)
dialer := net.Dialer{Timeout: timeout}
rawConn, err := dialer.Dial("tcp", telegramDCIPs[dcIdx])
conn := rawConn.(*net.TCPConn)
if err != nil {
return nil, errors.Annotate(err, "Cannot dial")
}