diff --git a/clientconn.go b/clientconn.go index 56d0bf71..25b3eaf1 100644 --- a/clientconn.go +++ b/clientconn.go @@ -1158,7 +1158,7 @@ func (ac *addrConn) createTransport(addr resolver.Address, copts transport.Conne Authority: ac.cc.authority, } - prefaceTimer := time.NewTimer(connectDeadline.Sub(time.Now())) + prefaceTimer := time.NewTimer(time.Until(connectDeadline)) onGoAway := func(r transport.GoAwayReason) { ac.mu.Lock() diff --git a/dialoptions.go b/dialoptions.go index f2864624..aea003e6 100644 --- a/dialoptions.go +++ b/dialoptions.go @@ -348,7 +348,7 @@ func WithDialer(f func(string, time.Duration) (net.Conn, error)) DialOption { return withContextDialer( func(ctx context.Context, addr string) (net.Conn, error) { if deadline, ok := ctx.Deadline(); ok { - return f(addr, deadline.Sub(time.Now())) + return f(addr, time.Until(deadline)) } return f(addr, 0) }) diff --git a/internal/transport/http2_client.go b/internal/transport/http2_client.go index babcaee5..22fa467a 100644 --- a/internal/transport/http2_client.go +++ b/internal/transport/http2_client.go @@ -417,7 +417,7 @@ func (t *http2Client) createHeaderFields(ctx context.Context, callHdr *CallHdr) if dl, ok := ctx.Deadline(); ok { // Send out timeout regardless its value. The server can detect timeout context by itself. // TODO(mmukhi): Perhaps this field should be updated when actually writing out to the wire. - timeout := dl.Sub(time.Now()) + timeout := time.Until(dl) headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-timeout", Value: encodeTimeout(timeout)}) } for k, v := range authData { diff --git a/proxy_test.go b/proxy_test.go index 54539c20..42b668bd 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -140,7 +140,7 @@ func testHTTPConnect(t *testing.T, proxyURLModify func(*url.URL) *url.URL, proxy // Dial to proxy server. dialer := newProxyDialer(func(ctx context.Context, addr string) (net.Conn, error) { if deadline, ok := ctx.Deadline(); ok { - return net.DialTimeout("tcp", addr, deadline.Sub(time.Now())) + return net.DialTimeout("tcp", addr, time.Until(deadline)) } return net.Dial("tcp", addr) }) diff --git a/server.go b/server.go index d705d7a8..ebf86bf4 100644 --- a/server.go +++ b/server.go @@ -748,7 +748,7 @@ func (s *Server) traceInfo(st transport.ServerTransport, stream *transport.Strea trInfo.firstLine.remoteAddr = st.RemoteAddr() if dl, ok := stream.Context().Deadline(); ok { - trInfo.firstLine.deadline = dl.Sub(time.Now()) + trInfo.firstLine.deadline = time.Until(dl) } return trInfo } @@ -874,7 +874,7 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport. PeerAddr: nil, } if deadline, ok := ctx.Deadline(); ok { - logEntry.Timeout = deadline.Sub(time.Now()) + logEntry.Timeout = time.Until(deadline) if logEntry.Timeout < 0 { logEntry.Timeout = 0 } @@ -1106,7 +1106,7 @@ func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transp PeerAddr: nil, } if deadline, ok := ctx.Deadline(); ok { - logEntry.Timeout = deadline.Sub(time.Now()) + logEntry.Timeout = time.Until(deadline) if logEntry.Timeout < 0 { logEntry.Timeout = 0 } diff --git a/stream.go b/stream.go index d06279a2..cd779cac 100644 --- a/stream.go +++ b/stream.go @@ -235,7 +235,7 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth trInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method) trInfo.firstLine.client = true if deadline, ok := ctx.Deadline(); ok { - trInfo.firstLine.deadline = deadline.Sub(time.Now()) + trInfo.firstLine.deadline = time.Until(deadline) } trInfo.tr.LazyLog(&trInfo.firstLine, false) ctx = trace.NewContext(ctx, trInfo.tr) @@ -297,7 +297,7 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth Authority: cs.cc.authority, } if deadline, ok := ctx.Deadline(); ok { - logEntry.Timeout = deadline.Sub(time.Now()) + logEntry.Timeout = time.Until(deadline) if logEntry.Timeout < 0 { logEntry.Timeout = 0 } diff --git a/test/end2end_test.go b/test/end2end_test.go index a61e1464..98ead4f8 100644 --- a/test/end2end_test.go +++ b/test/end2end_test.go @@ -6951,7 +6951,7 @@ func testLargeTimeout(t *testing.T, e env) { for i, maxTimeout := range timeouts { ts.unaryCall = func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { deadline, ok := ctx.Deadline() - timeout := deadline.Sub(time.Now()) + timeout := time.Until(deadline) minTimeout := maxTimeout - 5*time.Second if !ok || timeout < minTimeout || timeout > maxTimeout { t.Errorf("ctx.Deadline() = (now+%v), %v; want [%v, %v], true", timeout, ok, minTimeout, maxTimeout)