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
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
Refactor readers
  • Loading branch information
9seconds committed Dec 23, 2018
commit dbc6cd43fb3d58318ddd039d785821ef56b27b62
66 changes: 41 additions & 25 deletions client/readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,32 @@ func (c *countReader) Read(b []byte) (int, error) {
return n, err
}

type simpleReader struct {
countReader

type readerCloser struct {
dialer Dialer
addr string
closed bool
}

func (r *readerCloser) releaseConn(conn net.Conn) {
if !r.closed {
r.dialer.Release(conn, r.addr)
r.closed = true
}
}

func (r *readerCloser) closeConn(conn net.Conn) {
if !r.closed {
conn.Close()
r.dialer.NotifyClosed(r.addr)
r.closed = true
}
}

type simpleReader struct {
countReader
readerCloser
}

func (s *simpleReader) Read(b []byte) (int, error) {
if s.closed {
return 0, io.EOF
Expand All @@ -73,34 +91,28 @@ func (s *simpleReader) Read(b []byte) (int, error) {
if err == errCountReaderExhaust {
err = io.EOF
}

conn := s.countReader.conn.(net.Conn)
if err == io.EOF {
s.dialer.Release(s.countReader.conn.(net.Conn), s.addr)
s.closed = true
s.releaseConn(conn)
} else {
s.Close()
s.closeConn(conn)
}

return n, err
}

func (s *simpleReader) Close() error {
if !s.closed {
s.countReader.conn.(io.Closer).Close()
s.dialer.NotifyClosed(s.addr)
s.closed = true
}

s.closeConn(s.countReader.conn.(net.Conn))
return nil
}

type chunkedReader struct {
countReader
readerCloser

dialer Dialer
conn net.Conn
addr string
readData bool
closed bool
bufferedReader *bufio.Reader
}

Expand All @@ -125,16 +137,15 @@ func (c *chunkedReader) Read(b []byte) (int, error) {
c.Close()
}
}

return n, err
}

func (c *chunkedReader) Close() error {
if !c.closed {
poolBufferedReader.Put(c.bufferedReader)
c.conn.Close()
c.dialer.NotifyClosed(c.addr)
c.closed = true
}
c.closeConn(c.conn)

return nil
}
Expand All @@ -153,8 +164,7 @@ func (c *chunkedReader) prepareNextChunk() error {
if c.consumeCRLF() != nil {
c.Close()
} else {
c.dialer.Release(c.conn, c.addr)
c.closed = true
c.releaseConn(c.conn)
poolBufferedReader.Put(c.bufferedReader)
}
return io.EOF
Expand Down Expand Up @@ -220,8 +230,10 @@ func newSimpleReader(addr string, conn net.Conn, dialer Dialer, contentLength in
conn: conn,
bytesLeft: contentLength,
},
dialer: dialer,
addr: addr,
readerCloser: readerCloser{
dialer: dialer,
addr: addr,
},
}
}

Expand All @@ -230,10 +242,14 @@ func newChunkedReader(addr string, conn net.Conn, dialer Dialer) *chunkedReader
bufferedReader.Reset(conn)

return &chunkedReader{
countReader: countReader{conn: bufferedReader},
dialer: dialer,
countReader: countReader{
conn: bufferedReader,
},
readerCloser: readerCloser{
dialer: dialer,
addr: addr,
},
conn: conn,
addr: addr,
bufferedReader: bufferedReader,
}
}