2017-01-01 18:39:05 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-03-03 12:28:44 +00:00
|
|
|
"context"
|
2021-01-10 14:48:10 +03:00
|
|
|
"fmt"
|
2017-01-01 18:39:05 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
2017-01-01 18:39:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Implements the streamer interface
|
|
|
|
type httpStream struct {
|
|
|
|
sync.RWMutex
|
|
|
|
address string
|
2021-01-10 14:48:10 +03:00
|
|
|
opts client.CallOptions
|
|
|
|
codec codec.Codec
|
2017-01-01 18:39:05 +00:00
|
|
|
context context.Context
|
|
|
|
header http.Header
|
|
|
|
seq uint64
|
|
|
|
closed chan bool
|
|
|
|
err error
|
|
|
|
conn net.Conn
|
|
|
|
reader *bufio.Reader
|
|
|
|
request client.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2021-01-10 14:48:10 +03:00
|
|
|
errShutdown = fmt.Errorf("connection is shut down")
|
2017-01-01 18:39:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (h *httpStream) isClosed() bool {
|
|
|
|
select {
|
|
|
|
case <-h.closed:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpStream) Context() context.Context {
|
|
|
|
return h.context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpStream) Request() client.Request {
|
|
|
|
return h.request
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:32:12 +00:00
|
|
|
func (h *httpStream) Response() client.Response {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-01 18:39:05 +00:00
|
|
|
func (h *httpStream) Send(msg interface{}) error {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if h.isClosed() {
|
|
|
|
h.err = errShutdown
|
|
|
|
return errShutdown
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
hreq, err := newRequest(h.address, h.request, h.codec, msg, h.opts)
|
2017-01-01 18:39:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
hreq.Header = h.header
|
2021-01-10 14:48:10 +03:00
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
return hreq.Write(h.conn)
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpStream) Recv(msg interface{}) error {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if h.isClosed() {
|
|
|
|
h.err = errShutdown
|
|
|
|
return errShutdown
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
hrsp, err := http.ReadResponse(h.reader, new(http.Request))
|
2017-01-01 18:39:05 +00:00
|
|
|
if err != nil {
|
2021-01-10 14:48:10 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
2021-01-10 14:48:10 +03:00
|
|
|
defer hrsp.Body.Close()
|
2017-01-01 18:39:05 +00:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
return parseRsp(h.context, hrsp, h.codec, msg, h.opts)
|
2017-01-01 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpStream) Error() error {
|
|
|
|
h.RLock()
|
|
|
|
defer h.RUnlock()
|
|
|
|
return h.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpStream) Close() error {
|
|
|
|
select {
|
|
|
|
case <-h.closed:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(h.closed)
|
|
|
|
return h.conn.Close()
|
|
|
|
}
|
|
|
|
}
|