Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable wait time between each HTTP request #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/gitjacker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"strings"
"time"

"github.com/sirupsen/logrus"

Expand All @@ -19,11 +20,13 @@ import (

var outputDir string
var verbose bool
var wait time.Duration

func main() {

rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", verbose, "Enable verbose logging")
rootCmd.Flags().StringVarP(&outputDir, "output-dir", "o", outputDir, "Directory to output retrieved git repository - defaults to a temporary directory")
rootCmd.Flags().DurationVarP(&wait, "wait", "w", wait, "Wait this long between HTTP requests (to avoid throttling)")

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down Expand Up @@ -89,7 +92,7 @@ Output Dir: %s
_ = tml.Printf("\n<yellow>Gitjacking in progress...")
}

summary, err := gitjacker.New(u, outputDir).Run()
summary, err := gitjacker.New(u, outputDir, wait).Run()
if err != nil {
if !verbose {
fmt.Printf("\x1b[2K\r")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ require (
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v1.0.0
golang.org/x/time v0.3.0 // indirect
gopkg.in/src-d/go-git.v4 v4.13.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
11 changes: 10 additions & 1 deletion internal/pkg/gitjacker/retriever.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gitjacker

import (
"context"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -14,6 +15,7 @@ import (
"crypto/tls"

"github.com/sirupsen/logrus"
"golang.org/x/time/rate"
)

var paths = []string{
Expand All @@ -37,6 +39,7 @@ type retriever struct {
baseURL *url.URL
outputDir string
http *http.Client
limiter *rate.Limiter
downloaded map[string]bool
summary Summary
}
Expand Down Expand Up @@ -88,7 +91,7 @@ type Branch struct {
Remote string
}

func New(target *url.URL, outputDir string) *retriever {
func New(target *url.URL, outputDir string, wait time.Duration) *retriever {

relative, _ := url.Parse(".git/")
target = target.ResolveReference(relative)
Expand All @@ -103,6 +106,7 @@ func New(target *url.URL, outputDir string) *retriever {
Timeout: time.Second * 10,
Transport: customTransport,
},
limiter: rate.NewLimiter(rate.Every(wait), 1),
downloaded: make(map[string]bool),
summary: Summary{
OutputDirectory: outputDir,
Expand Down Expand Up @@ -172,6 +176,11 @@ func (r *retriever) downloadFile(path string) error {
return err
}

err = r.limiter.Wait(context.Background())
if err != nil {
return err
}

absolute := r.baseURL.ResolveReference(relative)
resp, err := r.http.Get(absolute.String())
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions vendor/golang.org/x/time/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/time/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading