diff --git a/cmd-cp.go b/cmd-cp.go index 517b5ff5..671248e5 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -27,6 +27,7 @@ import ( "github.com/minio/cli" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/countlock" + "github.com/minio/mc/pkg/yielder" "github.com/minio/minio/pkg/iodine" ) @@ -85,12 +86,11 @@ func doCopy(sourceURL string, targetURL string, bar *barSend) error { switch globalQuietFlag { case true: console.Infoln(fmt.Sprintf("ā€˜%s’ -> ā€˜%s’", sourceURL, targetURL)) - newReader = reader + newReader = yielder.NewReader(reader) default: // set up progress - newReader = bar.NewProxyReader(reader) + newReader = bar.NewProxyReader(yielder.NewReader(reader)) } - runtime.Gosched() // Yield more CPU err = putTarget(targetURL, length, newReader) if err != nil { if !globalQuietFlag { @@ -161,9 +161,6 @@ func doCopyCmd(sourceURLs []string, targetURL string, bar barSend) <-chan error errCh <- cpURLs.Error continue } - - runtime.Gosched() // Yield more CPU time to progress-bar builder. - cpQueue <- true // Wait for existing pool to drain. wg.Add(1) // keep track of all the goroutines if !globalQuietFlag { diff --git a/cmd-sync.go b/cmd-sync.go index ed231030..cf17743d 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -27,6 +27,7 @@ import ( "github.com/minio/cli" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/countlock" + "github.com/minio/mc/pkg/yielder" "github.com/minio/minio/pkg/iodine" ) @@ -90,12 +91,11 @@ func doSync(sURLs syncURLs, bar *barSend, syncQueue chan bool, errCh chan error, var newReader io.Reader switch globalQuietFlag { case true: - newReader = reader + newReader = yielder.NewReader(reader) default: // set up progress - newReader = bar.NewProxyReader(reader) + newReader = bar.NewProxyReader(yielder.NewReader(reader)) } - runtime.Gosched() // Yield more CPU for err := range putTargets(targetURLs, length, newReader) { if err != nil { if !globalQuietFlag { @@ -139,9 +139,6 @@ func doSyncCmd(sourceURL string, targetURLs []string, bar barSend) <-chan error errCh <- iodine.New(sURLs.Error, nil) continue } - - runtime.Gosched() // Yield more CPU time to progress-bar builder. - syncQueue <- true wg.Add(1) if !globalQuietFlag { diff --git a/pkg/yielder/yielder.go b/pkg/yielder/yielder.go new file mode 100644 index 00000000..fe2e2463 --- /dev/null +++ b/pkg/yielder/yielder.go @@ -0,0 +1,54 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package yielder relinquishes control to other go routines more +// frequently in the middle of busy I/O activity +package yielder + +import ( + "io" + "runtime" +) + +// NewReader returns an initialized yielding proxy reader. +func NewReader(reader io.Reader) *Reader { + return &Reader{reader, true} +} + +// Reader implements io.Reader compatible scheduler. +type Reader struct { + io.Reader + enabled bool +} + +// Read yields CPU to other go routines. Useful in making low-priority I/O routines. +func (r Reader) Read(p []byte) (n int, err error) { + if r.enabled { + runtime.Gosched() + } + n, err = r.Reader.Read(p) + return +} + +// Enable turns on the yielder. +func (r *Reader) Enable() { + r.enabled = true +} + +// Disable turns off the yielder. +func (r *Reader) Disable() { + r.enabled = false +} diff --git a/pkg/yielder/yielder_test.go b/pkg/yielder/yielder_test.go new file mode 100644 index 00000000..48c43395 --- /dev/null +++ b/pkg/yielder/yielder_test.go @@ -0,0 +1,40 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package yielder + +import ( + "bytes" + "testing" + + . "github.com/minio/check" +) + +func Test(t *testing.T) { TestingT(t) } + +type MySuite struct{} + +var _ = Suite(&MySuite{}) + +func (s *MySuite) TestYielder(c *C) { + r := NewReader(bytes.NewReader([]byte("hello"))) + c.Assert(r, Not(IsNil)) + + p := make([]byte, 5) + n, err := r.Read(p) + c.Assert(err, IsNil) + c.Assert(n, Equals, 5) +}