perf(tunnel): use defer for buffer pool release

cognitive-glitch committed Dec 10, 2025 at 16:54 UTC f3a31594a5fb8adc136a962dc67c64e751afbd2f
4 files changed +12 -46
cmd/portal-tunnel/main.go
+2 -2
@@ -181,15 +181,15 @@ func proxyConnection(ctx context.Context, localAddr string, relayConn net.Conn)
181
182 go func() {
183 buf := bufferPool.Get().([]byte)
184 + defer bufferPool.Put(buf)
185 _, err := io.CopyBuffer(localConn, relayConn, buf)
185 - bufferPool.Put(buf)
186 errCh <- err
187 }()
188
189 go func() {
190 buf := bufferPool.Get().([]byte)
191 + defer bufferPool.Put(buf)
192 _, err := io.CopyBuffer(relayConn, localConn, buf)
192 - bufferPool.Put(buf)
193 errCh <- err
194 }()
195
cmd/webclient/main_js.go
+1 -1
@@ -39,7 +39,7 @@ var (
39 dialerCredential *cryptoops.Credential
40
41 // DNS cache for lease name -> lease ID mapping
42 - dnsCache sync.Map // map[string]*dnsCacheEntry
42 + dnsCache sync.Map // map[string]*dnsCacheEntry
43 dnsCacheTTL = 5 * time.Minute
44 )
45
portal/client.go
+1 -1
@@ -98,7 +98,7 @@ func NewRelayClient(conn io.ReadWriteCloser) *RelayClient {
98
99 // Create yamux session as client
100 config := yamux.DefaultConfig()
101 - config.Logger = nil // Disable logging for cleaner output
101 + config.Logger = nil // Disable logging for cleaner output
102 config.MaxStreamWindowSize = 16 * 1024 * 1024 // 16MB for high-BDP scenarios
103 config.StreamOpenTimeout = 75 * time.Second
104 config.StreamCloseTimeout = 5 * time.Minute
portal/lease.go
+8 -42
@@ -1,7 +1,6 @@
1 package portal
2
3 import (
4 - "encoding/json"
4 "regexp"
5 "sync"
6 "time"
@@ -10,23 +9,12 @@ import (
9 "gosuda.org/portal/portal/core/proto/rdverb"
10 )
11
13 -// ParsedMetadata contains pre-parsed lease metadata fields.
14 -// Defined locally to avoid sdk dependency in core package.
15 -type ParsedMetadata struct {
16 - Description string
17 - Tags []string
18 - Thumbnail string
19 - Owner string
20 - Hide bool
21 -}
22 -
12 // LeaseEntry represents a registered lease with expiration tracking.
13 type LeaseEntry struct {
25 - Lease *rdverb.Lease
26 - Expires time.Time
27 - LastSeen time.Time
28 - ConnectionID int64
29 - ParsedMetadata *ParsedMetadata // Cached parsed metadata
14 + Lease *rdverb.Lease
15 + Expires time.Time
16 + LastSeen time.Time
17 + ConnectionID int64
18 }
19
20 type LeaseManager struct {
@@ -130,33 +118,11 @@ func (lm *LeaseManager) UpdateLease(lease *rdverb.Lease, connectionID int64) boo
118 }
119 }
120
133 - // Parse metadata once for cached access
134 - var parsedMeta *ParsedMetadata
135 - if lease.Metadata != "" {
136 - var meta struct {
137 - Description string `json:"description"`
138 - Tags []string `json:"tags"`
139 - Thumbnail string `json:"thumbnail"`
140 - Owner string `json:"owner"`
141 - Hide bool `json:"hide"`
142 - }
143 - if err := json.Unmarshal([]byte(lease.Metadata), &meta); err == nil {
144 - parsedMeta = &ParsedMetadata{
145 - Description: meta.Description,
146 - Tags: meta.Tags,
147 - Thumbnail: meta.Thumbnail,
148 - Owner: meta.Owner,
149 - Hide: meta.Hide,
150 - }
151 - }
152 - }
153 -
121 lm.leases[identityID] = &LeaseEntry{
155 - Lease: lease,
156 - Expires: expires,
157 - LastSeen: time.Now(),
158 - ConnectionID: connectionID,
159 - ParsedMetadata: parsedMeta,
122 + Lease: lease,
123 + Expires: expires,
124 + LastSeen: time.Now(),
125 + ConnectionID: connectionID,
126 }
127
128 return true