2020-08-23 20:37:22 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime/debug"
|
|
|
|
|
2021-10-28 00:58:31 +03:00
|
|
|
pb "go.unistack.org/micro-network-transport-grpc/v3/proto"
|
|
|
|
"go.unistack.org/micro/v3/errors"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/network/transport"
|
2020-08-23 20:37:22 +03:00
|
|
|
"google.golang.org/grpc/peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// microTransport satisfies the pb.TransportServer inteface
|
|
|
|
type microTransport struct {
|
2020-09-20 17:04:25 +03:00
|
|
|
pb.UnimplementedTransportServer
|
2020-08-23 20:37:22 +03:00
|
|
|
addr string
|
|
|
|
fn func(transport.Socket)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *microTransport) Stream(ts pb.Transport_StreamServer) (err 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 {
|
2021-01-29 18:47:28 +03:00
|
|
|
logger.Error(ts.Context(), "panic recovered: ", r)
|
|
|
|
logger.Error(ts.Context(), string(debug.Stack()))
|
2020-08-23 20:37:22 +03:00
|
|
|
sock.Close()
|
|
|
|
err = errors.InternalServerError("go.micro.transport", "panic recovered: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// execute socket func
|
|
|
|
m.fn(sock)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|