playlist manager: serialize insert requests

We've seen cases where videos are not inserted even though the API call succeeded.
Our suspicion is that two concurrent insert calls for the same video are causing a race.

We try to avoid this by putting a lock around insert calls
pull/288/head
Mike Lang 3 years ago
parent a942af9cb4
commit 63d8b1d504

@ -185,6 +185,9 @@ class PlaylistManager(object):
class YoutubeAPI(object): class YoutubeAPI(object):
def __init__(self, client): def __init__(self, client):
self.client = client self.client = client
# We've observed failures in the playlist API when doing concurrent calls for the same video.
# We could maybe have a per-video lock but this is easier.
self.insert_lock = gevent.lock.RLock()
def insert_into_playlist(self, playlist, video_id, index): def insert_into_playlist(self, playlist, video_id, index):
json = { json = {
@ -197,6 +200,7 @@ class YoutubeAPI(object):
"position": index, "position": index,
}, },
} }
with self.insert_lock:
resp = self.client.request("POST", "https://www.googleapis.com/youtube/v3/playlistItems", resp = self.client.request("POST", "https://www.googleapis.com/youtube/v3/playlistItems",
params={"part": "snippet"}, params={"part": "snippet"},
json=json, json=json,

Loading…
Cancel
Save