/
pipe_windows.go
84 lines (71 loc) · 1.49 KB
/
pipe_windows.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
75
76
77
78
79
80
81
82
83
84
// +build windows
package statsd
import (
"net"
"sync"
"time"
"github.com/Microsoft/go-winio"
)
const defaultPipeTimeout = 1 * time.Millisecond
type pipeWriter struct {
mu sync.RWMutex
conn net.Conn
timeout time.Duration
pipepath string
}
func (p *pipeWriter) SetWriteTimeout(d time.Duration) error {
p.mu.Lock()
p.timeout = d
p.mu.Unlock()
return nil
}
func (p *pipeWriter) Write(data []byte) (n int, err error) {
conn, err := p.ensureConnection()
if err != nil {
return 0, err
}
p.mu.RLock()
conn.SetWriteDeadline(time.Now().Add(p.timeout))
p.mu.RUnlock()
n, err = conn.Write(data)
if err != nil {
if e, ok := err.(net.Error); !ok || !e.Temporary() {
// disconnected; retry again on next attempt
p.mu.Lock()
p.conn = nil
p.mu.Unlock()
}
}
return n, err
}
func (p *pipeWriter) ensureConnection() (net.Conn, error) {
p.mu.RLock()
conn := p.conn
p.mu.RUnlock()
if conn != nil {
return conn, nil
}
// looks like we might need to connect - try again with write locking.
p.mu.Lock()
defer p.mu.Unlock()
if p.conn != nil {
return p.conn, nil
}
newconn, err := winio.DialPipe(p.pipepath, nil)
if err != nil {
return nil, err
}
p.conn = newconn
return newconn, nil
}
func (p *pipeWriter) Close() error {
return p.conn.Close()
}
func newWindowsPipeWriter(pipepath string) (*pipeWriter, error) {
// Defer connection establishment to first write
return &pipeWriter{
conn: nil,
timeout: defaultPipeTimeout,
pipepath: pipepath,
}, nil
}