cleanup: use time.Until(t) instead of t.Sub(time.Now) (#2571)

This commit is contained in:
Doug Fawley
2019-01-15 16:09:50 -08:00
committed by GitHub
parent 38b35dcb7c
commit dfd7708d35
7 changed files with 10 additions and 10 deletions

View File

@ -1158,7 +1158,7 @@ func (ac *addrConn) createTransport(addr resolver.Address, copts transport.Conne
Authority: ac.cc.authority, Authority: ac.cc.authority,
} }
prefaceTimer := time.NewTimer(connectDeadline.Sub(time.Now())) prefaceTimer := time.NewTimer(time.Until(connectDeadline))
onGoAway := func(r transport.GoAwayReason) { onGoAway := func(r transport.GoAwayReason) {
ac.mu.Lock() ac.mu.Lock()

View File

@ -348,7 +348,7 @@ func WithDialer(f func(string, time.Duration) (net.Conn, error)) DialOption {
return withContextDialer( return withContextDialer(
func(ctx context.Context, addr string) (net.Conn, error) { func(ctx context.Context, addr string) (net.Conn, error) {
if deadline, ok := ctx.Deadline(); ok { if deadline, ok := ctx.Deadline(); ok {
return f(addr, deadline.Sub(time.Now())) return f(addr, time.Until(deadline))
} }
return f(addr, 0) return f(addr, 0)
}) })

View File

@ -417,7 +417,7 @@ func (t *http2Client) createHeaderFields(ctx context.Context, callHdr *CallHdr)
if dl, ok := ctx.Deadline(); ok { if dl, ok := ctx.Deadline(); ok {
// Send out timeout regardless its value. The server can detect timeout context by itself. // 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. // 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)}) headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-timeout", Value: encodeTimeout(timeout)})
} }
for k, v := range authData { for k, v := range authData {

View File

@ -140,7 +140,7 @@ func testHTTPConnect(t *testing.T, proxyURLModify func(*url.URL) *url.URL, proxy
// Dial to proxy server. // Dial to proxy server.
dialer := newProxyDialer(func(ctx context.Context, addr string) (net.Conn, error) { dialer := newProxyDialer(func(ctx context.Context, addr string) (net.Conn, error) {
if deadline, ok := ctx.Deadline(); ok { 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) return net.Dial("tcp", addr)
}) })

View File

@ -748,7 +748,7 @@ func (s *Server) traceInfo(st transport.ServerTransport, stream *transport.Strea
trInfo.firstLine.remoteAddr = st.RemoteAddr() trInfo.firstLine.remoteAddr = st.RemoteAddr()
if dl, ok := stream.Context().Deadline(); ok { if dl, ok := stream.Context().Deadline(); ok {
trInfo.firstLine.deadline = dl.Sub(time.Now()) trInfo.firstLine.deadline = time.Until(dl)
} }
return trInfo return trInfo
} }
@ -874,7 +874,7 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.
PeerAddr: nil, PeerAddr: nil,
} }
if deadline, ok := ctx.Deadline(); ok { if deadline, ok := ctx.Deadline(); ok {
logEntry.Timeout = deadline.Sub(time.Now()) logEntry.Timeout = time.Until(deadline)
if logEntry.Timeout < 0 { if logEntry.Timeout < 0 {
logEntry.Timeout = 0 logEntry.Timeout = 0
} }
@ -1106,7 +1106,7 @@ func (s *Server) processStreamingRPC(t transport.ServerTransport, stream *transp
PeerAddr: nil, PeerAddr: nil,
} }
if deadline, ok := ctx.Deadline(); ok { if deadline, ok := ctx.Deadline(); ok {
logEntry.Timeout = deadline.Sub(time.Now()) logEntry.Timeout = time.Until(deadline)
if logEntry.Timeout < 0 { if logEntry.Timeout < 0 {
logEntry.Timeout = 0 logEntry.Timeout = 0
} }

View File

@ -235,7 +235,7 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
trInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method) trInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
trInfo.firstLine.client = true trInfo.firstLine.client = true
if deadline, ok := ctx.Deadline(); ok { 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) trInfo.tr.LazyLog(&trInfo.firstLine, false)
ctx = trace.NewContext(ctx, trInfo.tr) ctx = trace.NewContext(ctx, trInfo.tr)
@ -297,7 +297,7 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
Authority: cs.cc.authority, Authority: cs.cc.authority,
} }
if deadline, ok := ctx.Deadline(); ok { if deadline, ok := ctx.Deadline(); ok {
logEntry.Timeout = deadline.Sub(time.Now()) logEntry.Timeout = time.Until(deadline)
if logEntry.Timeout < 0 { if logEntry.Timeout < 0 {
logEntry.Timeout = 0 logEntry.Timeout = 0
} }

View File

@ -6951,7 +6951,7 @@ func testLargeTimeout(t *testing.T, e env) {
for i, maxTimeout := range timeouts { for i, maxTimeout := range timeouts {
ts.unaryCall = func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { ts.unaryCall = func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
deadline, ok := ctx.Deadline() deadline, ok := ctx.Deadline()
timeout := deadline.Sub(time.Now()) timeout := time.Until(deadline)
minTimeout := maxTimeout - 5*time.Second minTimeout := maxTimeout - 5*time.Second
if !ok || timeout < minTimeout || timeout > maxTimeout { if !ok || timeout < minTimeout || timeout > maxTimeout {
t.Errorf("ctx.Deadline() = (now+%v), %v; want [%v, %v], true", timeout, ok, minTimeout, maxTimeout) t.Errorf("ctx.Deadline() = (now+%v), %v; want [%v, %v], true", timeout, ok, minTimeout, maxTimeout)