loading up the forgejo repo on tangled to test page performance
1// Copyright 2022 The Gitea Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package mirror
5
6import (
7 "forgejo.org/modules/graceful"
8 "forgejo.org/modules/log"
9 "forgejo.org/modules/queue"
10 "forgejo.org/modules/setting"
11)
12
13var mirrorQueue *queue.WorkerPoolQueue[*SyncRequest]
14
15// SyncType type of sync request
16type SyncType int
17
18const (
19 // PullMirrorType for pull mirrors
20 PullMirrorType SyncType = iota
21 // PushMirrorType for push mirrors
22 PushMirrorType
23)
24
25// SyncRequest for the mirror queue
26type SyncRequest struct {
27 Type SyncType
28 ReferenceID int64 // RepoID for pull mirror, MirrorID for push mirror
29}
30
31// StartSyncMirrors starts a go routine to sync the mirrors
32func StartSyncMirrors(queueHandle func(data ...*SyncRequest) []*SyncRequest) {
33 if !setting.Mirror.Enabled {
34 return
35 }
36 mirrorQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "mirror", queueHandle)
37 if mirrorQueue == nil {
38 log.Fatal("Unable to create mirror queue")
39 }
40 go graceful.GetManager().RunWithCancel(mirrorQueue)
41}
42
43// AddPullMirrorToQueue adds repoID to mirror queue
44func AddPullMirrorToQueue(repoID int64) {
45 addMirrorToQueue(PullMirrorType, repoID)
46}
47
48// AddPushMirrorToQueue adds the push mirror to the queue
49func AddPushMirrorToQueue(mirrorID int64) {
50 addMirrorToQueue(PushMirrorType, mirrorID)
51}
52
53func addMirrorToQueue(syncType SyncType, referenceID int64) {
54 if !setting.Mirror.Enabled {
55 return
56 }
57 go func() {
58 if err := PushToQueue(syncType, referenceID); err != nil {
59 log.Error("Unable to push sync request for to the queue for pull mirror repo[%d]. Error: %v", referenceID, err)
60 }
61 }()
62}
63
64// PushToQueue adds the sync request to the queue
65func PushToQueue(mirrorType SyncType, referenceID int64) error {
66 return mirrorQueue.Push(&SyncRequest{
67 Type: mirrorType,
68 ReferenceID: referenceID,
69 })
70}