use session now saves in-transit information, remove usage of sync.Pool not needed

This commit is contained in:
Harshavardhana
2015-06-20 15:54:49 -07:00
parent 8cff0fe99b
commit 157bf10372
2 changed files with 21 additions and 26 deletions
+15 -9
View File
@@ -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",
+6 -17
View File
@@ -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
}