@@ -171,6 +171,7 @@ type Conn struct {
171171 w contextWriter
172172
173173 writeTimeout time.Duration
174+ requestTimeout time.Duration // Request timeout, used for setting up request timers
174175 cfg * ConnConfig
175176 frameObserver FrameHeaderObserver
176177 streamObserver StreamObserver
@@ -279,6 +280,7 @@ func (s *Session) dialWithoutObserver(ctx context.Context, host *HostInfo, cfg *
279280 logger : logger ,
280281 streamObserver : s .streamObserver ,
281282 writeTimeout : writeTimeout ,
283+ requestTimeout : cfg .ConnectTimeout ,
282284 }
283285
284286 if err := c .init (ctx , dialedHost ); err != nil {
@@ -312,6 +314,7 @@ func (c *Conn) init(ctx context.Context, dialedHost *DialedHost) error {
312314 }
313315
314316 c .r .SetTimeout (c .cfg .Timeout )
317+ c .requestTimeout = c .cfg .Timeout
315318
316319 // dont coalesce startup frames
317320 if c .session .cfg .WriteCoalesceWaitTime > 0 && ! c .cfg .disableCoalesce && ! dialedHost .DisableCoalesce {
@@ -335,8 +338,8 @@ type startupCoordinator struct {
335338
336339func (s * startupCoordinator ) setupConn (ctx context.Context ) error {
337340 var cancel context.CancelFunc
338- if s .conn .r . GetTimeout () > 0 {
339- ctx , cancel = context .WithTimeout (ctx , s .conn .r . GetTimeout () )
341+ if s .conn .requestTimeout > 0 {
342+ ctx , cancel = context .WithTimeout (ctx , s .conn .requestTimeout )
340343 } else {
341344 ctx , cancel = context .WithCancel (ctx )
342345 }
@@ -688,8 +691,9 @@ func (c *Conn) processFrame(ctx context.Context, r io.Reader) error {
688691
689692 // read a full header, ignore timeouts, as this is being ran in a loop
690693 // TODO: TCP level deadlines? or just query level deadlines?
691- if c .r .GetTimeout () > 0 {
692- c .r .SetReadDeadline (time.Time {})
694+ readTimeout := c .r .GetTimeout ()
695+ if readTimeout > 0 {
696+ c .r .SetTimeout (0 )
693697 }
694698
695699 headStartTime := time .Now ()
@@ -700,6 +704,11 @@ func (c *Conn) processFrame(ctx context.Context, r io.Reader) error {
700704 return err
701705 }
702706
707+ // Set timeout back for reading frame body
708+ if readTimeout > 0 {
709+ c .r .SetTimeout (readTimeout )
710+ }
711+
703712 if c .frameObserver != nil {
704713 c .frameObserver .ObserveFrameHeader (context .Background (), ObservedFrameHeader {
705714 Version : protoVersion (head .version ),
@@ -802,12 +811,24 @@ func (c *Conn) recvSegment(ctx context.Context) error {
802811 err error
803812 )
804813
814+ // Read segment without timeout, as this is being run in a loop waiting for the next segment
815+ readTimeout := c .r .GetTimeout ()
816+ if readTimeout > 0 {
817+ c .r .SetTimeout (0 )
818+ }
819+
805820 // Read frame based on compression
806821 if c .compressor != nil {
807822 frame , isSelfContained , err = readCompressedSegment (c .r , c .compressor )
808823 } else {
809824 frame , isSelfContained , err = readUncompressedSegment (c .r )
810825 }
826+
827+ // Restore timeout for subsequent segment reads in multi-segment frames
828+ if readTimeout > 0 {
829+ c .r .SetTimeout (readTimeout )
830+ }
831+
811832 if err != nil {
812833 return err
813834 }
@@ -908,6 +929,8 @@ func (c *connReader) Read(p []byte) (n int, err error) {
908929 var nn int
909930 if c .timeout > 0 {
910931 c .conn .SetReadDeadline (time .Now ().Add (c .timeout ))
932+ } else if c .timeout == 0 {
933+ c .conn .SetReadDeadline (time.Time {})
911934 }
912935
913936 nn , err = io .ReadFull (c .r , p [n :])
@@ -1309,7 +1332,7 @@ func (c *Conn) execInternal(ctx context.Context, req frameBuilder, tracer Tracer
13091332 }
13101333
13111334 var timeoutCh <- chan time.Time
1312- if timeout := c . r . GetTimeout (); timeout > 0 {
1335+ if c . requestTimeout > 0 {
13131336 if call .timer == nil {
13141337 call .timer = time .NewTimer (0 )
13151338 <- call .timer .C
@@ -1322,7 +1345,7 @@ func (c *Conn) execInternal(ctx context.Context, req frameBuilder, tracer Tracer
13221345 }
13231346 }
13241347
1325- call .timer .Reset (timeout )
1348+ call .timer .Reset (c . requestTimeout )
13261349 timeoutCh = call .timer .C
13271350 }
13281351
0 commit comments