Race conditions on data provider
Hey all - I have a golf-centric app I am building. There are various endpoints my data provider gives. I poll the leaderboard entry every minute via edge function and upsert the returned data into a table. The leaderboard lists basic info about the golfer - what their strokes on the various rounds are, what their current hole is, and how many holes the golfer is through. If the thru value changes, a trigger is invoked and a database function is called which then calls an edge function to get the scorecard for that golfer. The scorecard has a lot more detail - like how they did each hole on the day. So far this works great about 95% of the time. But that 5% there is a race condition where the data appears on the leaderboard endpoint but does not exist on the scorecard yet. Usually this is not that big of a deal and will just get scooped up and added when the golfer finishes the next hole (although it does look weird in the app during this time as the “thru” will be 13 and the scorecard only shows the first 12 holes, for instance. But when it really turns into a problem is the end of the day. Because the trigger is on thru, and the golfer may be done for the day so this doesn’t change, it will never be updated so data is just forever missing unless I take extra steps to fetch the scorecard. So with that book I just wrote out of the way, is there a way things like this can be handled more elegantly to avoid race conditions like these?
Things I’ve thought about or fiddled with:
Things I’ve thought about or fiddled with:
- Pass the thru in when I fetch the scorecards and if the hole marked thru is not on it, fetch until it is. But once it took 4 minutes to appear and that’s a long time in a loop so I do not think this is feasible/possible.
- Every time the trigger fired tried updating the thru for any golfer not matching their thru. This would fire the trigger and work that way, but the thru would be out of sync on the app until the next minute when it was reset to the correct value.