Skip to content

Commit

Permalink
resolve conflicts between clone and update
Browse files Browse the repository at this point in the history
  • Loading branch information
faceair committed Jun 18, 2018
1 parent ab7a38b commit 9769a0f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

<p align="center">
<a href="https://raw.githubusercontent.com/faceair/gotit/master/LICENSE"><img src="https://img.shields.io/hexpm/l/plug.svg" alt="License"></a>
<a href="https://travis-ci.org/faceair/gotit"><img src="https://img.shields.io/travis/faceair/gotit/master.svg?t=1529297795" alt="Travis branch"></a>
<a href="https://coveralls.io/github/faceair/gotit?branch=master"><img src="https://coveralls.io/repos/github/faceair/gotit/badge.svg?branch=master&t=1529297795" alt="Coverage Status"></a>
<a href="https://goreportcard.com/report/github.com/faceair/gotit"><img src="https://goreportcard.com/badge/github.com/faceair/gotit?t=1529297795" alt="Go Report Card"></a>
<a href="https://travis-ci.org/faceair/gotit"><img src="https://img.shields.io/travis/faceair/gotit/master.svg?t=1529303885" alt="Travis branch"></a>
<a href="https://coveralls.io/github/faceair/gotit?branch=master"><img src="https://coveralls.io/repos/github/faceair/gotit/badge.svg?branch=master&t=1529303885" alt="Coverage Status"></a>
<a href="https://goreportcard.com/report/github.com/faceair/gotit"><img src="https://goreportcard.com/badge/github.com/faceair/gotit?t=1529303885" alt="Go Report Card"></a>
<a href="https://godoc.org/github.com/faceair/gotit"><img src="https://godoc.org/github.com/faceair/gotit?status.svg" alt="GoDoc"></a>
</p>

Expand Down Expand Up @@ -65,7 +65,7 @@ HTTPS_PROXY=http://127.0.0.1:8080 GIT_SSL_NO_VERIFY=true go get -v -insecure git

#### other

Todo
TODO

## FAQ

Expand Down
74 changes: 49 additions & 25 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ func NewServer(gopath string) *Server {

g := &Server{
gopath: gopath,
queue: make(chan string, 1024),
queue: make(chan *updateTask, 1024),
}
go g.updateLoop()
go g.cloneLoop()
return g
}

// Server implement interface of betproxy.Client
type Server struct {
gopath string
queue chan string
queue chan *updateTask
upTime sync.Map
}

Expand All @@ -62,17 +62,16 @@ func (g *Server) Do(req *http.Request) (*http.Response, error) {

case "/info/refs":
if !g.checkRepo(repoPath) {
err := g.clone(repoPath)
if err != nil {
return nil, err
task := newUpdateTask(repoPath)
g.queue <- task
<-task.Done()
} else {
select {
case g.queue <- newUpdateTask(repoPath):
default:
}
}

select {
case g.queue <- repoPath:
default:
}

service := strings.Replace(req.FormValue("service"), "git-", "", 1)
args := []string{service, "--stateless-rpc", "--advertise-refs", "."}
refs, err := g.cmd(repoPath, args...).Output()
Expand Down Expand Up @@ -114,9 +113,30 @@ func (g *Server) Do(req *http.Request) (*http.Response, error) {
return betproxy.HTTPError(http.StatusBadRequest, "url not match", req), nil
}

func (g *Server) clone(repoPath string) error {
g.upTime.Store(repoPath, time.Now())
func (g *Server) cloneLoop() {
for {
task := <-g.queue
if g.shouldUpdate(task.repoPath) {
if err := g.clone(task.repoPath); err != nil {
log.Printf("Clone Failed: %s", err.Error())
}
}
close(task.Done())
}
}

func (g *Server) shouldUpdate(repoPath string) bool {
now := time.Now()
if ut, ok := g.upTime.Load(repoPath); ok {
if ut.(time.Time).Sub(now) < time.Hour {
return false
}
}
g.upTime.Store(repoPath, now)
return true
}

func (g *Server) clone(repoPath string) error {
logger := NewLogBuffer("Go Get")
cmd := exec.Command("go", []string{"get", "-d", "-f", "-u", "-v", repoPath}...)
cmd.Dir = g.gopath
Expand All @@ -132,18 +152,6 @@ func (g *Server) clone(repoPath string) error {
return err
}

func (g *Server) updateLoop() {
for {
repoPath := <-g.queue
if ut, ok := g.upTime.Load(repoPath); ok {
if ut.(time.Time).Sub(time.Now()) < time.Minute {
continue
}
}
g.clone(repoPath)
}
}

func (g *Server) checkRepo(dir string) bool {
_, err := os.Stat(fmt.Sprintf("%s/src/%s/.git", g.gopath, dir))
return err == nil
Expand Down Expand Up @@ -198,3 +206,19 @@ func (l *LogBuffer) Write(p []byte) (n int, err error) {
func (l *LogBuffer) String() string {
return string(l.buffer)
}

func newUpdateTask(repoPath string) *updateTask {
return &updateTask{
repoPath: repoPath,
done: make(chan struct{}),
}
}

type updateTask struct {
repoPath string
done chan struct{}
}

func (t *updateTask) Done() chan struct{} {
return t.done
}

0 comments on commit 9769a0f

Please sign in to comment.