@@ -217,6 +217,7 @@ type httpSmartSubtransportStream struct {
217
217
sentRequest bool
218
218
recvReply sync.WaitGroup
219
219
httpError error
220
+ m sync.RWMutex
220
221
}
221
222
222
223
func newManagedHttpStream (owner * httpSmartSubtransport , req * http.Request , client * http.Client ) * httpSmartSubtransportStream {
@@ -244,6 +245,8 @@ func (self *httpSmartSubtransportStream) Read(buf []byte) (int, error) {
244
245
245
246
self .recvReply .Wait ()
246
247
248
+ self .m .RLock ()
249
+ defer self .m .RUnlock ()
247
250
if self .httpError != nil {
248
251
return 0 , self .httpError
249
252
}
@@ -252,6 +255,8 @@ func (self *httpSmartSubtransportStream) Read(buf []byte) (int, error) {
252
255
}
253
256
254
257
func (self * httpSmartSubtransportStream ) Write (buf []byte ) (int , error ) {
258
+ self .m .RLock ()
259
+ defer self .m .RUnlock ()
255
260
if self .httpError != nil {
256
261
return 0 , self .httpError
257
262
}
@@ -266,7 +271,11 @@ func (self *httpSmartSubtransportStream) Free() {
266
271
267
272
func (self * httpSmartSubtransportStream ) sendRequestBackground () {
268
273
go func () {
269
- self .httpError = self .sendRequest ()
274
+ err := self .sendRequest ()
275
+
276
+ self .m .Lock ()
277
+ self .httpError = err
278
+ self .m .Unlock ()
270
279
}()
271
280
self .sentRequest = true
272
281
}
@@ -299,33 +308,29 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
299
308
}
300
309
}
301
310
302
- for {
303
- req := & http.Request {
304
- Method : self .req .Method ,
305
- URL : self .req .URL ,
306
- Header : self .req .Header ,
307
- }
308
- if req .Method == "POST" {
309
- req .Body = self .reader
310
- req .ContentLength = - 1
311
- }
312
-
313
- req .SetBasicAuth (userName , password )
314
- resp , err = self .client .Do (req )
315
- if err != nil {
316
- return err
317
- }
311
+ req := & http.Request {
312
+ Method : self .req .Method ,
313
+ URL : self .req .URL ,
314
+ Header : self .req .Header ,
315
+ }
316
+ if req .Method == "POST" {
317
+ req .Body = self .reader
318
+ req .ContentLength = - 1
319
+ }
318
320
319
- if resp .StatusCode == http .StatusOK {
320
- break
321
- }
321
+ req .SetBasicAuth (userName , password )
322
+ resp , err = self .client .Do (req )
323
+ if err != nil {
324
+ return err
325
+ }
322
326
323
- io .Copy (ioutil .Discard , resp .Body )
324
- resp .Body .Close ()
325
- return fmt .Errorf ("Unhandled HTTP error %s" , resp .Status )
327
+ if resp .StatusCode == http .StatusOK {
328
+ self .resp = resp
329
+ self .sentRequest = true
330
+ return nil
326
331
}
327
332
328
- self . sentRequest = true
329
- self . resp = resp
330
- return nil
333
+ io . Copy ( ioutil . Discard , resp . Body )
334
+ defer resp . Body . Close ()
335
+ return fmt . Errorf ( "Unhandled HTTP error %s" , resp . Status )
331
336
}
0 commit comments