fix the tunnel execution

This commit is contained in:
Asim Aslam 2019-08-11 18:11:33 +01:00
parent 113d87d855
commit d0d729a789
5 changed files with 34 additions and 6 deletions

View File

@ -16,6 +16,7 @@ type rpcRequest struct {
body []byte body []byte
rawBody interface{} rawBody interface{}
stream bool stream bool
first bool
} }
type rpcMessage struct { type rpcMessage struct {
@ -54,9 +55,9 @@ func (r *rpcRequest) Body() interface{} {
func (r *rpcRequest) Read() ([]byte, error) { func (r *rpcRequest) Read() ([]byte, error) {
// got a body // got a body
if r.body != nil { if r.first {
b := r.body b := r.body
r.body = nil r.first = false
return b, nil return b, nil
} }

View File

@ -146,6 +146,7 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
body: msg.Body, body: msg.Body,
socket: sock, socket: sock,
stream: true, stream: true,
first: true,
} }
// internal response // internal response

View File

@ -128,6 +128,9 @@ func (t *tun) process() {
// send the message via the interface // send the message via the interface
t.RLock() t.RLock()
if len(t.links) == 0 {
log.Debugf("Zero links to send to")
}
for _, link := range t.links { for _, link := range t.links {
log.Debugf("Sending %+v to %s", newMsg, link.Remote()) log.Debugf("Sending %+v to %s", newMsg, link.Remote())
link.Send(newMsg) link.Send(newMsg)
@ -146,6 +149,7 @@ func (t *tun) listen(link transport.Socket, listener bool) {
msg := new(transport.Message) msg := new(transport.Message)
err := link.Recv(msg) err := link.Recv(msg)
if err != nil { if err != nil {
log.Debugf("Tunnel link %s receive error: %v", link.Remote(), err)
return return
} }
@ -185,12 +189,12 @@ func (t *tun) listen(link transport.Socket, listener bool) {
// no socket in existence // no socket in existence
if !exists { if !exists {
log.Debugf("Skipping") log.Debugf("Tunnel skipping no socket exists")
// drop it, we don't care about // drop it, we don't care about
// messages we don't know about // messages we don't know about
continue continue
} }
log.Debugf("Using socket %s %s", s.id, s.session) log.Debugf("Tunnel using socket %s %s", s.id, s.session)
// is the socket closed? // is the socket closed?
select { select {
@ -260,6 +264,7 @@ func (t *tun) connect() error {
// delete the link // delete the link
defer func() { defer func() {
log.Debugf("Deleting connection from %s", sock.Remote())
t.Lock() t.Lock()
delete(t.links, id) delete(t.links, id)
t.Unlock() t.Unlock()

View File

@ -2,6 +2,8 @@ package tunnel
import ( import (
"io" "io"
"github.com/micro/go-micro/util/log"
) )
type tunListener struct { type tunListener struct {
@ -31,6 +33,7 @@ func (t *tunListener) process() {
case m := <-t.socket.recv: case m := <-t.socket.recv:
// get a socket // get a socket
sock, ok := conns[m.session] sock, ok := conns[m.session]
log.Debugf("Tunnel listener received id %s session %s exists: %t", m.id, m.session, ok)
if !ok { if !ok {
// create a new socket session // create a new socket session
sock = &socket{ sock = &socket{
@ -50,12 +53,14 @@ func (t *tunListener) process() {
// save the socket // save the socket
conns[m.session] = sock conns[m.session] = sock
sock.recv <- m
// send to accept chan // send to accept chan
select { select {
case <-t.closed: case <-t.closed:
return return
case t.accept <- sock: case t.accept <- sock:
continue
} }
} }
@ -64,6 +69,7 @@ func (t *tunListener) process() {
case <-sock.closed: case <-sock.closed:
delete(conns, m.session) delete(conns, m.session)
case sock.recv <- m: case sock.recv <- m:
log.Debugf("Tunnel listener sent to recv chan id %s session %s", m.id, m.session)
} }
} }
} }
@ -92,7 +98,7 @@ func (t *tunListener) Accept() (Conn, error) {
return nil, io.EOF return nil, io.EOF
case <-t.tunClosed: case <-t.tunClosed:
// close the listener when the tunnel closes // close the listener when the tunnel closes
close(t.closed) t.Close()
return nil, io.EOF return nil, io.EOF
// wait for a new connection // wait for a new connection
case c, ok := <-t.accept: case c, ok := <-t.accept:

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"github.com/micro/go-micro/transport" "github.com/micro/go-micro/transport"
"github.com/micro/go-micro/util/log"
) )
// socket is our pseudo socket for transport.Socket // socket is our pseudo socket for transport.Socket
@ -59,8 +60,21 @@ func (s *socket) Send(m *transport.Message) error {
default: default:
// no op // no op
} }
// make copy
data := &transport.Message{
Header: make(map[string]string),
Body: m.Body,
}
for k, v := range m.Header {
data.Header[k] = v
}
// append to backlog // append to backlog
s.send <- &message{id: s.id, session: s.session, data: m} msg := &message{id: s.id, session: s.session, data: data}
log.Debugf("Appending %+v to send backlog", msg)
s.send <- msg
return nil return nil
} }
@ -73,6 +87,7 @@ func (s *socket) Recv(m *transport.Message) error {
} }
// recv from backlog // recv from backlog
msg := <-s.recv msg := <-s.recv
log.Debugf("Received %+v from recv backlog", msg)
// set message // set message
*m = *msg.data *m = *msg.data
// return nil // return nil