-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathioutil.go
74 lines (65 loc) · 1.04 KB
/
ioutil.go
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
package pktconn
import (
"io"
"net"
"runtime"
)
func writeFull(conn io.Writer, data []byte) error {
left := len(data)
for left > 0 {
n, err := conn.Write(data)
if n == left && err == nil { // handle most common case first
return nil
}
if n > 0 {
data = data[n:]
left -= n
}
if err != nil {
if !IsTemporary(err) {
return err
} else {
runtime.Gosched()
}
}
}
return nil
}
func readFull(conn io.Reader, data []byte) error {
left := len(data)
for left > 0 {
n, err := conn.Read(data)
if n == left && err == nil { // handle most common case first
return nil
}
if n > 0 {
data = data[n:]
left -= n
}
if err != nil {
if !IsTemporary(err) {
return err
} else {
runtime.Gosched()
}
}
}
return nil
}
type flushable interface {
Flush() error
}
func tryFlush(conn net.Conn) error {
if f, ok := conn.(flushable); ok {
for {
err := f.Flush()
if err == nil || !IsTemporary(err) {
return err
} else {
runtime.Gosched()
}
}
} else {
return nil
}
}