NCBI C++ ToolKit
grpc_integration.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef SERIAL_GRPC_INTEGRATION___GRPC_INTEGRATION__HPP
2 #define SERIAL_GRPC_INTEGRATION___GRPC_INTEGRATION__HPP
3 
4 /* $Id: grpc_integration.hpp 92407 2021-01-19 13:37:29Z grichenk $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Aaron Ucko
30  *
31  */
32 
33 /// @file grpc_integration.hpp
34 /// Glue for integrating gRPC into the C++ toolkit, to ensure logging
35 /// and metadata propagation (sid, phid, auth tokens etc) are all
36 /// handled uniformly and appropriately.
37 
38 
40 #include <corelib/ncbi_param.hpp>
41 #ifdef HAVE_LIBGRPC
42 # include <grpc++/client_context.h>
43 #else
44 namespace grpc {
45  class ClientContext {};
46  class PropagationOptions {};
47 }
48 #endif
49 
50 
51 /** @addtogroup Miscellaneous
52  *
53  * @{
54  */
55 
56 
58 
59 #ifdef GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H
60 typedef grpc_impl::ClientContext TGRPCBaseClientContext;
61 typedef grpc_impl::PropagationOptions TGRPCPropagationOptions;
62 #else
63 typedef grpc::ClientContext TGRPCBaseClientContext;
64 typedef grpc::PropagationOptions TGRPCPropagationOptions;
65 #endif
66 
67 /// CGRPCClientContext -- client context for NCBI gRPC services.
68 ///
69 /// The only difference from plain ClientContext is the automatic
70 /// inclusion of NCBI metadata via AddStandardNCBIMetadata. As such,
71 /// if constructing a CGRPCClientContext is infeasible for any reason,
72 /// it's also possible to obtain a suitably configured ClientContext
73 /// by passing a ServerContext to FromServerContext or calling
74 /// AddStandardNCBIMetadata on an existing ClientContext (bearing in
75 /// mind that every actual remote procedure call requires a fresh context).
77 {
78 public:
80 
82  { AddStandardNCBIMetadata(*this); }
83 
84  static unique_ptr<TParent> FromServerContext(const TGRPCServerContext& sc,
87 
88  static void AddStandardNCBIMetadata(TParent& cctx);
89 
90  static bool IsImplemented(void);
91 };
92 
93 
94 /// BEGIN_NCBI_GRPC_REQUEST -- Set up AppLog logging for the current
95 /// (server-side) request handler.
96 ///
97 /// The third argument may be either a reply google::protobuf::Message
98 /// or a streaming grpc::ServerWriter<>. In the case of a reply
99 /// message, automatic wrap-up actions will include propagating its
100 /// byte size to the request context's count of bytes written. In the
101 /// case of a streaming writer, however, there's no automatic way to
102 /// determine that count, so callers are responsible for supplying it
103 /// themselves.
104 #define BEGIN_NCBI_GRPC_REQUEST(sctx, request, reply) \
105  CGRPCRequestLogger request_logger##__LINE__ \
106  (sctx, __func__, request, reply)
107 
108 
109 /// NCBI_GRPC_RETURN(_EX) -- Provide consistent gRPC request handler status
110 /// information to AppLog and gRPC. The _EX variant takes an error message
111 /// to substitute for a stock status code description. (A 1xx-3xx status
112 /// code still always yields grpc::OK, with no custom message.)
113 #define NCBI_GRPC_RETURN_EX(sc, msg) do { \
114  CRequestStatus::ECode status_code = (sc); \
115  GetDiagContext().GetRequestContext().SetRequestStatus(status_code); \
116  return g_AsGRPCStatus(status_code, (msg)); \
117 } while (0)
118 
119 #define NCBI_GRPC_RETURN(sc) NCBI_GRPC_RETURN_EX(sc, nullptr)
120 
121 
122 /// Get "hostport" for the likes of "grpc::CreateChannel(hostport, ...)" trying
123 /// (in order of priority):
124 /// - Config file entry "[section] variable"
125 /// - Environment variables: env_var_name (if not empty/NULL);
126 /// then "NCBI_CONFIG__<section>__<name>"; then "GRPC_PROXY"
127 /// - The hard-coded NCBI default "linkerd:4142"
128 /// The value_source (if not null) will get CParamBase::EParamSource value
129 string g_NCBI_GRPC_GetAddress(const char* section,
130  const char* variable,
131  const char* env_var_name = nullptr,
132  CParamBase::EParamSource* value_source = nullptr);
133 
135 
136 
137 /* @} */
138 
139 #endif /* SERIAL_GRPC_INTEGRATION___GRPC_INTEGRATION__HPP */
CGRPCClientContext – client context for NCBI gRPC services.
static void AddStandardNCBIMetadata(TParent &cctx)
grpc::ClientContext TGRPCBaseClientContext
TGRPCBaseClientContext TParent
grpc::PropagationOptions TGRPCPropagationOptions
grpc::ServerContext TGRPCServerContext
static unique_ptr< TParent > FromServerContext(const TGRPCServerContext &sc, TGRPCPropagationOptions opts=TGRPCPropagationOptions())
static bool IsImplemented(void)
string g_NCBI_GRPC_GetAddress(const char *section, const char *variable, const char *env_var_name=nullptr, CParamBase::EParamSource *value_source=nullptr)
Get "hostport" for the likes of "grpc::CreateChannel(hostport, ...)" trying (in order of priority):
EParamSource
Source of the value returned by CParam::GetDefault().
Definition: ncbi_param.hpp:395
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
Classes and functions supporting grpc_integration.
Modified on Tue Apr 23 07:39:42 2024 by modify_doxy.py rev. 669887