Skip to content

Commit 3a3c518

Browse files
committed
Choose the storoge provider with the least latency on initial configuration
1 parent 34fbdda commit 3a3c518

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

cmd/git-gitopia/lfs/init.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ func InitCommand() *cobra.Command {
5959
return err
6060
}
6161
}
62+
gitServerHost, _ := config.GitConfigGet(config.GitopiaConfigGitServerHostOption)
63+
if gitServerHost == "" {
64+
gitServerHost = api.GetBestGitServerHost(grpcHost)
65+
if gitServerHost != "" {
66+
if err := api.SetConfiguredGitServerHost(gitServerHost); err != nil {
67+
return err
68+
}
69+
}
70+
}
6271

6372
grpcConn, err := grpc.Dial(grpcHost,
6473
grpc.WithTransportCredentials(insecure.NewCredentials()),
@@ -81,7 +90,11 @@ func InitCommand() *cobra.Command {
8190
}
8291

8392
remoteRepository := *res.Repository
84-
lfsURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, remoteRepository.Id)
93+
gitServerHost, _ = config.GitConfigGet(config.GitopiaConfigGitServerHostOption)
94+
if gitServerHost == "" {
95+
gitServerHost = config.GitServerHost
96+
}
97+
lfsURL := fmt.Sprintf("%v/%v.git", gitServerHost, remoteRepository.Id)
8598

8699
c = core.GitCommand("git", "config",
87100
fmt.Sprintf("--file=%s", lfsConfigPath),

cmd/git-remote-gitopia/gitopia.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ func (h *GitopiaHandler) Initialize(remote *core.Remote) error {
6868
}
6969
}
7070

71+
gitServerHost, _ := config.GitConfigGet(config.GitopiaConfigGitServerHostOption)
72+
if gitServerHost == "" {
73+
gitServerHost = api.GetBestGitServerHost(grpcHost)
74+
if gitServerHost != "" {
75+
if err := api.SetConfiguredGitServerHost(gitServerHost); err != nil {
76+
return err
77+
}
78+
}
79+
}
80+
7181
h.grpcConn, err = grpc.Dial(grpcHost, grpc.WithTransportCredentials(insecure.NewCredentials()))
7282
if err != nil {
7383
return err
@@ -138,7 +148,11 @@ func (h *GitopiaHandler) List(remote *core.Remote, forPush bool) ([]string, erro
138148
}
139149

140150
func (h *GitopiaHandler) Fetch(remote *core.Remote, refsToFetch []core.RefToFetch) error {
141-
remoteURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, h.remoteRepository.Id)
151+
gitServerHost, _ := config.GitConfigGet(config.GitopiaConfigGitServerHostOption)
152+
if gitServerHost == "" {
153+
gitServerHost = config.GitServerHost
154+
}
155+
remoteURL := fmt.Sprintf("%v/%v.git", gitServerHost, h.remoteRepository.Id)
142156

143157
if !remote.Force {
144158
args := []string{
@@ -214,7 +228,11 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
214228
return nil, fmt.Errorf("fatal: you don't have write permissions to this repository")
215229
}
216230

217-
remoteURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, h.remoteRepository.Id)
231+
gitServerHost, _ := config.GitConfigGet(config.GitopiaConfigGitServerHostOption)
232+
if gitServerHost == "" {
233+
gitServerHost = config.GitServerHost
234+
}
235+
remoteURL := fmt.Sprintf("%v/%v.git", gitServerHost, h.remoteRepository.Id)
218236

219237
var newRemoteRefSha string
220238
var setBranches []gitopiatypes.MsgMultiSetBranch_Branch

core/api/gitserver.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
"time"
6+
7+
"github.com/gitopia/git-remote-gitopia/core"
8+
)
9+
10+
func checkHttpHostLatency(host string) time.Duration {
11+
start := time.Now()
12+
_, err := http.Get(host)
13+
if err != nil {
14+
return time.Hour
15+
}
16+
return time.Since(start)
17+
}
18+
19+
func GetBestGitServerHost(grpcHost string) string {
20+
providers := GetActiveStorageProviders(grpcHost)
21+
if len(providers) == 0 {
22+
return ""
23+
}
24+
25+
var bestHost string
26+
bestLatency := time.Hour
27+
28+
for _, p := range providers {
29+
latency := checkHttpHostLatency(p.ApiUrl)
30+
if latency < bestLatency {
31+
bestHost = p.ApiUrl
32+
bestLatency = latency
33+
}
34+
}
35+
36+
return bestHost
37+
}
38+
39+
func SetConfiguredGitServerHost(host string) error {
40+
cmd := core.GitCommand("git", "config", "--global", "gitopia.gitServerHost", host)
41+
return cmd.Run()
42+
}

core/api/grpc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
88
"github.com/gitopia/git-remote-gitopia/core"
9+
storagetypes "github.com/gitopia/gitopia/v6/x/storage/types"
910
"google.golang.org/grpc"
1011
"google.golang.org/grpc/credentials/insecure"
1112
)
@@ -50,3 +51,18 @@ func SetConfiguredGRPCHost(host string) error {
5051
cmd := core.GitCommand("git", "config", "--global", "gitopia.grpcHost", host)
5152
return cmd.Run()
5253
}
54+
55+
func GetActiveStorageProviders(host string) []storagetypes.Provider {
56+
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
57+
if err != nil {
58+
return nil
59+
}
60+
defer conn.Close()
61+
62+
client := storagetypes.NewQueryClient(conn)
63+
res, err := client.ActiveProviders(context.Background(), &storagetypes.QueryActiveProvidersRequest{})
64+
if err != nil {
65+
return nil
66+
}
67+
return res.Providers
68+
}

0 commit comments

Comments
 (0)