MCPcopy Create free account
hub / github.com/dan-v/lambda-nat-proxy / copyWithBufferAndMetrics

Function copyWithBufferAndMetrics

pkg/shared/network.go:162–196  ·  view source on GitHub ↗

copyWithBufferAndMetrics performs optimized copying with metrics tracking

(dst io.Writer, src io.Reader, bufferSize int, recordBytes func(int64))

Source from the content-addressed store, hash-verified

160
161// copyWithBufferAndMetrics performs optimized copying with metrics tracking
162func copyWithBufferAndMetrics(dst io.Writer, src io.Reader, bufferSize int, recordBytes func(int64)) (written int64, err error) {
163 buf := make([]byte, bufferSize)
164 for {
165 nr, er := src.Read(buf)
166 if nr > 0 {
167 nw, ew := dst.Write(buf[0:nr])
168 if nw < 0 || nr < nw {
169 nw = 0
170 if ew == nil {
171 ew = fmt.Errorf("invalid write result")
172 }
173 }
174 written += int64(nw)
175 // Record bytes transferred for metrics
176 if recordBytes != nil && nw > 0 {
177 recordBytes(int64(nw))
178 }
179 if ew != nil {
180 err = ew
181 break
182 }
183 if nr != nw {
184 err = io.ErrShortWrite
185 break
186 }
187 }
188 if er != nil {
189 if er != io.EOF {
190 err = er
191 }
192 break
193 }
194 }
195 return written, err
196}
197
198// OptimizedCopyWithContext performs high-performance bidirectional copying between two connections with context support
199func OptimizedCopyWithContext(ctx context.Context, dst, src net.Conn) {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected