5
5
"encoding/hex"
6
6
"net"
7
7
"sync"
8
+ "time"
8
9
9
10
gossh "golang.org/x/crypto/ssh"
10
11
)
@@ -92,10 +93,14 @@ type Context interface {
92
93
}
93
94
94
95
type sshContext struct {
95
- context.Context
96
- * sync.RWMutex
96
+ ctx context.Context
97
+ mtx * sync.RWMutex
97
98
}
98
99
100
+ var _ context.Context = & sshContext {}
101
+
102
+ var _ sync.Locker = & sshContext {}
103
+
99
104
func newContext (srv * Server ) (* sshContext , context.CancelFunc ) {
100
105
innerCtx , cancel := context .WithCancel (context .Background ())
101
106
ctx := & sshContext {innerCtx , & sync.RWMutex {}}
@@ -120,21 +125,45 @@ func applyConnMetadata(ctx Context, conn gossh.ConnMetadata) {
120
125
}
121
126
122
127
func (ctx * sshContext ) SetValue (key , value interface {}) {
123
- ctx .RWMutex .Lock ()
124
- defer ctx .RWMutex .Unlock ()
125
- ctx .Context = context .WithValue (ctx .Context , key , value )
128
+ ctx .mtx .Lock ()
129
+ defer ctx .mtx .Unlock ()
130
+ ctx .ctx = context .WithValue (ctx .ctx , key , value )
126
131
}
127
132
128
133
func (ctx * sshContext ) Value (key interface {}) interface {} {
129
- ctx .RWMutex .RLock ()
130
- defer ctx .RWMutex .RUnlock ()
131
- return ctx .Context .Value (key )
134
+ ctx .mtx .RLock ()
135
+ defer ctx .mtx .RUnlock ()
136
+ return ctx .ctx .Value (key )
132
137
}
133
138
134
139
func (ctx * sshContext ) Done () <- chan struct {} {
135
- ctx .RWMutex .RLock ()
136
- defer ctx .RWMutex .RUnlock ()
137
- return ctx .Context .Done ()
140
+ ctx .mtx .RLock ()
141
+ defer ctx .mtx .RUnlock ()
142
+ return ctx .ctx .Done ()
143
+ }
144
+
145
+ // Deadline implements context.Context.
146
+ func (ctx * sshContext ) Deadline () (deadline time.Time , ok bool ) {
147
+ ctx .mtx .RLock ()
148
+ defer ctx .mtx .RUnlock ()
149
+ return ctx .ctx .Deadline ()
150
+ }
151
+
152
+ // Err implements context.Context.
153
+ func (ctx * sshContext ) Err () error {
154
+ ctx .mtx .RLock ()
155
+ defer ctx .mtx .RUnlock ()
156
+ return ctx .ctx .Err ()
157
+ }
158
+
159
+ // Lock implements sync.Locker.
160
+ func (ctx * sshContext ) Lock () {
161
+ ctx .mtx .Lock ()
162
+ }
163
+
164
+ // Unlock implements sync.Locker.
165
+ func (ctx * sshContext ) Unlock () {
166
+ ctx .mtx .Unlock ()
138
167
}
139
168
140
169
func (ctx * sshContext ) User () string {
0 commit comments