@@ -109,7 +109,7 @@ func (c *Conn) write(ctx context.Context, typ MessageType, p []byte) (int, error
109109
110110 if ! c .flate () {
111111 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 )
113113 }
114114
115115 n , err := mw .Write (p )
@@ -146,20 +146,19 @@ func (mw *msgWriter) putFlateWriter() {
146146
147147// Write writes the given bytes to the WebSocket connection.
148148func (mw * msgWriter ) Write (p []byte ) (_ int , err error ) {
149- if mw .closed {
150- return 0 , errors .New ("cannot use closed writer" )
151- }
152-
153149 err = mw .writeMu .lock (mw .ctx )
154150 if err != nil {
155151 return 0 , fmt .Errorf ("failed to write: %w" , err )
156152 }
157153 defer mw .writeMu .unlock ()
158154
155+ if mw .closed {
156+ return 0 , errors .New ("cannot use closed writer" )
157+ }
158+
159159 defer func () {
160160 if err != nil {
161161 err = fmt .Errorf ("failed to write: %w" , err )
162- mw .writeMu .unlock ()
163162 mw .c .close (err )
164163 }
165164 }()
@@ -180,7 +179,7 @@ func (mw *msgWriter) Write(p []byte) (_ int, err error) {
180179}
181180
182181func (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 )
184183 if err != nil {
185184 return n , fmt .Errorf ("failed to write data frame: %w" , err )
186185 }
@@ -210,7 +209,7 @@ func (mw *msgWriter) Close() (err error) {
210209 }
211210 }
212211
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 )
214213 if err != nil {
215214 return fmt .Errorf ("failed to write fin frame: %w" , err )
216215 }
@@ -236,15 +235,15 @@ func (c *Conn) writeControl(ctx context.Context, opcode opcode, p []byte) error
236235 ctx , cancel := context .WithTimeout (ctx , time .Second * 5 )
237236 defer cancel ()
238237
239- _ , err := c .writeFrame (false , ctx , true , false , opcode , p )
238+ _ , err := c .writeFrame (ctx , true , false , opcode , p )
240239 if err != nil {
241240 return fmt .Errorf ("failed to write control frame %v: %w" , opcode , err )
242241 }
243242 return nil
244243}
245244
246245// 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 ) {
248247 err = c .writeFrameMu .lock (ctx )
249248 if err != nil {
250249 return 0 , err
@@ -284,10 +283,6 @@ func (c *Conn) writeFrame(msgWriter bool, ctx context.Context, fin bool, flate b
284283 err = ctx .Err ()
285284 default :
286285 }
287- c .writeFrameMu .unlock ()
288- if msgWriter {
289- c .msgWriter .writeMu .unlock ()
290- }
291286 c .close (err )
292287 err = fmt .Errorf ("failed to write frame: %w" , err )
293288 }
0 commit comments