storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/http/dial_linux.go (about) 1 //go:build linux 2 // +build linux 3 4 /* 5 * MinIO Cloud Storage, (C) 2020 MinIO, Inc. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package http 21 22 import ( 23 "context" 24 "net" 25 "syscall" 26 "time" 27 28 "golang.org/x/sys/unix" 29 ) 30 31 func setInternalTCPParameters(c syscall.RawConn) error { 32 return c.Control(func(fdPtr uintptr) { 33 // got socket file descriptor to set parameters. 34 fd := int(fdPtr) 35 36 // Enable TCP fast connect 37 // TCPFastOpenConnect sets the underlying socket to use 38 // the TCP fast open connect. This feature is supported 39 // since Linux 4.11. 40 _ = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, unix.TCP_FASTOPEN_CONNECT, 1) 41 42 // Enable TCP quick ACK, John Nagle says 43 // "Set TCP_QUICKACK. If you find a case where that makes things worse, let me know." 44 _ = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, unix.TCP_QUICKACK, 1) 45 46 // The time (in seconds) the connection needs to remain idle before 47 // TCP starts sending keepalive probes, set this to 5 secs 48 // system defaults to 7200 secs!!! 49 _ = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, 5) 50 51 // Number of probes. 52 // ~ cat /proc/sys/net/ipv4/tcp_keepalive_probes (defaults to 9, we reduce it to 5) 53 // 9 54 _ = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_KEEPCNT, 5) 55 56 // Wait time after successful probe in seconds. 57 // ~ cat /proc/sys/net/ipv4/tcp_keepalive_intvl (defaults to 75 secs, we reduce it to 3 secs) 58 // 75 59 _ = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, 3) 60 61 }) 62 } 63 64 // DialContext is a function to make custom Dial for internode communications 65 type DialContext func(ctx context.Context, network, address string) (net.Conn, error) 66 67 // NewInternodeDialContext setups a custom dialer for internode communication 68 func NewInternodeDialContext(dialTimeout time.Duration) DialContext { 69 return func(ctx context.Context, network, addr string) (net.Conn, error) { 70 dialer := &net.Dialer{ 71 Timeout: dialTimeout, 72 Control: func(network, address string, c syscall.RawConn) error { 73 return setInternalTCPParameters(c) 74 }, 75 } 76 return dialer.DialContext(ctx, network, addr) 77 } 78 } 79 80 // NewCustomDialContext setups a custom dialer for any external communication and proxies. 81 func NewCustomDialContext(dialTimeout time.Duration) DialContext { 82 return func(ctx context.Context, network, addr string) (net.Conn, error) { 83 dialer := &net.Dialer{ 84 Timeout: dialTimeout, 85 Control: func(network, address string, c syscall.RawConn) error { 86 return c.Control(func(fdPtr uintptr) { 87 // got socket file descriptor to set parameters. 88 fd := int(fdPtr) 89 90 // Enable TCP fast connect 91 // TCPFastOpenConnect sets the underlying socket to use 92 // the TCP fast open connect. This feature is supported 93 // since Linux 4.11. 94 _ = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, unix.TCP_FASTOPEN_CONNECT, 1) 95 96 // Enable TCP quick ACK, John Nagle says 97 // "Set TCP_QUICKACK. If you find a case where that makes things worse, let me know." 98 _ = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, unix.TCP_QUICKACK, 1) 99 }) 100 }, 101 } 102 return dialer.DialContext(ctx, network, addr) 103 } 104 }