Skip to content

Commit

Permalink
use deadline within http transport
Browse files Browse the repository at this point in the history
  • Loading branch information
Asim committed Jul 28, 2016
1 parent 5b034ba commit e7903c6
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions transport/http_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type httpTransportClient struct {
}

type httpTransportSocket struct {
ht *httpTransport
r chan *http.Request
conn net.Conn
once sync.Once
Expand All @@ -51,6 +52,7 @@ type httpTransportSocket struct {
}

type httpTransportListener struct {
ht *httpTransport
listener net.Listener
}

Expand Down Expand Up @@ -144,6 +146,11 @@ func (h *httpTransportClient) Send(m *Message) error {
}
h.Unlock()

// set deadline if its greater than 0
if h.ht.opts.Deadline > time.Duration(0) {
h.conn.SetDeadline(time.Now().Add(h.ht.opts.Deadline))
}

return req.Write(h.conn)
}

Expand All @@ -163,6 +170,11 @@ func (h *httpTransportClient) Recv(m *Message) error {
return io.EOF
}

// set deadline if its greater than 0
if h.ht.opts.Deadline > time.Duration(0) {
h.conn.SetDeadline(time.Now().Add(h.ht.opts.Deadline))
}

rsp, err := http.ReadResponse(h.buff, r)
if err != nil {
return err
Expand Down Expand Up @@ -212,6 +224,11 @@ func (h *httpTransportSocket) Recv(m *Message) error {
return errors.New("message passed in is nil")
}

// set deadline if its greater than 0
if h.ht.opts.Deadline > time.Duration(0) {
h.conn.SetDeadline(time.Now().Add(h.ht.opts.Deadline))
}

r, err := http.ReadRequest(h.buff)
if err != nil {
return err
Expand Down Expand Up @@ -271,6 +288,11 @@ func (h *httpTransportSocket) Send(m *Message) error {
default:
}

// set deadline if its greater than 0
if h.ht.opts.Deadline > time.Duration(0) {
h.conn.SetDeadline(time.Now().Add(h.ht.opts.Deadline))
}

return rsp.Write(h.conn)
}

Expand Down Expand Up @@ -337,6 +359,7 @@ func (h *httpTransportListener) Accept(fn func(Socket)) error {
}

sock := &httpTransportSocket{
ht: h.ht,
conn: c,
buff: bufio.NewReader(c),
r: make(chan *http.Request, 1),
Expand Down Expand Up @@ -444,6 +467,7 @@ func (h *httpTransport) Listen(addr string, opts ...ListenOption) (Listener, err
}

return &httpTransportListener{
ht: h,
listener: l,
}, nil
}
Expand Down

0 comments on commit e7903c6

Please sign in to comment.