Code walkthrough: Gitea mirror service

This is one of the Code walkthrough I wrote while exploring the Gitea codebase.

When Gitea starts, it initializes the mirror service which creates the “mirror” queue singleton which runs in a RunWithShutdownFns goroutine. The “mirror” queue is created by CreateUniqueQueue via NewQueue and is backed by a persistent storage. It handles retries, timeouts etc.

The queueHandle function is called when a new SyncRequest is pushed to the queue. It calls either SyncPullMirror or SyncPushMirror to perform the mirroring and schedules the next run by updating the LastUpdateUnix database field (date and time). When the Update function runs from cron every 10 minutes, it iterates on the database records and runs the handler when it is due.

The handler does not perform the action, it pushes a SyncRequest onto the “mirror” queue and it will be passed on to the queueHandle function that was registered when the “mirror” queue was initialized. There can only be one SyncPushMirror for a given repository in the queue. That’s what the “Unique” in “UniqueQueue” stands for: the redis unique queue enforces this constraint by using SADD and failing on duplicate entries.

When the mirror is removed, the git remote is removed and the database record deleted.

I’m not sure to understand the benefit of having a UniqueQueue here. It looks like it would work exactly the same if there was only the database and a query on the existing mirrors at regular intervals to figure out what needs mirroring. I must be missing something. Ideas?