github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/network/transport/grpc/handler.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/micro/go-micro/v3/network/transport/grpc/handler.go 14 15 package grpc 16 17 import ( 18 "runtime/debug" 19 20 pb "github.com/tickoalcantara12/micro/v3/proto/transport" 21 "github.com/tickoalcantara12/micro/v3/service/errors" 22 "github.com/tickoalcantara12/micro/v3/service/logger" 23 "github.com/tickoalcantara12/micro/v3/service/network/transport" 24 "google.golang.org/grpc/peer" 25 ) 26 27 // microTransport satisfies the pb.TransportServer inteface 28 type microTransport struct { 29 addr string 30 fn func(transport.Socket) 31 } 32 33 func (m *microTransport) Stream(ts pb.Transport_StreamServer) (err error) { 34 35 sock := &grpcTransportSocket{ 36 stream: ts, 37 local: m.addr, 38 } 39 40 p, ok := peer.FromContext(ts.Context()) 41 if ok { 42 sock.remote = p.Addr.String() 43 } 44 45 defer func() { 46 if r := recover(); r != nil { 47 logger.Error(r, string(debug.Stack())) 48 sock.Close() 49 err = errors.InternalServerError("go.micro.transport", "panic recovered: %v", r) 50 } 51 }() 52 53 // execute socket func 54 m.fn(sock) 55 56 return err 57 }