@@ -8,6 +8,19 @@ import (
8
8
"fmt"
9
9
"net"
10
10
"sync"
11
+ "time"
12
+ )
13
+
14
+ const (
15
+ // defaultBufferPoolSize controls the per-Receive temporary buffer size used to read from the socket.
16
+ // Larger values reduce syscalls and fragmentation for KVM streaming payloads.
17
+ defaultBufferPoolSize = 64 * 1024
18
+
19
+ // tcpSocketBufferSize sets OS-level socket read/write buffer hints for throughput.
20
+ tcpSocketBufferSize = 256 * 1024
21
+
22
+ // defaultKeepAlive configures TCP keepalive probe interval on the dialer.
23
+ defaultKeepAlive = 30 * time .Second
11
24
)
12
25
13
26
func NewWsmanTCP (cp Parameters ) * Target {
@@ -28,29 +41,30 @@ func NewWsmanTCP(cp Parameters) *Target {
28
41
PinnedCert : cp .PinnedCert ,
29
42
bufferPool : sync.Pool {
30
43
New : func () interface {} {
31
- return make ([]byte , 4096 ) // Adjust size according to your needs.
44
+ // Larger buffer to reduce read syscalls and frame fragmentation for KVM streams
45
+ return make ([]byte , defaultBufferPoolSize )
32
46
},
33
47
},
34
48
}
35
49
}
36
50
37
51
// Connect establishes a TCP connection to the endpoint specified in the Target struct.
38
52
func (t * Target ) Connect () error {
39
- var err error
53
+ // Use a Dialer so we can enable TCP keep-alives and TCP_NODELAY for lower latency.
54
+ d := & net.Dialer {KeepAlive : defaultKeepAlive }
40
55
41
56
if t .UseTLS {
42
- // check if pinnedCert is not null and not empty
57
+ // Build TLS config with optional pinning
43
58
var config * tls.Config
44
59
if len (t .PinnedCert ) > 0 {
45
60
config = & tls.Config {
46
61
InsecureSkipVerify : t .InsecureSkipVerify ,
47
- VerifyPeerCertificate : func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
62
+ VerifyPeerCertificate : func (rawCerts [][]byte , _ [][]* x509.Certificate ) error {
48
63
for _ , rawCert := range rawCerts {
49
64
cert , err := x509 .ParseCertificate (rawCert )
50
65
if err != nil {
51
66
return err
52
67
}
53
-
54
68
// Compare the current certificate with the pinned certificate
55
69
sha256Fingerprint := sha256 .Sum256 (cert .Raw )
56
70
if hex .EncodeToString (sha256Fingerprint [:]) == t .PinnedCert {
@@ -65,15 +79,45 @@ func (t *Target) Connect() error {
65
79
config = & tls.Config {InsecureSkipVerify : t .InsecureSkipVerify }
66
80
}
67
81
68
- t .conn , err = tls .Dial ("tcp" , t .endpoint , config )
69
- } else {
70
- t .conn , err = net .Dial ("tcp" , t .endpoint )
82
+ // Establish plain TCP first to set socket options
83
+ plainConn , err := d .Dial ("tcp" , t .endpoint )
84
+ if err != nil {
85
+ return fmt .Errorf ("failed to connect to %s: %w" , t .endpoint , err )
86
+ }
87
+
88
+ if tcp , ok := plainConn .(* net.TCPConn ); ok {
89
+ // Best-effort; ignore error to avoid failing connection setup
90
+ _ = tcp .SetNoDelay (true )
91
+ _ = tcp .SetReadBuffer (tcpSocketBufferSize )
92
+ _ = tcp .SetWriteBuffer (tcpSocketBufferSize )
93
+ }
94
+
95
+ tlsConn := tls .Client (plainConn , config )
96
+ if err := tlsConn .Handshake (); err != nil {
97
+ _ = plainConn .Close ()
98
+
99
+ return fmt .Errorf ("TLS handshake failed with %s: %w" , t .endpoint , err )
100
+ }
101
+
102
+ t .conn = tlsConn
103
+
104
+ return nil
71
105
}
72
106
107
+ // Non-TLS path
108
+ c , err := d .Dial ("tcp" , t .endpoint )
73
109
if err != nil {
74
110
return fmt .Errorf ("failed to connect to %s: %w" , t .endpoint , err )
75
111
}
76
112
113
+ if tcp , ok := c .(* net.TCPConn ); ok {
114
+ _ = tcp .SetNoDelay (true )
115
+ _ = tcp .SetReadBuffer (tcpSocketBufferSize )
116
+ _ = tcp .SetWriteBuffer (tcpSocketBufferSize )
117
+ }
118
+
119
+ t .conn = c
120
+
77
121
return nil
78
122
}
79
123
0 commit comments