-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutil.go
More file actions
167 lines (151 loc) · 3.63 KB
/
util.go
File metadata and controls
167 lines (151 loc) · 3.63 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package proxy
import (
"context"
// "net"
// "net/http"
"io"
// "os"
"time"
// "strings"
"errors"
// "fmt"
// "sync"
"github.com/urnetwork/connect"
)
func copyBufferWithTimeout(dst io.Writer, src io.Reader, buf []byte, readTimeout time.Duration, writeTimeout time.Duration) (written int64, err error) {
return copyBufferWithTimeoutAndFlush(dst, src, buf, readTimeout, writeTimeout, nil)
}
// based on `io.copyBuffer` with changes:
// - default buffer 2kib
// - read and write timeouts
// - optional flush
func copyBufferWithTimeoutAndFlush(dst io.Writer, src io.Reader, buf []byte, readTimeout time.Duration, writeTimeout time.Duration, flush func()) (written int64, err error) {
if buf == nil {
buf = connect.MessagePoolGet(2048)
defer connect.MessagePoolReturn(buf)
}
for {
if 0 < readTimeout {
if c, ok := src.(interface{ SetReadDeadline(time.Time) error }); ok {
c.SetReadDeadline(time.Now().Add(readTimeout))
}
}
nr, er := src.Read(buf)
if nr > 0 {
if 0 < writeTimeout {
if c, ok := dst.(interface{ SetWriteDeadline(time.Time) error }); ok {
c.SetWriteDeadline(time.Now().Add(writeTimeout))
}
}
nw, ew := dst.Write(buf[0:nr])
if nw < 0 || nr < nw {
nw = 0
if ew == nil {
ew = errors.New("invalid write result")
}
}
written += int64(nw)
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
if flush != nil {
flush()
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return written, err
}
func copyConn(ctx context.Context, cancel context.CancelFunc, conn io.ReadWriter, proxyConn io.ReadWriter, readTimeout time.Duration, writeTimeout time.Duration) error {
return copyRw(
ctx,
cancel,
conn,
conn,
proxyConn,
proxyConn,
readTimeout,
writeTimeout,
)
}
func copyRw(ctx context.Context, cancel context.CancelFunc, connReader io.Reader, connWriter io.Writer, proxyConnReader io.Reader, proxyConnWriter io.Writer, readTimeout time.Duration, writeTimeout time.Duration) error {
// errs := make(chan error)
// var wg sync.WaitGroup
// wg.Add(1)
// go connect.HandleError(func() {
// defer wg.Done()
// defer cancel()
// _, err := copyBufferWithTimeout(proxyConnWriter, connReader, nil, readTimeout, writeTimeout)
// select {
// case <- ctx.Done():
// case errs <- err:
// }
// })
// wg.Add(1)
// go connect.HandleError(func() {
// defer wg.Done()
// defer cancel()
// buf := connect.MessagePoolGet(2048)
// defer connect.MessagePoolReturn(buf)
// n := int((readTimeout + flushTimeout - 1) / flushTimeout)
// for c := 0; c < n; {
// n, err := copyBufferWithTimeout(connWriter, proxyConnReader, buf, flushTimeout, writeTimeout)
// if err == nil || !errors.Is(err, os.ErrDeadlineExceeded) {
// select {
// case <- ctx.Done():
// case errs <- err:
// }
// }
// if 0 < n {
// c = 0
// } else {
// select {
// case <- ctx.Done():
// return
// default:
// c += 1
// }
// }
// }
// })
// select {
// case <- ctx.Done():
// return nil
// case err := <- errs:
// wg.Wait()
// return err
// }
errs := make(chan error)
go connect.HandleError(func() {
defer cancel()
_, err := copyBufferWithTimeout(proxyConnWriter, connReader, nil, readTimeout, writeTimeout)
select {
case <-ctx.Done():
case errs <- err:
}
})
go connect.HandleError(func() {
defer cancel()
_, err := copyBufferWithTimeout(connWriter, proxyConnReader, nil, readTimeout, writeTimeout)
select {
case <-ctx.Done():
case errs <- err:
}
})
select {
case <-ctx.Done():
return nil
case err := <-errs:
return err
}
}