Buffer buffers Body up to BufferSize. Calling this function blocks. Call with goroutine.
()
| 113 | // Buffer buffers Body up to BufferSize. |
| 114 | // Calling this function blocks. Call with goroutine. |
| 115 | func (m *Migration) Buffer() error { |
| 116 | var op errors.Op = "migrate.Migration.Buffer" |
| 117 | if m.Body == nil { |
| 118 | return nil |
| 119 | } |
| 120 | |
| 121 | m.StartedBuffering = time.Now() |
| 122 | |
| 123 | b := bufio.NewReaderSize(m.Body, int(m.BufferSize)) |
| 124 | |
| 125 | // start reading from body, peek won't move the read pointer though |
| 126 | // poor man's solution? |
| 127 | if _, err := b.Peek(int(m.BufferSize)); err != nil && err != io.EOF { |
| 128 | return errors.E(op, err) |
| 129 | } |
| 130 | |
| 131 | m.FinishedBuffering = time.Now() |
| 132 | |
| 133 | // write to bufferWriter, this will block until |
| 134 | // something starts reading from m.Buffer |
| 135 | n, err := b.WriteTo(m.bufferWriter) |
| 136 | if err != nil { |
| 137 | return errors.E(op, err) |
| 138 | } |
| 139 | |
| 140 | m.FinishedReading = time.Now() |
| 141 | m.BytesRead = n |
| 142 | |
| 143 | // close bufferWriter so Buffer knows that there is no |
| 144 | // more data coming |
| 145 | m.bufferWriter.Close() |
| 146 | |
| 147 | // it's safe to close the Body too |
| 148 | m.Body.Close() |
| 149 | |
| 150 | return nil |
| 151 | } |
no test coverage detected