@@ -6,23 +6,68 @@ import (
66
77 "gitlab-sync/internal/utils"
88
9+ "github.com/hashicorp/go-retryablehttp"
910 gitlab "gitlab.com/gitlab-org/api/client-go"
11+ "go.uber.org/zap"
1012)
1113
1214type GitlabInstance struct {
13- Gitlab * gitlab.Client
14- Projects map [string ]* gitlab.Project
15- muProjects sync.RWMutex
16- Groups map [string ]* gitlab.Group
17- muGroups sync.RWMutex
15+ // Gitlab is the GitLab client used to interact with the GitLab API
16+ Gitlab * gitlab.Client
17+ // Projects is a map of project paths to GitLab project objects, it is used to
18+ // cache projects and avoid unnecessary API calls
19+ Projects map [string ]* gitlab.Project
20+ // muProjects is a mutex used to synchronize access to the Projects map
21+ // It ensures that only one goroutine can read or write to the Projects map at a time
22+ muProjects sync.RWMutex
23+ // Groups is a map of group paths to GitLab group objects, it is used to
24+ // cache groups and avoid unnecessary API calls
25+ Groups map [string ]* gitlab.Group
26+ // muGroups is a mutex used to synchronize access to the Groups map
27+ // It ensures that only one goroutine can read or write to the Groups map at a time
28+ muGroups sync.RWMutex
29+ // GraphQLClient is the GraphQL client used to interact with the GitLab GraphQL API
30+ // It is used to perform GraphQL queries and mutations
31+ // It is initialized with the GitLab token and URL
1832 GraphQLClient * utils.GraphQLClient
33+ // Role is the role of the GitLab instance, it can be either "source" or "destination"
34+ // It is used to determine the behavior of the mirroring process
35+ Role string
36+ // BigInstance indicates whether the GitLab instance is a large instance
37+ // It is used to determine the behavior of the fetching process
38+ BigInstance bool
39+ }
40+
41+ type GitlabInstanceOpts struct {
42+ // GitlabURL is the URL of the GitLab instance
43+ GitlabURL string
44+ // GitlabToken is the token used to authenticate with the GitLab API
45+ GitlabToken string
46+ // Role is the role of the GitLab instance, it can be either "source" or "destination"
47+ Role string
48+ // Timeout is the timeout for GitLab API requests
49+ Timeout time.Duration
50+ // MaxRetries is the maximum number of retries for GitLab API requests
51+ MaxRetries int
52+ // BigInstance indicates whether the GitLab instance is a large instance
53+ BigInstance bool
1954}
2055
2156// newGitlabInstance creates a new GitlabInstance with the provided parameters
2257// and initializes the GitLab client with a custom HTTP client.
23- func newGitlabInstance (gitlabURL string , gitlabToken string , timeout time.Duration , maxRetries int ) (* GitlabInstance , error ) {
58+ func newGitlabInstance (initArgs * GitlabInstanceOpts ) (* GitlabInstance , error ) {
59+ // Create a custom HTTP client with the specified timeout and max retries
60+ retryClient := retryablehttp .NewClient ()
61+ retryClient .HTTPClient .Timeout = initArgs .Timeout
62+ retryClient .RetryMax = initArgs .MaxRetries
63+ retryClient .CheckRetry = retryablehttp .DefaultRetryPolicy
64+ retryClient .RetryWaitMin = 1 * time .Second
65+ retryClient .RetryWaitMax = 5 * time .Second
66+ retryClient .Backoff = retryablehttp .DefaultBackoff
67+ retryClient .Logger = zap .L ()
68+
2469 // Initialize the GitLab client with the custom HTTP client
25- gitlabClient , err := gitlab .NewClient (gitlabToken , gitlab .WithBaseURL (gitlabURL ))
70+ gitlabClient , err := gitlab .NewClient (initArgs . GitlabToken , gitlab .WithBaseURL (initArgs . GitlabURL ), gitlab . WithHTTPClient ( retryClient . StandardClient () ))
2671 if err != nil {
2772 return nil , err
2873 }
@@ -31,7 +76,8 @@ func newGitlabInstance(gitlabURL string, gitlabToken string, timeout time.Durati
3176 Gitlab : gitlabClient ,
3277 Projects : make (map [string ]* gitlab.Project ),
3378 Groups : make (map [string ]* gitlab.Group ),
34- GraphQLClient : utils .NewGitlabGraphQLClient (gitlabToken , gitlabURL ),
79+ GraphQLClient : utils .NewGitlabGraphQLClient (initArgs .GitlabToken , initArgs .GitlabURL ),
80+ Role : initArgs .Role ,
3581 }
3682
3783 return gitlabInstance , nil
0 commit comments