Simplify ReadFileStream closer, make sure to flush all HTTP responses (#7374)

master
Harshavardhana 6 years ago committed by Nitish Tiwari
parent 1011d21416
commit 6702d23d52
  1. 1
      cmd/globals.go
  2. 20
      cmd/posix.go
  3. 7
      cmd/storage-rest-server.go

@ -57,7 +57,6 @@ const (
globalMinioDefaultStorageClass = "STANDARD" globalMinioDefaultStorageClass = "STANDARD"
globalWindowsOSName = "windows" globalWindowsOSName = "windows"
globalNetBSDOSName = "netbsd" globalNetBSDOSName = "netbsd"
globalSolarisOSName = "solaris"
globalMinioModeFS = "mode-server-fs" globalMinioModeFS = "mode-server-fs"
globalMinioModeXL = "mode-server-xl" globalMinioModeXL = "mode-server-xl"
globalMinioModeDistXL = "mode-server-distributed-xl" globalMinioModeDistXL = "mode-server-distributed-xl"

@ -232,7 +232,6 @@ func getDiskInfo(diskPath string) (di disk.Info, err error) {
var ignoreDiskFreeOS = []string{ var ignoreDiskFreeOS = []string{
globalWindowsOSName, globalWindowsOSName,
globalNetBSDOSName, globalNetBSDOSName,
globalSolarisOSName,
} }
// check if disk total has minimum required size. // check if disk total has minimum required size.
@ -943,20 +942,6 @@ func (s *posix) openFile(volume, path string, mode int) (f *os.File, err error)
return w, nil return w, nil
} }
// Just like io.LimitedReader but supports Close() to be compatible with io.ReadCloser that is
// returned by posix.ReadFileStream()
type posixLimitedReader struct {
io.LimitedReader
}
func (l *posixLimitedReader) Close() error {
c, ok := l.R.(io.Closer)
if !ok {
return errUnexpected
}
return c.Close()
}
// ReadFileStream - Returns the read stream of the file. // ReadFileStream - Returns the read stream of the file.
func (s *posix) ReadFileStream(volume, path string, offset, length int64) (io.ReadCloser, error) { func (s *posix) ReadFileStream(volume, path string, offset, length int64) (io.ReadCloser, error) {
var err error var err error
@ -1030,7 +1015,10 @@ func (s *posix) ReadFileStream(volume, path string, offset, length int64) (io.Re
if _, err = file.Seek(offset, io.SeekStart); err != nil { if _, err = file.Seek(offset, io.SeekStart); err != nil {
return nil, err return nil, err
} }
return &posixLimitedReader{io.LimitedReader{R: file, N: length}}, nil return struct {
io.Reader
io.Closer
}{Reader: io.LimitReader(file, length), Closer: file}, nil
} }
// CreateFile - creates the file. // CreateFile - creates the file.

@ -47,6 +47,7 @@ type storageRESTServer struct {
func (s *storageRESTServer) writeErrorResponse(w http.ResponseWriter, err error) { func (s *storageRESTServer) writeErrorResponse(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
w.(http.Flusher).Flush()
} }
// Authenticates storage client's requests and validates for skewed time. // Authenticates storage client's requests and validates for skewed time.
@ -93,6 +94,7 @@ func (s *storageRESTServer) GetInstanceID(w http.ResponseWriter, r *http.Request
} }
w.Header().Set("Content-Length", strconv.Itoa(len(s.instanceID))) w.Header().Set("Content-Length", strconv.Itoa(len(s.instanceID)))
w.Write([]byte(s.instanceID)) w.Write([]byte(s.instanceID))
w.(http.Flusher).Flush()
} }
// DiskInfoHandler - returns disk info. // DiskInfoHandler - returns disk info.
@ -268,6 +270,7 @@ func (s *storageRESTServer) ReadAllHandler(w http.ResponseWriter, r *http.Reques
} }
w.Header().Set("Content-Length", strconv.Itoa(len(buf))) w.Header().Set("Content-Length", strconv.Itoa(len(buf)))
w.Write(buf) w.Write(buf)
w.(http.Flusher).Flush()
} }
// ReadFileHandler - read section of a file. // ReadFileHandler - read section of a file.
@ -311,6 +314,7 @@ func (s *storageRESTServer) ReadFileHandler(w http.ResponseWriter, r *http.Reque
} }
w.Header().Set("Content-Length", strconv.Itoa(len(buf))) w.Header().Set("Content-Length", strconv.Itoa(len(buf)))
w.Write(buf) w.Write(buf)
w.(http.Flusher).Flush()
} }
// ReadFileHandler - read section of a file. // ReadFileHandler - read section of a file.
@ -331,6 +335,7 @@ func (s *storageRESTServer) ReadFileStreamHandler(w http.ResponseWriter, r *http
s.writeErrorResponse(w, err) s.writeErrorResponse(w, err)
return return
} }
rc, err := s.storage.ReadFileStream(volume, filePath, int64(offset), int64(length)) rc, err := s.storage.ReadFileStream(volume, filePath, int64(offset), int64(length))
if err != nil { if err != nil {
s.writeErrorResponse(w, err) s.writeErrorResponse(w, err)
@ -338,7 +343,9 @@ func (s *storageRESTServer) ReadFileStreamHandler(w http.ResponseWriter, r *http
} }
defer rc.Close() defer rc.Close()
w.Header().Set("Content-Length", strconv.Itoa(length)) w.Header().Set("Content-Length", strconv.Itoa(length))
io.Copy(w, rc) io.Copy(w, rc)
w.(http.Flusher).Flush()
} }
// ListDirHandler - list a directory. // ListDirHandler - list a directory.

Loading…
Cancel
Save