-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.go
More file actions
95 lines (81 loc) · 2.02 KB
/
util.go
File metadata and controls
95 lines (81 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"io"
"net"
"sync"
"time"
)
/* The buffer (64KB) is used after successful authentication between the connections. */
const ConnectionBuffer = 64 * 1024
/* Strongly-typed buffer pool wrappers to eliminate type assertion overhead. */
type bufferPool struct {
pool sync.Pool
size int
}
func newBufferPool(size int) *bufferPool {
return &bufferPool{
pool: sync.Pool{
New: func() interface{} {
return make([]byte, size)
},
},
size: size,
}
}
func (p *bufferPool) Get() []byte {
v := p.pool.Get()
if v == nil {
return make([]byte, p.size)
}
vBytes := v.([]byte)
return vBytes
}
func (p *bufferPool) Put(buf []byte) {
if cap(buf) == p.size {
p.pool.Put(buf)
}
}
var bytePool = newBufferPool(ConnectionBuffer)
/* Transfer copies data from src to dst with a timeout policy. Returns copied bytes. */
func Transfer(dst net.Conn, src net.Conn) (int64, error) {
buf := bytePool.Get()
defer bytePool.Put(buf)
var written int64
const deadlineInterval = 30 * time.Second
const idleTimeout = 5 * time.Minute
/* Set initial deadline immediately to prevent infinite wait. */
_ = src.SetReadDeadline(time.Now().Add(idleTimeout))
_ = dst.SetWriteDeadline(time.Now().Add(idleTimeout))
lastDeadlineUpdate := time.Now()
for {
/* Refresh deadline less frequently. */
if time.Since(lastDeadlineUpdate) > deadlineInterval {
_ = src.SetReadDeadline(time.Now().Add(idleTimeout))
_ = dst.SetWriteDeadline(time.Now().Add(idleTimeout))
lastDeadlineUpdate = time.Now()
}
n, err := src.Read(buf)
if n > 0 {
/* Write all bytes with retry loop to handle short writes. */
offset := 0
for offset < n {
nw, wErr := dst.Write(buf[offset:n])
if wErr != nil {
return written, wErr
} else if nw > 0 {
written += int64(nw)
offset += nw
} else if nw == 0 {
/* Zero write without error is unusual, treat as stall. */
return written, io.ErrShortWrite
}
}
}
if err != nil {
if err == io.EOF {
return written, nil
}
return written, err
}
}
}