99 "crypto/rand"
1010 "encoding/binary"
1111 "io"
12+ "sync"
1213 "time"
1314
1415 "golang.org/x/xerrors"
@@ -50,8 +51,8 @@ func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error {
5051type msgWriter struct {
5152 c * Conn
5253
53- mu * mu
54- activeMu * mu
54+ mu * mu
55+ writeMu sync. Mutex
5556
5657 ctx context.Context
5758 opcode opcode
@@ -64,9 +65,8 @@ type msgWriter struct {
6465
6566func newMsgWriter (c * Conn ) * msgWriter {
6667 mw := & msgWriter {
67- c : c ,
68- mu : newMu (c ),
69- activeMu : newMu (c ),
68+ c : c ,
69+ mu : newMu (c ),
7070 }
7171 return mw
7272}
@@ -149,21 +149,17 @@ func (mw *msgWriter) returnFlateWriter() {
149149func (mw * msgWriter ) Write (p []byte ) (_ int , err error ) {
150150 defer errd .Wrap (& err , "failed to write" )
151151
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 ()
157154
158155 if mw .closed {
159156 return 0 , xerrors .New ("cannot use closed writer" )
160157 }
161158
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 {
167163 mw .ensureFlate ()
168164 }
169165 }
@@ -188,11 +184,8 @@ func (mw *msgWriter) write(p []byte) (int, error) {
188184func (mw * msgWriter ) Close () (err error ) {
189185 defer errd .Wrap (& err , "failed to close writer" )
190186
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 ()
196189
197190 if mw .closed {
198191 return xerrors .New ("cannot use closed writer" )
@@ -222,7 +215,7 @@ func (mw *msgWriter) Close() (err error) {
222215}
223216
224217func (mw * msgWriter ) close () {
225- mw .activeMu .Lock (context . Background () )
218+ mw .writeMu .Lock ()
226219 mw .returnFlateWriter ()
227220}
228221
0 commit comments