You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.1 KiB
75 lines
2.1 KiB
![]()
9 years ago
|
/*
|
||
|
* Minio Cloud Storage, (C) 2016 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 main
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
![]()
9 years ago
|
// waitCloser implements a Closer that blocks until released, this
|
||
|
// prevents the writer closing one end of a pipe while making sure
|
||
|
// that all data is written and committed to disk on the end.
|
||
|
// Additionally this also implements Write().
|
||
|
type waitCloser struct {
|
||
|
wg *sync.WaitGroup // Waitgroup for atomicity.
|
||
|
writer io.WriteCloser // Embedded writer.
|
||
![]()
9 years ago
|
}
|
||
|
|
||
|
// Write to the underlying writer.
|
||
![]()
9 years ago
|
func (b *waitCloser) Write(data []byte) (int, error) {
|
||
![]()
9 years ago
|
return b.writer.Write(data)
|
||
![]()
9 years ago
|
}
|
||
|
|
||
|
// Close blocks until another goroutine calls Release(error). Returns
|
||
|
// error code if either writer fails or Release is called with an error.
|
||
![]()
9 years ago
|
func (b *waitCloser) Close() error {
|
||
![]()
9 years ago
|
err := b.writer.Close()
|
||
![]()
9 years ago
|
b.wg.Wait()
|
||
![]()
9 years ago
|
return err
|
||
![]()
9 years ago
|
}
|
||
|
|
||
![]()
8 years ago
|
// CloseWithError closes the writer; subsequent read to the read
|
||
|
// half of the pipe will return the error err.
|
||
|
func (b *waitCloser) CloseWithError(err error) error {
|
||
|
w, ok := b.writer.(*io.PipeWriter)
|
||
|
if ok {
|
||
|
return w.CloseWithError(err)
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// release the Close, causing it to unblock. Only call this
|
||
![]()
9 years ago
|
// once. Calling it multiple times results in a panic.
|
||
![]()
9 years ago
|
func (b *waitCloser) release() {
|
||
|
b.wg.Done()
|
||
![]()
9 years ago
|
return
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// newWaitCloser creates a new write closer that must be
|
||
![]()
9 years ago
|
// released by the read consumer.
|
||
![]()
9 years ago
|
func newWaitCloser(writer io.WriteCloser) *waitCloser {
|
||
![]()
9 years ago
|
// Wait group for the go-routine.
|
||
|
wg := &sync.WaitGroup{}
|
||
|
// Add to the wait group to wait for.
|
||
|
wg.Add(1)
|
||
![]()
9 years ago
|
return &waitCloser{
|
||
|
wg: wg,
|
||
|
writer: writer,
|
||
![]()
9 years ago
|
}
|
||
|
}
|