firebase / firebase-admin-go

Firebase Admin Go SDK
Apache License 2.0
1.13k stars 242 forks source link

How to set a timeout for the send method of the messaging client? #442

Closed skuskusku closed 3 years ago

skuskusku commented 3 years ago

Step 2: Describe your environment

Operating System version: Windows
Firebase SDK version: v4
Library version: How can I find out
Firebase Product: messaging

Step 3: Describe the problem

I am using firebase v4 to push notifications to android devices. Now if I try to create an error situation w.r.t. to the network, the Send method of the instance of messaging.Client hangs indefinitly. In order to provoke this hanging situation, I simply made an entry in my computers hosts file to point fcm.googleapis.com to some nonsense IP address. I would have expected, that there is some timeout involved with the Send method of the messaging client, but it either seems to be rather long or there is an indefinite timeout. Is there a way that I can specify a reasonable timeout for this in order to prevent a stuck send operation?

Steps to reproduce:

Send a message but before make a detour of fcm.googleapis.com via hosts file entry to a nonsense address Relevant Code:

I am using this code to send messages to devices:

response, err := client.Send(ctx, message) if err != nil { logger.Info(err) os.Exit(-536870138) /// -536870138 = 0xE0000306 is ERR_INOTE_FCM_FAILED_SEND }

Any help or insights appreciated,

-- Stefan

hiranya911 commented 3 years ago

Just set a timeout on the context:

https://pkg.go.dev/cloud.google.com/go#hdr-Timeouts_and_Cancellation https://golang.org/pkg/context/#WithTimeout

skuskusku commented 3 years ago

Thank you very much, hiranya911, this works just fine. However, how can I determine that the error object from the client.Send method call is actually a timeout? When logging the error object when a timeout occurs, I get "context deadline exceeded" as the error string, but I would rather not want to compare this string description of the error object against the hardcoded text "context deadline exceeded". I am already checking the error object against other error conditions with calls to errorutils.IsUnauthenticated or messaging.IsRegistrationTokenNotRegistered. Is there anything available along these lines in order to check the error object for a timeout condition? Thanks in advance, Stefan.

skuskusku commented 3 years ago

Never mind, I found out on my own. Thanks again, hiranya911