示例#1
0
func wrapStreamWithAuth(authDesc StreamAuthDesc, desc *grpc.StreamDesc, handler interface{}) {
	h := desc.Handler
	desc.Handler = func(srv interface{}, stream grpc.ServerStream) error {
		stream = &authServerStream{stream, authDesc, handler}
		return h(srv, stream)
	}
}
示例#2
0
func wrapStreamWithErrorMapper(srv *grpc.ServiceDesc, desc grpc.StreamDesc, mapper ErrorMapper) grpc.StreamDesc {
	h := desc.Handler
	desc.Handler = func(srv interface{}, stream grpc.ServerStream) (err error) {
		err = h(srv, stream)
		if err != nil {
			if e := mapper.HandleError(err); e != nil {
				err = e
			}
			return err
		}
		return nil
	}
	return desc
}
示例#3
0
func wrapStreamWithTracer(srv *grpc.ServiceDesc, desc grpc.StreamDesc) grpc.StreamDesc {
	name := "/" + srv.ServiceName + "/" + desc.StreamName + "/root"
	h := desc.Handler
	desc.Handler = func(srv interface{}, stream grpc.ServerStream) (err error) {
		span, ctx := trace.New(stream.Context(), name, trace.WithPanicGuard)
		defer func(errPtr *error) {
			if span.Failed && *errPtr == nil {
				*errPtr = grpc.Errorf(codes.Internal, "internal server error")
			}
		}(&err)
		defer span.Close()

		stream = wrapServerSteamWithContext(stream, ctx)
		return h(srv, stream)
	}
	return desc
}