-
Notifications
You must be signed in to change notification settings - Fork 63
/
remoteconn.go
166 lines (142 loc) · 2.67 KB
/
remoteconn.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
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
package main
import (
"errors"
"sync"
"time"
"github.com/ejoy/goscon/scp"
)
var errConnClosed = errors.New("conn closed")
// SCPConn .
type SCPConn struct {
*scp.Conn
connMutex sync.Mutex
connCond *sync.Cond
connErr error // error when operate on conn
connClosed bool // conn closed
// for reuse timeout
reuseCh chan struct{}
reuseTimeout time.Duration
}
func (s *SCPConn) setConnError(conn *scp.Conn, err error) {
if err == nil {
return
}
s.connMutex.Lock()
defer s.connMutex.Unlock()
if conn != s.Conn {
return
}
if s.connClosed {
return
}
s.connErr = err
}
// startWait 启动超时计数
func (s *SCPConn) startWait() {
if s.reuseCh != nil {
return
}
done := make(chan struct{})
go func() {
select {
case <-time.After(s.reuseTimeout):
s.Close()
case <-done:
}
}()
s.reuseCh = done
}
// stopWait 停止超时计数
func (s *SCPConn) stopWait() {
if s.reuseCh == nil {
return
}
close(s.reuseCh)
s.reuseCh = nil
}
func (s *SCPConn) acquireConn() (*scp.Conn, error) {
s.connMutex.Lock()
defer s.connMutex.Unlock()
for {
if s.connClosed {
return nil, s.connErr
} else if s.connErr != nil {
s.startWait()
s.connCond.Wait()
s.stopWait()
} else {
return s.Conn, nil
}
}
}
// Read .
func (s *SCPConn) Read(p []byte) (int, error) {
conn, err := s.acquireConn()
if err != nil {
return 0, err
}
n, err := conn.Read(p)
if err != nil {
// freeze, waiting for reuse
conn.Freeze()
s.setConnError(conn, err)
}
return n, nil
}
// Write returns until succeed or Conn is closed
func (s *SCPConn) Write(p []byte) (int, error) {
var nn int
for {
conn, err := s.acquireConn()
if err != nil { // conn is closed
return 0, err
}
n, err := conn.Write(p[nn:])
if err != nil {
// freeze, waiting for reuse
conn.Freeze()
s.setConnError(conn, err)
}
nn = nn + n
if nn == len(p) {
break
}
}
return nn, nil
}
// ReplaceConn .
func (s *SCPConn) ReplaceConn(conn *scp.Conn) bool {
s.connMutex.Lock()
defer s.connMutex.Unlock()
if s.connClosed {
return false
}
// TODO: check s.Conn and conn is match
// close old conn
s.Conn.Close()
// set new status
s.Conn = conn
s.connErr = nil
s.connCond.Broadcast()
return true
}
// Close .
func (s *SCPConn) Close() error {
s.connMutex.Lock()
defer s.connMutex.Unlock()
if s.connClosed {
return s.connErr
}
s.connClosed = true
s.connErr = errConnClosed
err := s.Conn.Close()
s.connCond.Broadcast()
return err
}
// NewSCPConn .
func NewSCPConn(scon *scp.Conn) *SCPConn {
scpConn := &SCPConn{Conn: scon}
scpConn.connCond = sync.NewCond(&scpConn.connMutex)
scpConn.reuseTimeout = configItemTime("scp.reuse_time")
return scpConn
}