From 157bf10372524b7c7c42a2c7dd104fcedf409fa1 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 20 Jun 2015 15:54:41 -0700 Subject: [PATCH] use session now saves in-transit information, remove usage of sync.Pool not needed --- cmd-cp.go | 24 +++++++++++++++--------- session.go | 23 ++++++----------------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/cmd-cp.go b/cmd-cp.go index 2ae0faf8..5f04e091 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -116,7 +116,8 @@ func doCopyInRoutineSession(cURLs cpURLs, bar *barSend, cpQueue chan bool, errCh if err := doCopy(cURLs, bar); err != nil { errCh <- err } - <-cpQueue // Signal that this copy routine is done. + // Signal that this copy routine is done. + <-cpQueue // store files which have finished copying s.Files = append(s.Files, cURLs.SourceContent.Name) } @@ -135,7 +136,7 @@ func doPrepareCopyURLs(sourceURLs []string, targetURL string, bar barSend, lock } } -func trap(sid string) { +func trap(done chan bool) { // Go signal notification works by sending `os.Signal` // values on a channel. sigs := make(chan os.Signal, 1) @@ -148,17 +149,15 @@ func trap(sid string) { // When it gets one it'll then notify the program // that it can finish. <-sigs - if err := saveSession(sid); err != nil { - console.Fatalln(err) - } - console.Fatal("") + done <- true } func doCopySession(sourceURLs []string, targetURL string, bar barSend, s *sessionV1) <-chan error { errCh := make(chan error) + done := make(chan bool) go func(sourceURLs []string, targetURL string, bar barSend, errCh chan error, s *sessionV1) { defer close(errCh) - go trap(s.SessionID) + go trap(done) var lock countlock.Locker if !globalQuietFlag { @@ -178,7 +177,7 @@ func doCopySession(sourceURLs []string, targetURL string, bar barSend, s *sessio for cURLs := range prepareCopyURLs(sourceURLs, targetURL) { if cURLs.Error != nil { - errCh <- cURLs.Error + errCh <- iodine.New(cURLs.Error, nil) continue } cpQueue <- true // Wait for existing pool to drain. @@ -190,6 +189,13 @@ func doCopySession(sourceURLs []string, targetURL string, bar barSend, s *sessio } wg.Wait() // wait for the go routines to complete }(sourceURLs, targetURL, bar, errCh, s) + if <-done { + close(done) + if err := saveSession(s); err != nil { + console.Fatalln(iodine.ToError(err)) + } + os.Exit(0) + } return errCh } @@ -281,7 +287,7 @@ func runCopyCmd(ctx *cli.Context) { bar = newCpBar() } - for err := range doCopyCmd(sourceURLs, targetURL, bar) { + for err := range doCopySession(sourceURLs, targetURL, bar, s) { if err != nil { console.Errors(ErrorMessage{ Message: "Failed with", diff --git a/session.go b/session.go index 77fde69b..39aee85a 100644 --- a/session.go +++ b/session.go @@ -18,12 +18,10 @@ package main import ( "crypto/rand" - "errors" "fmt" "io" "os" "path/filepath" - "sync" "github.com/minio/mc/pkg/quick" "github.com/minio/minio/pkg/iodine" @@ -36,9 +34,6 @@ type sessionV1 struct { Files []string } -// cached variables should *NEVER* be accessed directly from outside this file. -var scache sync.Pool - func isSessionDirExists() bool { sdir, err := getSessionDir() if err != nil { @@ -119,16 +114,16 @@ func getSessionFile(sid string) (string, error) { } // save a session -func saveSession(sid string) error { - sessionFile, err := getSessionFile(sid) +func saveSession(s *sessionV1) error { + sessionFile, err := getSessionFile(s.SessionID) if err != nil { return err } - // Use previously cached config. - if v := scache.Get(); v != nil { - return v.(quick.Config).Save(sessionFile) + qs, err := quick.New(s) + if err != nil { + return err } - return errors.New("No cached session found") + return qs.Save(sessionFile) } // provides a new session @@ -140,7 +135,6 @@ func newSession() (*sessionV1, error) { if err := writeSession(qs); err != nil { return nil, iodine.New(err, nil) } - scache.Put(qs) return qs.Data().(*sessionV1), nil } @@ -157,10 +151,6 @@ func loadSession(sid string) (*sessionV1, error) { if err != nil { return nil, iodine.New(err, nil) } - // Cached in private global variable. - if v := scache.Get(); v != nil { // Use previously cached config. - return v.(quick.Config).Data().(*sessionV1), nil - } s := new(sessionV1) s.Version = mcCurrentSessionVersion // map of command and files copied @@ -174,7 +164,6 @@ func loadSession(sid string) (*sessionV1, error) { if err != nil { return nil, iodine.New(err, nil) } - scache.Put(qs) return qs.Data().(*sessionV1), nil }