micro/transport/grpc/handler.go

45 lines
915 B
Go
Raw Normal View History

2019-05-24 17:15:59 +01:00
package grpc
import (
"runtime/debug"
"github.com/unistack-org/micro/v3/errors"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/transport"
pb "github.com/unistack-org/micro/v3/transport/grpc/proto"
2019-05-24 17:15:59 +01:00
"google.golang.org/grpc/peer"
)
// microTransport satisfies the pb.TransportServer inteface
type microTransport struct {
pb.UnimplementedTransportServer
2019-05-24 17:15:59 +01:00
addr string
fn func(transport.Socket)
}
func (m *microTransport) Stream(ts pb.Transport_StreamServer) (err error) {
2019-05-24 17:15:59 +01:00
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 {
logger.Error(r, string(debug.Stack()))
2019-05-24 17:15:59 +01:00
sock.Close()
err = errors.InternalServerError("go.micro.transport", "panic recovered: %v", r)
2019-05-24 17:15:59 +01:00
}
}()
// execute socket func
m.fn(sock)
return err
2019-05-24 17:15:59 +01:00
}