2019-05-24 17:15:59 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime/debug"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/errors"
|
2020-02-23 16:45:20 +03:00
|
|
|
log "github.com/micro/go-micro/v2/logger"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/transport"
|
|
|
|
pb "github.com/micro/go-micro/v2/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 {
|
|
|
|
addr string
|
|
|
|
fn func(transport.Socket)
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:08:21 +08:00
|
|
|
func (m *microTransport) Stream(ts pb.Transport_StreamServer) (err error) {
|
2019-12-03 01:36:14 +03:00
|
|
|
|
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 {
|
2020-02-23 16:45:20 +03:00
|
|
|
log.Error(r, string(debug.Stack()))
|
2019-05-24 17:15:59 +01:00
|
|
|
sock.Close()
|
2019-12-03 01:36:14 +03:00
|
|
|
err = errors.InternalServerError("go.micro.transport", "panic recovered: %v", r)
|
2019-05-24 17:15:59 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// execute socket func
|
|
|
|
m.fn(sock)
|
2019-12-03 01:36:14 +03:00
|
|
|
|
|
|
|
return err
|
2019-05-24 17:15:59 +01:00
|
|
|
}
|