grpc-swift: How to set timeout for an RPC in Swift?

732 views Asked by At

I am using https://github.com/grpc/grpc-swift for inter-process communication. I have a GRPC server written in Go that listens on a unix domain socket, and a macOS app written in Swift that communicates with it over the socket.

Let's say the Go server process is not running and I make an RPC call from my Swift program. The default timeout before the call will fail is 20 seconds, but I would like to shorten it to 1 second. I am trying to do something like this:

let callOptions = CallOptions(timeLimit: .seconds(1)) // <-- Does not compile

This fails with compile error Type 'TimeLimit' has no member 'seconds'.

What is the correct way to decrease the timeout interval for Swift GRPC calls?

1

There are 1 answers

3
Clément Jean On BEST ANSWER

As mentioned in the error TimeLimit don't have a member seconds. This seconds function that you are trying to access is inside TimeAmount. So if you want to use a deadline, you will need to use:

CallOptions(timeLimit: .deadline(.now() + .seconds(1)))

here the .now is inside NIODeadline and it as a + operator defined for adding with TimeLimit (check here).

and for a timeout:

CallOptions(timeLimit: .timeout(.seconds(1)))

Note that I'm not an expert in Swift, but I checked in TimeLimitTests.swift and that seems to be the idea.