9
9
"crypto/rand"
10
10
"encoding/binary"
11
11
"io"
12
+ "sync"
12
13
"time"
13
14
14
15
"golang.org/x/xerrors"
@@ -50,8 +51,8 @@ func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error {
50
51
type msgWriter struct {
51
52
c * Conn
52
53
53
- mu * mu
54
- activeMu * mu
54
+ mu * mu
55
+ writeMu sync. Mutex
55
56
56
57
ctx context.Context
57
58
opcode opcode
@@ -64,9 +65,8 @@ type msgWriter struct {
64
65
65
66
func newMsgWriter (c * Conn ) * msgWriter {
66
67
mw := & msgWriter {
67
- c : c ,
68
- mu : newMu (c ),
69
- activeMu : newMu (c ),
68
+ c : c ,
69
+ mu : newMu (c ),
70
70
}
71
71
return mw
72
72
}
@@ -149,21 +149,17 @@ func (mw *msgWriter) returnFlateWriter() {
149
149
func (mw * msgWriter ) Write (p []byte ) (_ int , err error ) {
150
150
defer errd .Wrap (& err , "failed to write" )
151
151
152
- err = mw .activeMu .Lock (mw .ctx )
153
- if err != nil {
154
- return 0 , err
155
- }
156
- defer mw .activeMu .Unlock ()
152
+ mw .writeMu .Lock ()
153
+ defer mw .writeMu .Unlock ()
157
154
158
155
if mw .closed {
159
156
return 0 , xerrors .New ("cannot use closed writer" )
160
157
}
161
158
162
- if mw .opcode != opContinuation {
163
- // First frame needs to be written.
164
- if len (p ) >= mw .c .flateThreshold {
165
- // Only enables flate if the length crosses the
166
- // threshold on the first write.
159
+ if mw .c .flate () {
160
+ // Only enables flate if the length crosses the
161
+ // threshold on the first frame
162
+ if mw .opcode != opContinuation && len (p ) >= mw .c .flateThreshold {
167
163
mw .ensureFlate ()
168
164
}
169
165
}
@@ -188,11 +184,8 @@ func (mw *msgWriter) write(p []byte) (int, error) {
188
184
func (mw * msgWriter ) Close () (err error ) {
189
185
defer errd .Wrap (& err , "failed to close writer" )
190
186
191
- err = mw .activeMu .Lock (mw .ctx )
192
- if err != nil {
193
- return err
194
- }
195
- defer mw .activeMu .Unlock ()
187
+ mw .writeMu .Lock ()
188
+ defer mw .writeMu .Unlock ()
196
189
197
190
if mw .closed {
198
191
return xerrors .New ("cannot use closed writer" )
@@ -222,7 +215,7 @@ func (mw *msgWriter) Close() (err error) {
222
215
}
223
216
224
217
func (mw * msgWriter ) close () {
225
- mw .activeMu .Lock (context . Background () )
218
+ mw .writeMu .Lock ()
226
219
mw .returnFlateWriter ()
227
220
}
228
221
0 commit comments