use deadline within http transport

This commit is contained in:
Asim 2016-07-28 19:12:56 +01:00
parent 5b034ba253
commit e7903c65ce

View File

@ -42,6 +42,7 @@ type httpTransportClient struct {
} }
type httpTransportSocket struct { type httpTransportSocket struct {
ht *httpTransport
r chan *http.Request r chan *http.Request
conn net.Conn conn net.Conn
once sync.Once once sync.Once
@ -51,6 +52,7 @@ type httpTransportSocket struct {
} }
type httpTransportListener struct { type httpTransportListener struct {
ht *httpTransport
listener net.Listener listener net.Listener
} }
@ -144,6 +146,11 @@ func (h *httpTransportClient) Send(m *Message) error {
} }
h.Unlock() 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) return req.Write(h.conn)
} }
@ -163,6 +170,11 @@ func (h *httpTransportClient) Recv(m *Message) error {
return io.EOF 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) rsp, err := http.ReadResponse(h.buff, r)
if err != nil { if err != nil {
return err return err
@ -212,6 +224,11 @@ func (h *httpTransportSocket) Recv(m *Message) error {
return errors.New("message passed in is nil") 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) r, err := http.ReadRequest(h.buff)
if err != nil { if err != nil {
return err return err
@ -271,6 +288,11 @@ func (h *httpTransportSocket) Send(m *Message) error {
default: 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) return rsp.Write(h.conn)
} }
@ -337,6 +359,7 @@ func (h *httpTransportListener) Accept(fn func(Socket)) error {
} }
sock := &httpTransportSocket{ sock := &httpTransportSocket{
ht: h.ht,
conn: c, conn: c,
buff: bufio.NewReader(c), buff: bufio.NewReader(c),
r: make(chan *http.Request, 1), r: make(chan *http.Request, 1),
@ -444,6 +467,7 @@ func (h *httpTransport) Listen(addr string, opts ...ListenOption) (Listener, err
} }
return &httpTransportListener{ return &httpTransportListener{
ht: h,
listener: l, listener: l,
}, nil }, nil
} }