2019-05-24 19:15:59 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime/debug"
|
|
|
|
|
2019-07-07 12:37:34 +03:00
|
|
|
"github.com/micro/go-micro/network/transport"
|
|
|
|
pb "github.com/micro/go-micro/network/transport/grpc/proto"
|
2019-06-03 21:30:43 +03:00
|
|
|
"github.com/micro/go-micro/util/log"
|
2019-05-24 19:15:59 +03:00
|
|
|
"google.golang.org/grpc/peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// microTransport satisfies the pb.TransportServer inteface
|
|
|
|
type microTransport struct {
|
|
|
|
addr string
|
|
|
|
fn func(transport.Socket)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *microTransport) Stream(ts pb.Transport_StreamServer) error {
|
|
|
|
sock := &grpcTransportSocket{
|
|
|
|
stream: ts,
|
|
|
|
local: m.addr,
|
|
|
|
}
|
|
|
|
|
|
|
|
p, ok := peer.FromContext(ts.Context())
|
|
|
|
if ok {
|
|
|
|
sock.remote = p.Addr.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.Log(r, string(debug.Stack()))
|
|
|
|
sock.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// execute socket func
|
|
|
|
m.fn(sock)
|
|
|
|
return nil
|
|
|
|
}
|