Commit a5b35530 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Update gitaly stream and dial library functions

parent cdabb988
...@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ...@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in
copies or substantial portions of the Software. all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
SOFTWARE. THE SOFTWARE.
This diff is collapsed.
...@@ -23,8 +23,8 @@ func Dial(rawAddress string, connOpts []grpc.DialOption) (*grpc.ClientConn, erro ...@@ -23,8 +23,8 @@ func Dial(rawAddress string, connOpts []grpc.DialOption) (*grpc.ClientConn, erro
} }
connOpts = append(connOpts, connOpts = append(connOpts,
grpc.WithDialer(func(a string, _ time.Duration) (net.Conn, error) { grpc.WithDialer(func(a string, timeout time.Duration) (net.Conn, error) {
return net.Dial(network, a) return net.DialTimeout(network, a, timeout)
})) }))
conn, err := grpc.Dial(addr, connOpts...) conn, err := grpc.Dial(addr, connOpts...)
if err != nil { if err != nil {
......
// Package streamio contains wrappers intended for turning gRPC streams
// that send/receive messages with a []byte field into io.Writers and
// io.Readers.
//
package streamio package streamio
import ( import (
"io" "io"
"os"
"strconv"
) )
func init() {
bufSize64, err := strconv.ParseInt(os.Getenv("GITALY_STREAMIO_WRITE_BUFFER_SIZE"), 10, 32)
if err == nil && bufSize64 > 0 {
WriteBufferSize = int(bufSize64)
}
}
// NewReader turns receiver into an io.Reader. Errors from the receiver // NewReader turns receiver into an io.Reader. Errors from the receiver
// function are passed on unmodified. This means receiver should emit // function are passed on unmodified. This means receiver should emit
// io.EOF when done. // io.EOF when done.
...@@ -29,16 +42,95 @@ func (rr *receiveReader) Read(p []byte) (int, error) { ...@@ -29,16 +42,95 @@ func (rr *receiveReader) Read(p []byte) (int, error) {
return n, nil return n, nil
} }
// NewWriter turns sender into an io.Writer. The number of 'bytes // WriteTo implements io.WriterTo.
// written' reported back is always len(p). func (rr *receiveReader) WriteTo(w io.Writer) (int64, error) {
var written int64
// Deal with left-over state in rr.data and rr.err, if any
if len(rr.data) > 0 {
n, err := w.Write(rr.data)
written += int64(n)
if err != nil {
return written, err
}
}
if rr.err != nil {
return written, rr.err
}
// Consume the response stream
var errRead, errWrite error
var n int
var buf []byte
for errWrite == nil && errRead != io.EOF {
buf, errRead = rr.receiver()
if errRead != nil && errRead != io.EOF {
return written, errRead
}
if len(buf) > 0 {
n, errWrite = w.Write(buf)
written += int64(n)
}
}
return written, errWrite
}
// NewWriter turns sender into an io.Writer. The sender callback will
// receive []byte arguments of length at most WriteBufferSize.
func NewWriter(sender func(p []byte) error) io.Writer { func NewWriter(sender func(p []byte) error) io.Writer {
return &sendWriter{sender: sender} return &sendWriter{sender: sender}
} }
// WriteBufferSize is the largest []byte that Write() will pass to its
// underlying send function. This value can be changed at runtime using
// the GITALY_STREAMIO_WRITE_BUFFER_SIZE environment variable.
var WriteBufferSize = 128 * 1024
type sendWriter struct { type sendWriter struct {
sender func([]byte) error sender func([]byte) error
} }
func (sw *sendWriter) Write(p []byte) (int, error) { func (sw *sendWriter) Write(p []byte) (int, error) {
return len(p), sw.sender(p) var sent int
for len(p) > 0 {
chunkSize := len(p)
if chunkSize > WriteBufferSize {
chunkSize = WriteBufferSize
}
if err := sw.sender(p[:chunkSize]); err != nil {
return sent, err
}
sent += chunkSize
p = p[chunkSize:]
}
return sent, nil
}
// ReadFrom implements io.ReaderFrom.
func (sw *sendWriter) ReadFrom(r io.Reader) (int64, error) {
var nRead int64
buf := make([]byte, WriteBufferSize)
var errRead, errSend error
for errSend == nil && errRead != io.EOF {
var n int
n, errRead = r.Read(buf)
nRead += int64(n)
if errRead != nil && errRead != io.EOF {
return nRead, errRead
}
if n > 0 {
errSend = sw.sender(buf[:n])
}
}
return nRead, errSend
} }
...@@ -199,24 +199,26 @@ ...@@ -199,24 +199,26 @@
{ {
"checksumSHA1": "dUHJbKas746n5fLzlwxHb6FOCxs=", "checksumSHA1": "dUHJbKas746n5fLzlwxHb6FOCxs=",
"path": "gitlab.com/gitlab-org/gitaly/auth", "path": "gitlab.com/gitlab-org/gitaly/auth",
"revision": "e4f8d3d14cc3fe673cb511fb4d0189b68a158ccd", "revision": "95a198aef54c42fd8e84c62acc63f0cd620864b3",
"revisionTime": "2017-06-30T12:58:40Z", "revisionTime": "2018-01-18T11:33:00Z",
"version": "v0.14.0", "version": "v0.71.0",
"versionExact": "v0.14.0" "versionExact": "v0.71.0"
}, },
{ {
"checksumSHA1": "qlzYmQ21XX/voiKWHDWUZ3lybGQ=", "checksumSHA1": "s53Qjro9ZHX0DV2tCYQXLO2FHqI=",
"path": "gitlab.com/gitlab-org/gitaly/client", "path": "gitlab.com/gitlab-org/gitaly/client",
"revision": "871c07b758cf13892972089e1357c5a28bfc40d9", "revision": "95a198aef54c42fd8e84c62acc63f0cd620864b3",
"revisionTime": "2017-09-22T12:56:31Z" "revisionTime": "2018-01-18T11:33:00Z",
"version": "v0.71.0",
"versionExact": "v0.71.0"
}, },
{ {
"checksumSHA1": "sdUF3j5MaQ9Tjc2dGHqc/toQxyk=", "checksumSHA1": "mifcYH0qXpoPkX5KzXoM3mterWQ=",
"path": "gitlab.com/gitlab-org/gitaly/streamio", "path": "gitlab.com/gitlab-org/gitaly/streamio",
"revision": "e4f8d3d14cc3fe673cb511fb4d0189b68a158ccd", "revision": "95a198aef54c42fd8e84c62acc63f0cd620864b3",
"revisionTime": "2017-06-30T12:58:40Z", "revisionTime": "2018-01-18T11:33:00Z",
"version": "v0.14.0", "version": "v0.71.0",
"versionExact": "v0.14.0" "versionExact": "v0.71.0"
}, },
{ {
"checksumSHA1": "9jjO5GjLa0XF/nfWihF02RoH4qc=", "checksumSHA1": "9jjO5GjLa0XF/nfWihF02RoH4qc=",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment