Skip to content

Commit

Permalink
feat(service): support basic github pat to avoid rate-limit (#477)
Browse files Browse the repository at this point in the history
Because

- avoid rate-limit for unauthenticated requests

This commit

- add optional config for github PAT
  • Loading branch information
heiruwu committed Dec 20, 2023
1 parent dba3ed6 commit 45931ca
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
10 changes: 8 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ type ServerConfig struct {
InferenceBackend string `koanf:"inferencebackend"`
}

type Github struct {
PatEnabled bool `koanf:"patenabled"`
Pat string `koanf:"pat"`
}

// DatabaseConfig related to database
type DatabaseConfig struct {
Username string `koanf:"username"`
Expand Down Expand Up @@ -86,8 +91,8 @@ type CacheConfig struct {
RedisOptions redis.Options `koanf:"redisoptions"`
}
Model struct {
Enabled bool `koanf:"enabled"`
CacheDir string `koanf:"cache_dir"`
Enabled bool `koanf:"enabled"`
CacheDir string `koanf:"cache_dir"`
}
}

Expand Down Expand Up @@ -140,6 +145,7 @@ type LogConfig struct {
// AppConfig defines
type AppConfig struct {
Server ServerConfig `koanf:"server"`
Github Github `koanf:"github"`
Database DatabaseConfig `koanf:"database"`
TritonServer TritonServerConfig `koanf:"tritonserver"`
RayServer RayServerConfig `koanf:"rayserver"`
Expand Down
3 changes: 3 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ server:
maxworkflowretry: 3
maxactivityretry: 1
inferencebackend: ray
github:
patenabled: false
pat:
database:
username: postgres
password: password
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/public_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ func createGitHubModel(service service.Service, ctx context.Context, req *modelP
githubInfo, err = utils.GetGitHubRepoInfo(modelConfig.Repository)
if err != nil {
span.SetStatus(1, "Invalid GitHub Info")
return &modelPB.CreateUserModelResponse{}, status.Errorf(codes.InvalidArgument, "Invalid GitHub Info")
return &modelPB.CreateUserModelResponse{}, status.Errorf(codes.InvalidArgument, fmt.Sprintf("Invalid Github info: %s", err))
}
if len(githubInfo.Tags) == 0 {
span.SetStatus(1, "There is no tag in GitHub repository")
Expand Down
1 change: 1 addition & 0 deletions pkg/ray/ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (r *ray) ModelReady(ctx context.Context, modelName string, modelInstance st
logger.Error(err.Error())
return nil, err
}
defer resp.Body.Close()

var applicationStatus rayserver.GetApplicationStatus
err = json.NewDecoder(resp.Body).Decode(&applicationStatus)
Expand Down
40 changes: 35 additions & 5 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ func GitHubClone(dir string, instanceConfig datamodel.GitHubModelConfiguration,
}
if !isWithLargeFile || isWithLargeFile && !config.Config.Cache.Model.Enabled {
if !strings.HasPrefix(urlRepo, "https://github.com") {
urlRepo = "https://github.com/" + urlRepo
if config.Config.Github.PatEnabled {
urlRepo = fmt.Sprintf("https://%s@github.com/%s", config.Config.Github.Pat, urlRepo)
fmt.Println(urlRepo)
} else {
urlRepo = fmt.Sprintf("https://github.com/%s", urlRepo)
}
}
if !strings.HasSuffix(urlRepo, ".git") {
urlRepo = urlRepo + ".git"
Expand Down Expand Up @@ -279,11 +284,23 @@ func GetGitHubRepoInfo(repo string) (*GitHubInfo, error) {
return &GitHubInfo{}, fmt.Errorf("invalid repo URL")
}

resp, err := http.Get(fmt.Sprintf("https://api.github.com/repos/%v", repo))
repoRequest, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://api.github.com/repos/%v", repo), nil)
if err != nil {
return &GitHubInfo{}, err
}
body, err := io.ReadAll(resp.Body)
if config.Config.Github.PatEnabled {
repoRequest.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.Config.Github.Pat))
}
repoResp, err := http.DefaultClient.Do(repoRequest)
if err != nil {
return &GitHubInfo{}, err
}
defer repoResp.Body.Close()
if repoResp.StatusCode != http.StatusOK {
return &GitHubInfo{}, fmt.Errorf(repoResp.Status)
}

body, err := io.ReadAll(repoResp.Body)
if err != nil {
return &GitHubInfo{}, err
}
Expand All @@ -292,11 +309,24 @@ func GetGitHubRepoInfo(repo string) (*GitHubInfo, error) {
if err != nil {
return &GitHubInfo{}, err
}
resp, err = http.Get(fmt.Sprintf("https://api.github.com/repos/%v/tags", repo))

tagRequest, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://api.github.com/repos/%v/tags", repo), nil)
if err != nil {
return &GitHubInfo{}, err
}
body, err = io.ReadAll(resp.Body)
if config.Config.Github.PatEnabled {
tagRequest.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.Config.Github.Pat))
}
tagResp, err := http.DefaultClient.Do(tagRequest)
if err != nil {
return &GitHubInfo{}, err
}
defer tagResp.Body.Close()
if tagResp.StatusCode != http.StatusOK {
return &GitHubInfo{}, fmt.Errorf(tagResp.Status)
}

body, err = io.ReadAll(tagResp.Body)
if err != nil {
return &GitHubInfo{}, err
}
Expand Down

0 comments on commit 45931ca

Please sign in to comment.