2

I've just started using google cloud functions. And they behave in a strange way (or I'm doing something wrong). I have the following function which recalculates the average rating once user rates some item

exports.onItemRated = functions.database.ref('/votes_history/{rankingId}/current/{itemId}').onWrite(event => {

const ranks = event.data.val();
const itemId = event.params.itemId;
const rankingId = event.params.rankingId;

let avg = 0;
let num = 0;
Object.keys(ranks).forEach( (rankerId) => { //sum up all ratings for the item
    avg += ranks[rankerId];
    num ++;
});

avg /= num;

return admin.database().ref(`averages/${rankingId}/${itemId}`).transaction( (curVal) => {
    return {avg, num};
});
}

I was testing it on a very small amount of data. Like 3 ratings. So, there should be nothing to take long time, I believe. However, here are the logs: Function logs

It's true that my "Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions". The thing I want to figure out is - whether there is a problem in my function or it's just restrictions. And also would be great if someone could explain how those restrictions work.

===UPDATE===

Thanks to Cloud Functions for Firebase onWrite timeout managed to solve the timeout problem by wrapping transaction into own Promise. Looks like it's a bug.

However, even now function execution time is very unpredictable. Not as crazy as before but still ranges from 50ms (perfect) up to 2k+ ms. I don't get why there is so huge difference. And what could even be the reason for that function to execute 2000ms?

Community
  • 1
  • 1
  • 1
    Please bear in mind that Cloud Functions is current in public beta, and as such, the engineers are still working out some problems. Note that there are currently no guarantees for performance or availability. There should be guarantees when the service exits beta. – Doug Stevenson Apr 01 '17 at 20:15
  • 1
    What you're likely seeing is "cold start time" -- when a function is executed if it's been idle or all existing instances of the function container are running a new one needs to be spun up. That's why you might see ~2-3s instead of ms on some invocations. When your function is being executed on a regular basis this will tend to happen less. – Michael Bleigh Apr 01 '17 at 23:57

0 Answers0