Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP client #1

Merged
merged 27 commits into from
Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5f20404
Add networks conns
9seconds Dec 17, 2018
16483e7
Add connection hub
9seconds Dec 17, 2018
92dda1b
Rework into dialer
9seconds Dec 17, 2018
8bbcca8
Add dialer
9seconds Dec 17, 2018
be7e428
Remove possible deadlock on conns obsolete
9seconds Dec 18, 2018
40eb96b
Add simple dialer
9seconds Dec 18, 2018
40b8844
Rename dialer to client
9seconds Dec 18, 2018
4fc0a65
Small fix for pooled dialer
9seconds Dec 18, 2018
b7475fa
Add simplified client
9seconds Dec 18, 2018
35b33e5
Add client readers
9seconds Dec 22, 2018
9d916ca
Do not write numbers to chunked reader
9seconds Dec 22, 2018
acc4cee
Inverse chunked reader logic
9seconds Dec 22, 2018
e69a19a
Add pool of buffered readers
9seconds Dec 22, 2018
fdacad8
Add tests for countReader
9seconds Dec 22, 2018
c515e49
Add tests for simple reader
9seconds Dec 23, 2018
2a3032d
Add tests for chunked reader
9seconds Dec 23, 2018
dbc6cd4
Refactor readers
9seconds Dec 23, 2018
215ae19
Add alwaysClose to readers
9seconds Dec 23, 2018
eb779c0
Set correct reader type to client
9seconds Dec 23, 2018
ef4622d
Use pooled buffered readers for client
9seconds Dec 23, 2018
0e9a066
Some fixes for http client
9seconds Dec 24, 2018
65012f4
Add tests for client
9seconds Dec 25, 2018
1c56237
Integration of client into the codebase
9seconds Dec 25, 2018
5066c3a
Fix lint errors
9seconds Dec 25, 2018
f95bf67
Add documentation to the client
9seconds Dec 25, 2018
ae8f2f4
Add rest of the documentation
9seconds Dec 25, 2018
8b89d2e
Update golangci-lint version
9seconds Dec 25, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add alwaysClose to readers
  • Loading branch information
9seconds committed Dec 23, 2018
commit 215ae194c7c7bcfefd146ff708f17cae6a02fba8
25 changes: 15 additions & 10 deletions client/readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ func (c *countReader) Read(b []byte) (int, error) {
}

type readerCloser struct {
dialer Dialer
addr string
closed bool
dialer Dialer
addr string
closed bool
alwaysClose bool
}

func (r *readerCloser) releaseConn(conn net.Conn) {
if !r.closed {
if r.alwaysClose {
r.closeConn(conn)
} else if !r.closed {
r.dialer.Release(conn, r.addr)
r.closed = true
}
Expand Down Expand Up @@ -224,20 +227,21 @@ func (c *chunkedReader) consumeCRLF() error {
return nil
}

func newSimpleReader(addr string, conn net.Conn, dialer Dialer, contentLength int64) *simpleReader {
func newSimpleReader(addr string, conn net.Conn, dialer Dialer, contentLength int64, closeOnEOF bool) *simpleReader {
return &simpleReader{
countReader: countReader{
conn: conn,
bytesLeft: contentLength,
},
readerCloser: readerCloser{
dialer: dialer,
addr: addr,
dialer: dialer,
addr: addr,
alwaysClose: closeOnEOF,
},
}
}

func newChunkedReader(addr string, conn net.Conn, dialer Dialer) *chunkedReader {
func newChunkedReader(addr string, conn net.Conn, dialer Dialer, closeOnEOF bool) *chunkedReader {
bufferedReader := poolBufferedReader.Get().(*bufio.Reader)
bufferedReader.Reset(conn)

Expand All @@ -246,8 +250,9 @@ func newChunkedReader(addr string, conn net.Conn, dialer Dialer) *chunkedReader
conn: bufferedReader,
},
readerCloser: readerCloser{
dialer: dialer,
addr: addr,
dialer: dialer,
addr: addr,
alwaysClose: closeOnEOF,
},
conn: conn,
bufferedReader: bufferedReader,
Expand Down
38 changes: 19 additions & 19 deletions client/readers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (suite *SimpleReaderTestSuite) TestReadByte() {
suite.writer.Write([]byte{1, 2, 3, 4, 5})
}()

reader := newSimpleReader("addr", suite.conn, suite.dialer, 1)
reader := newSimpleReader("addr", suite.conn, suite.dialer, 1, false)
arr := make([]byte, 10)

n, err := reader.Read(arr)
Expand All @@ -141,7 +141,7 @@ func (suite *SimpleReaderTestSuite) TestReadAll() {
suite.writer.Write([]byte{1, 2, 3, 4, 5})
}()

reader := newSimpleReader("addr", suite.conn, suite.dialer, 5)
reader := newSimpleReader("addr", suite.conn, suite.dialer, 5, false)
arr := make([]byte, 10)

n, err := reader.Read(arr)
Expand All @@ -162,7 +162,7 @@ func (suite *SimpleReaderTestSuite) TestReadAllChunks() {
suite.writer.Write([]byte{1, 2, 3, 4, 5})
}()

reader := newSimpleReader("addr", suite.conn, suite.dialer, 5)
reader := newSimpleReader("addr", suite.conn, suite.dialer, 5, false)
arr := make([]byte, 2)

n, err := reader.Read(arr)
Expand Down Expand Up @@ -194,7 +194,7 @@ func (suite *SimpleReaderTestSuite) TestReadClose() {
suite.writer.Write([]byte{1, 2, 3, 4, 5})
}()

reader := newSimpleReader("addr", suite.conn, suite.dialer, 5)
reader := newSimpleReader("addr", suite.conn, suite.dialer, 5, false)
arr := make([]byte, 2)

n, err := reader.Read(arr)
Expand All @@ -221,7 +221,7 @@ func (suite *SimpleReaderTestSuite) TestReadCloseEOF() {
suite.writer.Write([]byte{1, 2, 3, 4, 5})
}()

reader := newSimpleReader("addr", suite.conn, suite.dialer, 5)
reader := newSimpleReader("addr", suite.conn, suite.dialer, 5, false)
arr := make([]byte, 2)

n, err := reader.Read(arr)
Expand All @@ -244,7 +244,7 @@ func (suite *SimpleReaderTestSuite) TestDoubleClose() {
suite.dialer.On("NotifyClosed", "addr")
suite.conn.On("Close").Return(nil)

reader := newSimpleReader("addr", suite.conn, suite.dialer, 5)
reader := newSimpleReader("addr", suite.conn, suite.dialer, 5, false)
suite.Nil(reader.Close())
suite.Nil(reader.Close())

Expand All @@ -262,7 +262,7 @@ func (suite *SimpleReaderTestSuite) TestCloseAfterError() {
suite.writer.Write([]byte{1, 2, 3, 4, 5})
}()

reader := newSimpleReader("addr", suite.conn, suite.dialer, 5)
reader := newSimpleReader("addr", suite.conn, suite.dialer, 5, false)
arr := make([]byte, 2)
n, err := reader.Read(arr)
suite.Equal(n, 2)
Expand All @@ -289,7 +289,7 @@ func (suite *SimpleReaderTestSuite) TestCloseAfterEOF() {
suite.writer.Write([]byte{1, 2, 3, 4, 5})
}()

reader := newSimpleReader("addr", suite.conn, suite.dialer, 5)
reader := newSimpleReader("addr", suite.conn, suite.dialer, 5, false)
arr := make([]byte, 2)
n, err := reader.Read(arr)
suite.Equal(n, 2)
Expand Down Expand Up @@ -328,7 +328,7 @@ func (suite *ChunkedReaderTestSuite) TestReferenceExample() {
suite.writer.Write([]byte(data))
}()

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
arr := make([]byte, 100)
n, err := reader.Read(arr)
suite.Equal(n, 4)
Expand Down Expand Up @@ -358,7 +358,7 @@ func (suite *ChunkedReaderTestSuite) TestMDNExample() {
suite.writer.Write([]byte(data))
}()

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
arr := make([]byte, 100)
n, err := reader.Read(arr)
suite.Equal(n, 7)
Expand Down Expand Up @@ -388,7 +388,7 @@ func (suite *ChunkedReaderTestSuite) TestBigNumber() {
suite.writer.Write([]byte(data))
}()

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
arr := make([]byte, 100)
n, err := reader.Read(arr)
suite.Equal(n, 26)
Expand All @@ -409,7 +409,7 @@ func (suite *ChunkedReaderTestSuite) TestTooBigNumber() {
suite.writer.Write([]byte(data))
}()

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
arr := make([]byte, 100)
n, err := reader.Read(arr)
suite.Equal(n, 0)
Expand All @@ -430,7 +430,7 @@ func (suite *ChunkedReaderTestSuite) TestNoNumber() {
suite.writer.Write([]byte(data))
}()

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
arr := make([]byte, 100)
n, err := reader.Read(arr)
suite.Equal(n, 0)
Expand All @@ -451,7 +451,7 @@ func (suite *ChunkedReaderTestSuite) TestCorruptedNumber() {
suite.writer.Write([]byte(data))
}()

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
arr := make([]byte, 100)
n, err := reader.Read(arr)
suite.Equal(n, 0)
Expand All @@ -472,7 +472,7 @@ func (suite *ChunkedReaderTestSuite) TestCorruptedNumber2() {
suite.writer.Write([]byte(data))
}()

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
arr := make([]byte, 100)
n, err := reader.Read(arr)
suite.Equal(n, 0)
Expand All @@ -493,7 +493,7 @@ func (suite *ChunkedReaderTestSuite) TestCorruptedCRLF() {
suite.writer.Write([]byte(data))
}()

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
arr := make([]byte, 100)
n, err := reader.Read(arr)
suite.Equal(n, 5)
Expand All @@ -514,7 +514,7 @@ func (suite *ChunkedReaderTestSuite) TestSlowReader() {
}()
consumed := []byte{}

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
arr := make([]byte, 2)
var err error
var n int
Expand All @@ -535,7 +535,7 @@ func (suite *ChunkedReaderTestSuite) TestCloseUnexpected() {
suite.writer.Write([]byte(data))
}()

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
arr := make([]byte, 100)
reader.Read(arr)

Expand All @@ -558,7 +558,7 @@ func (suite *ChunkedReaderTestSuite) TestDoubleClose() {
suite.dialer.On("NotifyClosed", "addr")
suite.conn.On("Close").Return(nil)

reader := newChunkedReader("addr", suite.conn, suite.dialer)
reader := newChunkedReader("addr", suite.conn, suite.dialer, false)
suite.Nil(reader.Close())
suite.Nil(reader.Close())

Expand Down