add support for streaming requests. cleanup watcher initilisation

This commit is contained in:
Asim
2015-06-01 18:55:27 +01:00
parent fa2c27b64f
commit 09c784d294
25 changed files with 729 additions and 384 deletions

View File

@@ -1,6 +1,7 @@
package transport
import (
"bufio"
"bytes"
"errors"
"io/ioutil"
@@ -9,34 +10,27 @@ import (
"net/url"
)
type headerRoundTripper struct {
r http.RoundTripper
}
type httpTransport struct {
client *http.Client
}
type httpTransport struct{}
type httpTransportClient struct {
ht *httpTransport
addr string
ht *httpTransport
addr string
conn net.Conn
buff *bufio.Reader
dialOpts dialOptions
r chan *http.Request
}
type httpTransportSocket struct {
r *http.Request
w http.ResponseWriter
r *http.Request
conn net.Conn
}
type httpTransportListener struct {
listener net.Listener
}
func (t *headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("X-Client-Version", "1.0")
return t.r.RoundTrip(r)
}
func (h *httpTransportClient) Send(m *Message) (*Message, error) {
func (h *httpTransportClient) Send(m *Message) error {
header := make(http.Header)
for k, v := range m.Header {
@@ -49,7 +43,7 @@ func (h *httpTransportClient) Send(m *Message) (*Message, error) {
reqB,
}
hreq := &http.Request{
req := &http.Request{
Method: "POST",
URL: &url.URL{
Scheme: "http",
@@ -61,15 +55,26 @@ func (h *httpTransportClient) Send(m *Message) (*Message, error) {
Host: h.addr,
}
rsp, err := h.ht.client.Do(hreq)
h.r <- req
return req.Write(h.conn)
}
func (h *httpTransportClient) Recv(m *Message) error {
var r *http.Request
if !h.dialOpts.stream {
r = <-h.r
}
rsp, err := http.ReadResponse(h.buff, r)
if err != nil {
return nil, err
return err
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, err
return err
}
mr := &Message{
@@ -85,11 +90,12 @@ func (h *httpTransportClient) Send(m *Message) (*Message, error) {
}
}
return mr, nil
*m = *mr
return nil
}
func (h *httpTransportClient) Close() error {
return nil
return h.conn.Close()
}
func (h *httpTransportSocket) Recv(m *Message) error {
@@ -101,7 +107,7 @@ func (h *httpTransportSocket) Recv(m *Message) error {
if err != nil {
return err
}
h.r.Body.Close()
mr := &Message{
Header: make(map[string]string),
Body: b,
@@ -120,16 +126,30 @@ func (h *httpTransportSocket) Recv(m *Message) error {
}
func (h *httpTransportSocket) Send(m *Message) error {
for k, v := range m.Header {
h.w.Header().Set(k, v)
b := bytes.NewBuffer(m.Body)
defer b.Reset()
rsp := &http.Response{
Header: h.r.Header,
Body: &buffer{b},
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: int64(len(m.Body)),
// Request: h.r,
}
_, err := h.w.Write(m.Body)
return err
for k, v := range m.Header {
rsp.Header.Set(k, v)
}
return rsp.Write(h.conn)
}
func (h *httpTransportSocket) Close() error {
return nil
// TODO: fix this
return h.conn.Close()
}
func (h *httpTransportListener) Addr() string {
@@ -143,9 +163,14 @@ func (h *httpTransportListener) Close() error {
func (h *httpTransportListener) Accept(fn func(Socket)) error {
srv := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, _, err := w.(http.Hijacker).Hijack()
if err != nil {
return
}
fn(&httpTransportSocket{
r: r,
w: w,
conn: conn,
r: r,
})
}),
}
@@ -153,10 +178,25 @@ func (h *httpTransportListener) Accept(fn func(Socket)) error {
return srv.Serve(h.listener)
}
func (h *httpTransport) Dial(addr string) (Client, error) {
func (h *httpTransport) Dial(addr string, opts ...DialOption) (Client, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
var dopts dialOptions
for _, opt := range opts {
opt(&dopts)
}
return &httpTransportClient{
ht: h,
addr: addr,
ht: h,
addr: addr,
conn: conn,
buff: bufio.NewReader(conn),
dialOpts: dopts,
r: make(chan *http.Request, 1),
}, nil
}
@@ -172,8 +212,5 @@ func (h *httpTransport) Listen(addr string) (Listener, error) {
}
func newHttpTransport(addrs []string, opt ...Option) *httpTransport {
client := &http.Client{}
client.Transport = &headerRoundTripper{http.DefaultTransport}
return &httpTransport{client: client}
return &httpTransport{}
}

View File

@@ -17,6 +17,8 @@ type ntport struct {
type ntportClient struct {
conn *nats.Conn
addr string
id string
sub *nats.Subscription
}
type ntportSocket struct {
@@ -30,26 +32,32 @@ type ntportListener struct {
exit chan bool
}
func (n *ntportClient) Send(m *transport.Message) (*transport.Message, error) {
func (n *ntportClient) Send(m *transport.Message) error {
b, err := json.Marshal(m)
if err != nil {
return nil, err
return err
}
rsp, err := n.conn.Request(n.addr, b, time.Second*10)
return n.conn.PublishRequest(n.addr, n.id, b)
}
func (n *ntportClient) Recv(m *transport.Message) error {
rsp, err := n.sub.NextMsg(time.Second * 10)
if err != nil {
return nil, err
return err
}
var mr *transport.Message
if err := json.Unmarshal(rsp.Data, &mr); err != nil {
return nil, err
return err
}
return mr, nil
*m = *mr
return nil
}
func (n *ntportClient) Close() error {
n.sub.Unsubscribe()
n.conn.Close()
return nil
}
@@ -102,7 +110,7 @@ func (n *ntportListener) Accept(fn func(transport.Socket)) error {
return s.Unsubscribe()
}
func (n *ntport) Dial(addr string) (transport.Client, error) {
func (n *ntport) Dial(addr string, opts ...transport.DialOption) (transport.Client, error) {
cAddr := nats.DefaultURL
if len(n.addrs) > 0 && strings.HasPrefix(n.addrs[0], "nats://") {
@@ -114,9 +122,17 @@ func (n *ntport) Dial(addr string) (transport.Client, error) {
return nil, err
}
id := nats.NewInbox()
sub, err := c.SubscribeSync(id)
if err != nil {
return nil, err
}
return &ntportClient{
conn: c,
addr: addr,
id: id,
sub: sub,
}, nil
}

View File

@@ -15,18 +15,21 @@ import (
type rmqtport struct {
conn *rabbitMQConn
addrs []string
}
type rmqtportClient struct {
once sync.Once
rt *rmqtport
addr string
replyTo string
sync.Mutex
inflight map[string]chan amqp.Delivery
}
type rmqtportClient struct {
rt *rmqtport
addr string
corId string
reply chan amqp.Delivery
}
type rmqtportSocket struct {
conn *rabbitMQConn
d *amqp.Delivery
@@ -37,86 +40,34 @@ type rmqtportListener struct {
addr string
}
func (r *rmqtportClient) init() {
<-r.rt.conn.Init()
if err := r.rt.conn.Channel.DeclareReplyQueue(r.replyTo); err != nil {
return
}
deliveries, err := r.rt.conn.Channel.ConsumeQueue(r.replyTo)
if err != nil {
return
}
go func() {
for delivery := range deliveries {
go r.handle(delivery)
}
}()
}
func (r *rmqtportClient) handle(delivery amqp.Delivery) {
ch := r.getReq(delivery.CorrelationId)
if ch == nil {
return
}
select {
case ch <- delivery:
default:
}
}
func (r *rmqtportClient) putReq(id string) chan amqp.Delivery {
r.Lock()
ch := make(chan amqp.Delivery, 1)
r.inflight[id] = ch
r.Unlock()
return ch
}
func (r *rmqtportClient) getReq(id string) chan amqp.Delivery {
r.Lock()
defer r.Unlock()
if ch, ok := r.inflight[id]; ok {
delete(r.inflight, id)
return ch
}
return nil
}
func (r *rmqtportClient) Send(m *transport.Message) (*transport.Message, error) {
r.once.Do(r.init)
func (r *rmqtportClient) Send(m *transport.Message) error {
if !r.rt.conn.IsConnected() {
return nil, errors.New("Not connected to AMQP")
return errors.New("Not connected to AMQP")
}
id, err := uuid.NewV4()
if err != nil {
return nil, err
}
replyChan := r.putReq(id.String())
headers := amqp.Table{}
for k, v := range m.Header {
headers[k] = v
}
message := amqp.Publishing{
CorrelationId: id.String(),
CorrelationId: r.corId,
Timestamp: time.Now().UTC(),
Body: m.Body,
ReplyTo: r.replyTo,
ReplyTo: r.rt.replyTo,
Headers: headers,
}
if err := r.rt.conn.Publish("micro", r.addr, message); err != nil {
r.getReq(id.String())
return nil, err
return err
}
return nil
}
func (r *rmqtportClient) Recv(m *transport.Message) error {
select {
case d := <-replyChan:
case d := <-r.reply:
mr := &transport.Message{
Header: make(map[string]string),
Body: d.Body,
@@ -126,13 +77,15 @@ func (r *rmqtportClient) Send(m *transport.Message) (*transport.Message, error)
mr.Header[k] = fmt.Sprintf("%v", v)
}
return mr, nil
*m = *mr
return nil
case <-time.After(time.Second * 10):
return nil, errors.New("timed out")
return errors.New("timed out")
}
}
func (r *rmqtportClient) Close() error {
r.rt.popReq(r.corId)
return nil
}
@@ -202,17 +155,68 @@ func (r *rmqtportListener) Accept(fn func(transport.Socket)) error {
return nil
}
func (r *rmqtport) Dial(addr string) (transport.Client, error) {
func (r *rmqtport) putReq(id string) chan amqp.Delivery {
r.Lock()
ch := make(chan amqp.Delivery, 1)
r.inflight[id] = ch
r.Unlock()
return ch
}
func (r *rmqtport) getReq(id string) chan amqp.Delivery {
r.Lock()
defer r.Unlock()
if ch, ok := r.inflight[id]; ok {
return ch
}
return nil
}
func (r *rmqtport) popReq(id string) {
r.Lock()
defer r.Unlock()
if _, ok := r.inflight[id]; ok {
delete(r.inflight, id)
}
}
func (r *rmqtport) init() {
<-r.conn.Init()
if err := r.conn.Channel.DeclareReplyQueue(r.replyTo); err != nil {
return
}
deliveries, err := r.conn.Channel.ConsumeQueue(r.replyTo)
if err != nil {
return
}
go func() {
for delivery := range deliveries {
go r.handle(delivery)
}
}()
}
func (r *rmqtport) handle(delivery amqp.Delivery) {
ch := r.getReq(delivery.CorrelationId)
if ch == nil {
return
}
ch <- delivery
}
func (r *rmqtport) Dial(addr string, opts ...transport.DialOption) (transport.Client, error) {
id, err := uuid.NewV4()
if err != nil {
return nil, err
}
r.once.Do(r.init)
return &rmqtportClient{
rt: r,
addr: addr,
inflight: make(map[string]chan amqp.Delivery),
replyTo: fmt.Sprintf("replyTo-%s", id.String()),
rt: r,
addr: addr,
corId: id.String(),
reply: r.putReq(id.String()),
}, nil
}
@@ -232,8 +236,12 @@ func (r *rmqtport) Listen(addr string) (transport.Listener, error) {
}
func NewTransport(addrs []string, opt ...transport.Option) transport.Transport {
id, _ := uuid.NewV4()
return &rmqtport{
conn: newRabbitMQConn("", addrs),
addrs: addrs,
conn: newRabbitMQConn("", addrs),
addrs: addrs,
replyTo: id.String(),
inflight: make(map[string]chan amqp.Delivery),
}
}

View File

@@ -1,6 +1,7 @@
package transport
type Message struct {
Id string
Header map[string]string
Body []byte
}
@@ -12,7 +13,8 @@ type Socket interface {
}
type Client interface {
Send(*Message) (*Message, error)
Recv(*Message) error
Send(*Message) error
Close() error
}
@@ -23,24 +25,36 @@ type Listener interface {
}
type Transport interface {
Dial(addr string) (Client, error)
Dial(addr string, opts ...DialOption) (Client, error)
Listen(addr string) (Listener, error)
}
type options struct{}
type dialOptions struct {
stream bool
}
type Option func(*options)
type DialOption func(*dialOptions)
var (
DefaultTransport Transport = newHttpTransport([]string{})
)
func WithStream() DialOption {
return func(o *dialOptions) {
o.stream = true
}
}
func NewTransport(addrs []string, opt ...Option) Transport {
return newHttpTransport(addrs, opt...)
}
func Dial(addr string) (Client, error) {
return DefaultTransport.Dial(addr)
func Dial(addr string, opts ...DialOption) (Client, error) {
return DefaultTransport.Dial(addr, opts...)
}
func Listen(addr string) (Listener, error) {