0

I deployed a function in Azure that is triggered by a timer, every 5 minutes it executes a GET request to an API and stores the results into MongoDB hosted in a server.

Running the function in my localhost accessing the same MongoDB server works fine, but running the function on Azure I get a exception precisely every 10 minutes (or 2 function executions) as we can see bellow:

enter image description here

The exception throw is the following:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I have tried changing the MongoDB client configuration, but didn't work. Any ideias why this happens on Azure and not on the localhost emulator? And more importantly, is there any fix or work around?

Thank you!

1 Answer 1

0

The connection you're opening here is actually a pool of 5 connections (by default) so you're right to just connect and leave it open. My guess is that you intermittently lose connectivity with mongolab and your connections die when that occurs. Hopefully, enabling auto_reconnect resolves that.

You can write a ping request to check if connection is alive or not like below.

var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
server.Ping();

Alternatively while making connection

client = new MongoClient("mongodb://localhost");
database = client.GetDatabase(mongoDbStr);
bool isMongoLive = database.RunCommandAsync((Command<BsonDocument>)"{ping:1}").Wait(1000);

if(isMongoLive)
{
    // connected
}
else
{
    // couldn't connect
}

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.