@@ -109,7 +109,7 @@ func (c *Conn) write(ctx context.Context, typ MessageType, p []byte) (int, error
109
109
110
110
if ! c .flate () {
111
111
defer c .msgWriter .mu .unlock ()
112
- return c .writeFrame (true , ctx , true , false , c .msgWriter .opcode , p )
112
+ return c .writeFrame (ctx , true , false , c .msgWriter .opcode , p )
113
113
}
114
114
115
115
n , err := mw .Write (p )
@@ -146,20 +146,19 @@ func (mw *msgWriter) putFlateWriter() {
146
146
147
147
// Write writes the given bytes to the WebSocket connection.
148
148
func (mw * msgWriter ) Write (p []byte ) (_ int , err error ) {
149
- if mw .closed {
150
- return 0 , errors .New ("cannot use closed writer" )
151
- }
152
-
153
149
err = mw .writeMu .lock (mw .ctx )
154
150
if err != nil {
155
151
return 0 , fmt .Errorf ("failed to write: %w" , err )
156
152
}
157
153
defer mw .writeMu .unlock ()
158
154
155
+ if mw .closed {
156
+ return 0 , errors .New ("cannot use closed writer" )
157
+ }
158
+
159
159
defer func () {
160
160
if err != nil {
161
161
err = fmt .Errorf ("failed to write: %w" , err )
162
- mw .writeMu .unlock ()
163
162
mw .c .close (err )
164
163
}
165
164
}()
@@ -180,7 +179,7 @@ func (mw *msgWriter) Write(p []byte) (_ int, err error) {
180
179
}
181
180
182
181
func (mw * msgWriter ) write (p []byte ) (int , error ) {
183
- n , err := mw .c .writeFrame (true , mw .ctx , false , mw .flate , mw .opcode , p )
182
+ n , err := mw .c .writeFrame (mw .ctx , false , mw .flate , mw .opcode , p )
184
183
if err != nil {
185
184
return n , fmt .Errorf ("failed to write data frame: %w" , err )
186
185
}
@@ -210,7 +209,7 @@ func (mw *msgWriter) Close() (err error) {
210
209
}
211
210
}
212
211
213
- _ , err = mw .c .writeFrame (true , mw .ctx , true , mw .flate , mw .opcode , nil )
212
+ _ , err = mw .c .writeFrame (mw .ctx , true , mw .flate , mw .opcode , nil )
214
213
if err != nil {
215
214
return fmt .Errorf ("failed to write fin frame: %w" , err )
216
215
}
@@ -236,15 +235,15 @@ func (c *Conn) writeControl(ctx context.Context, opcode opcode, p []byte) error
236
235
ctx , cancel := context .WithTimeout (ctx , time .Second * 5 )
237
236
defer cancel ()
238
237
239
- _ , err := c .writeFrame (false , ctx , true , false , opcode , p )
238
+ _ , err := c .writeFrame (ctx , true , false , opcode , p )
240
239
if err != nil {
241
240
return fmt .Errorf ("failed to write control frame %v: %w" , opcode , err )
242
241
}
243
242
return nil
244
243
}
245
244
246
245
// frame handles all writes to the connection.
247
- func (c * Conn ) writeFrame (msgWriter bool , ctx context.Context , fin bool , flate bool , opcode opcode , p []byte ) (_ int , err error ) {
246
+ func (c * Conn ) writeFrame (ctx context.Context , fin bool , flate bool , opcode opcode , p []byte ) (_ int , err error ) {
248
247
err = c .writeFrameMu .lock (ctx )
249
248
if err != nil {
250
249
return 0 , err
@@ -284,10 +283,6 @@ func (c *Conn) writeFrame(msgWriter bool, ctx context.Context, fin bool, flate b
284
283
err = ctx .Err ()
285
284
default :
286
285
}
287
- c .writeFrameMu .unlock ()
288
- if msgWriter {
289
- c .msgWriter .writeMu .unlock ()
290
- }
291
286
c .close (err )
292
287
err = fmt .Errorf ("failed to write frame: %w" , err )
293
288
}
0 commit comments