commands/http: Made MultiFileReader thread safe
Matt Bell committed
Nov 18, 2014 at 01:15 UTC
57c48adfcff1237deafb2188db69e507dc41097f
1 file changed
+6
-1
commands/http/multifilereader.go
+6
-1
@@ -6,13 +6,13 @@ import (
6
"io"
7
"mime/multipart"
8
"net/textproto"
9
+ "sync"
10
11
cmds "github.com/jbenet/go-ipfs/commands"
12
)
13
14
// MultiFileReader reads from a `commands.File` (which can be a directory of files
15
// or a regular file) as HTTP multipart encoded data.
15
-// WARNING: Not thread-safe!
16
type MultiFileReader struct {
17
io.Reader
18
@@ -21,6 +21,7 @@ type MultiFileReader struct {
21
buf bytes.Buffer
22
mpWriter *multipart.Writer
23
closed bool
24
+ mutex *sync.Mutex
25
26
// if true, the data will be type 'multipart/form-data'
27
// if false, the data will be type 'multipart/mixed'
@@ -34,6 +35,7 @@ func NewMultiFileReader(file cmds.File, form bool) *MultiFileReader {
35
mfr := &MultiFileReader{
36
files: file,
37
form: form,
38
+ mutex: &sync.Mutex{},
39
}
40
mfr.mpWriter = multipart.NewWriter(&mfr.buf)
41
@@ -41,6 +43,9 @@ func NewMultiFileReader(file cmds.File, form bool) *MultiFileReader {
43
}
44
45
func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
46
+ mfr.mutex.Lock()
47
+ defer mfr.mutex.Unlock()
48
+
49
// if we are closed and the buffer is flushed, end reading
50
if mfr.closed && mfr.buf.Len() == 0 {
51
return 0, io.EOF