@@ -169,7 +169,7 @@ func (client *SingleDaxClient) startHealthChecks(cc *cluster, host hostPort) {
169169 ctx , cfn := context .WithTimeout (aws .BackgroundContext (), 1 * time .Second )
170170 defer cfn ()
171171 var err error
172- _ , err = client .endpoints (RequestOptions {MaxRetries : 2 , Context : ctx })
172+ _ , err = client .endpoints (RequestOptions {MaxRetries : 3 , Context : ctx })
173173 if err != nil {
174174 cc .debugLog (fmt .Sprintf ("Health checks failed with error " + err .Error () + " for host :: " + host .host ))
175175 cc .onHealthCheckFailed (host )
@@ -740,31 +740,41 @@ func (client *SingleDaxClient) executeWithContext(ctx aws.Context, op string, en
740740 return err
741741 }
742742 if err = client .pool .setDeadline (ctx , t ); err != nil {
743- client .pool .discard (t )
743+ // If the error is just due to context cancelled or timeout
744+ // then the tube is still usable because we have not written anything to tube
745+ if err == ctx .Err () {
746+ client .pool .put (t )
747+ return err
748+ }
749+ // If we get error while setting deadline of tube
750+ // probably something is wrong with the tube
751+ client .pool .closeTube (t )
744752 return err
745753 }
746754
747755 if err = client .auth (t ); err != nil {
748- client .pool .discard (t )
756+ // Auth method writes in the tube and
757+ // it is not guaranteed that it will be drained completely on error
758+ client .pool .closeTube (t )
749759 return err
750760 }
751761
752762 writer := t .CborWriter ()
753763 if err = encoder (writer ); err != nil {
754- // Validation errors will cause pool to be discarded as there is no guarantee
764+ // Validation errors will cause connection to be closed as there is no guarantee
755765 // that the validation was performed before any data was written into tube
756- client .pool .discard (t )
766+ client .pool .closeTube (t )
757767 return err
758768 }
759769 if err := writer .Flush (); err != nil {
760- client .pool .discard (t )
770+ client .pool .closeTube (t )
761771 return err
762772 }
763773
764774 reader := t .CborReader ()
765775 ex , err := decodeError (reader )
766- if err != nil { // decode or network error
767- client .pool .discard (t )
776+ if err != nil { // decode or network error - doesn't guarantee completely drained tube
777+ client .pool .closeTube (t )
768778 return err
769779 }
770780 if ex != nil { // user or server error
@@ -774,7 +784,8 @@ func (client *SingleDaxClient) executeWithContext(ctx aws.Context, op string, en
774784
775785 err = decoder (reader )
776786 if err != nil {
777- client .pool .discard (t )
787+ // we are not able to completely drain tube
788+ client .pool .closeTube (t )
778789 } else {
779790 client .pool .put (t )
780791 }
@@ -809,7 +820,7 @@ func (client *SingleDaxClient) recycleTube(t tube, err error) {
809820 if recycle {
810821 client .pool .put (t )
811822 } else {
812- client .pool .discard (t )
823+ client .pool .closeTube (t )
813824 }
814825}
815826func (client * SingleDaxClient ) auth (t tube ) error {
0 commit comments