Specify a deadline with Go gRPC for peer to peer connections.

12,830

According to the WithTimeout example of context

package main

import (
    "context"
    "fmt"
    "time"
)

func main() {
    // Pass a context with a timeout to tell a blocking function that it
    // should abandon its work after the timeout elapses.
    ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
    defer cancel()

    select {
        case <-time.After(1 * time.Second):
        fmt.Println("overslept")
    case <-ctx.Done():
        fmt.Println(ctx.Err()) // prints "context deadline exceeded"
    }
} 

You can change the helloworld example client code for 100ms timeout:

ctx, _ := context.WithTimeout(context.Background(), 100 * time.Millisecond)
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
    log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.Message)
Share:
12,830
bbengfort
Author by

bbengfort

Python programmer and UMD PhD student.

Updated on June 14, 2022

Comments

  • bbengfort
    bbengfort almost 2 years

    According to the gRPC documentation, deadlines can be specified by clients to determine how long the client will wait on the server before exiting with a DEADLINE_EXCEEDED error. The documentation mentions that different languages have different implementations and that some languages do not have default values.

    Indeed, a quick CTRL+F for "deadline" on the Go gRPC documentation reveals no results. What I did discover was a WithTimeout on the dialer for the TCP connection.

    Implemented as follows (from the helloworld example):

    package main
    
    import (
        "log"
        "os"
        "time"
    
        "golang.org/x/net/context"
        "google.golang.org/grpc"
        pb "google.golang.org/grpc/examples/helloworld/helloworld"
    )
    
    const (
        address     = "localhost:50051"
        defaultName = "world"
        deadline    = 20 
    )
    
    func main() {
        // Set up a connection to the server with a timeout 
        conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithTimeout(time.Duration(deadline)*time.Second)
        if err != nil {
            log.Fatalf("did not connect: %v", err)
        }
        defer conn.Close()
        c := pb.NewGreeterClient(conn)
    
        // Contact the server and print out its response.
        name := defaultName
        if len(os.Args) > 1 {
            name = os.Args[1]
        }
        r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
        if err != nil {
            log.Fatalf("could not greet: %v", err)
        }
        log.Printf("Greeting: %s", r.Message)
    }
    

    The code will raise an error only if the client cannot connect after 20 seconds. The output will be something as follows:

    2016/05/24 09:02:54 grpc: Conn.resetTransport failed to create client transport: connection error: desc = "transport: dial tcp [::1]:3265: getsockopt: connection refused"; Reconnecting to "localhost:3265"
    2016/05/24 09:02:54 Failed to dial localhost:3265: grpc: timed out trying to connect; please retry.
    2016/05/24 09:02:54 could not greet: rpc error: code = 2 desc = grpc: the client connection is closing
    

    As noted in the question title, the system I'm working with is peer to peer, so there is no central, always up server and therefore the retry system that gRPC implements is wonderful. However, I'm actually looking for deadlines because if the remote does connect, but the server takes > 20 seconds to respond, no exception will be raised in the WithTimeout context.

    A complete win for me would be a timeout/deadline system where:

    • if the client cannot connect, an error is returned after timeout
    • if the client connects, but the server doesn't respond before timeout, an error is returned.
    • if the client connects, but the connection drops before timeout, an error is returned.

    My feeling though is that I will need some combination of connection management plus gRPC deadline management. Does anyone know how to implement deadlines in Go?

  • bbengfort
    bbengfort almost 7 years
    It's been a while since I looked at this, but you're right - having a timeout on the client side seems to be the only way to go about it according to my reading of the context library. I would update your suggestion to use the cancel method to ensure that the server does not try to respond when the client cancels the result.