diff options
author | 2015-08-09 09:21:01 -0700 | |
---|---|---|
committer | 2015-08-09 09:21:01 -0700 | |
commit | 49a513031880a6303273f3fe3cec837225a5ad78 (patch) | |
tree | 0140ee377eb87732cdb6072576751d98f8c9d8cc /include/grpc++ | |
parent | 7c0d914cce379f14a1adfae9374641967c45d7b2 (diff) | |
parent | 10494fcb61d638682fb8e5d28356a1f5125e8d0a (diff) |
Merge branch 'compression-accept-encoding' into compression-interop
Diffstat (limited to 'include/grpc++')
-rw-r--r-- | include/grpc++/channel_interface.h | 30 | ||||
-rw-r--r-- | include/grpc++/client_context.h | 76 | ||||
-rw-r--r-- | include/grpc++/config.h | 2 | ||||
-rw-r--r-- | include/grpc++/credentials.h | 6 | ||||
-rw-r--r-- | include/grpc++/dynamic_thread_pool.h | 82 | ||||
-rw-r--r-- | include/grpc++/impl/sync_no_cxx11.h | 2 | ||||
-rw-r--r-- | include/grpc++/server_context.h | 25 | ||||
-rw-r--r-- | include/grpc++/server_credentials.h | 3 | ||||
-rw-r--r-- | include/grpc++/stream.h | 6 | ||||
-rw-r--r-- | include/grpc++/stub_options.h | 43 |
10 files changed, 261 insertions, 14 deletions
diff --git a/include/grpc++/channel_interface.h b/include/grpc++/channel_interface.h index 10fb9538bc..4176cded7b 100644 --- a/include/grpc++/channel_interface.h +++ b/include/grpc++/channel_interface.h @@ -36,6 +36,7 @@ #include <memory> +#include <grpc/grpc.h> #include <grpc++/status.h> #include <grpc++/impl/call.h> @@ -47,7 +48,6 @@ class CallOpBuffer; class ClientContext; class CompletionQueue; class RpcMethod; -class CallInterface; class ChannelInterface : public CallHook, public std::enable_shared_from_this<ChannelInterface> { @@ -57,6 +57,34 @@ class ChannelInterface : public CallHook, virtual void* RegisterMethod(const char* method_name) = 0; virtual Call CreateCall(const RpcMethod& method, ClientContext* context, CompletionQueue* cq) = 0; + + // Get the current channel state. If the channel is in IDLE and try_to_connect + // is set to true, try to connect. + virtual grpc_connectivity_state GetState(bool try_to_connect) = 0; + + // Return the tag on cq when the channel state is changed or deadline expires. + // GetState needs to called to get the current state. + template <typename T> + void NotifyOnStateChange(grpc_connectivity_state last_observed, T deadline, + CompletionQueue* cq, void* tag) { + TimePoint<T> deadline_tp(deadline); + NotifyOnStateChangeImpl(last_observed, deadline_tp.raw_time(), cq, tag); + } + + // Blocking wait for channel state change or deadline expiration. + // GetState needs to called to get the current state. + template <typename T> + bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline) { + TimePoint<T> deadline_tp(deadline); + return WaitForStateChangeImpl(last_observed, deadline_tp.raw_time()); + } + + private: + virtual void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed, + gpr_timespec deadline, + CompletionQueue* cq, void* tag) = 0; + virtual bool WaitForStateChangeImpl(grpc_connectivity_state last_observed, + gpr_timespec deadline) = 0; }; } // namespace grpc diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h index ccaf582d0a..5137bf6a1a 100644 --- a/include/grpc++/client_context.h +++ b/include/grpc++/client_context.h @@ -39,6 +39,7 @@ #include <string> #include <grpc/compression.h> +#include <grpc/grpc.h> #include <grpc/support/log.h> #include <grpc/support/time.h> #include <grpc++/auth_context.h> @@ -46,8 +47,6 @@ #include <grpc++/status.h> #include <grpc++/time.h> -struct grpc_call; -struct grpc_completion_queue; struct census_context; namespace grpc { @@ -70,6 +69,57 @@ template <class R, class W> class ClientAsyncReaderWriter; template <class R> class ClientAsyncResponseReader; +class ServerContext; + +class PropagationOptions { + public: + PropagationOptions() : propagate_(GRPC_PROPAGATE_DEFAULTS) {} + + PropagationOptions& enable_deadline_propagation() { + propagate_ |= GRPC_PROPAGATE_DEADLINE; + return *this; + } + + PropagationOptions& disable_deadline_propagation() { + propagate_ &= ~GRPC_PROPAGATE_DEADLINE; + return *this; + } + + PropagationOptions& enable_census_stats_propagation() { + propagate_ |= GRPC_PROPAGATE_CENSUS_STATS_CONTEXT; + return *this; + } + + PropagationOptions& disable_census_stats_propagation() { + propagate_ &= ~GRPC_PROPAGATE_CENSUS_STATS_CONTEXT; + return *this; + } + + PropagationOptions& enable_census_tracing_propagation() { + propagate_ |= GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT; + return *this; + } + + PropagationOptions& disable_census_tracing_propagation() { + propagate_ &= ~GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT; + return *this; + } + + PropagationOptions& enable_cancellation_propagation() { + propagate_ |= GRPC_PROPAGATE_CANCELLATION; + return *this; + } + + PropagationOptions& disable_cancellation_propagation() { + propagate_ &= ~GRPC_PROPAGATE_CANCELLATION; + return *this; + } + + gpr_uint32 c_bitmask() const { return propagate_; } + + private: + gpr_uint32 propagate_; +}; namespace testing { class InteropClientContextInspector; @@ -80,6 +130,11 @@ class ClientContext { ClientContext(); ~ClientContext(); + /// Create a new ClientContext that propagates some or all of its attributes + static std::unique_ptr<ClientContext> FromServerContext( + const ServerContext& server_context, + PropagationOptions options = PropagationOptions()); + void AddMetadata(const grpc::string& meta_key, const grpc::string& meta_value); @@ -114,7 +169,7 @@ class ClientContext { creds_ = creds; } - grpc_compression_algorithm get_compression_algorithm() const { + grpc_compression_algorithm compression_algorithm() const { return compression_algorithm_; } @@ -122,9 +177,15 @@ class ClientContext { std::shared_ptr<const AuthContext> auth_context() const; + // Return the peer uri in a string. + // WARNING: this value is never authenticated or subject to any security + // related code. It must not be used for any authentication related + // functionality. Instead, use auth_context. + grpc::string peer() const; + // Get and set census context - void set_census_context(census_context* ccp) { census_context_ = ccp; } - census_context* get_census_context() const { return census_context_; } + void set_census_context(struct census_context* ccp) { census_context_ = ccp; } + struct census_context* census_context() const { return census_context_; } void TryCancel(); @@ -175,11 +236,14 @@ class ClientContext { grpc::string authority_; std::shared_ptr<Credentials> creds_; mutable std::shared_ptr<const AuthContext> auth_context_; - census_context* census_context_; + struct census_context* census_context_; std::multimap<grpc::string, grpc::string> send_initial_metadata_; std::multimap<grpc::string, grpc::string> recv_initial_metadata_; std::multimap<grpc::string, grpc::string> trailing_metadata_; + grpc_call* propagate_from_call_; + PropagationOptions propagation_options_; + grpc_compression_algorithm compression_algorithm_; }; diff --git a/include/grpc++/config.h b/include/grpc++/config.h index 1362c0a1fa..889dc39eb7 100644 --- a/include/grpc++/config.h +++ b/include/grpc++/config.h @@ -79,6 +79,7 @@ #ifdef GRPC_CXX0X_NO_NULLPTR #include <memory> +namespace grpc { const class { public: template <class T> @@ -98,6 +99,7 @@ const class { private: void operator&() const = delete; } nullptr = {}; +} #endif #ifndef GRPC_CUSTOM_STRING diff --git a/include/grpc++/credentials.h b/include/grpc++/credentials.h index 0eaaefcbca..a4f1e73118 100644 --- a/include/grpc++/credentials.h +++ b/include/grpc++/credentials.h @@ -106,13 +106,13 @@ std::shared_ptr<Credentials> ServiceAccountCredentials( const grpc::string& json_key, const grpc::string& scope, long token_lifetime_seconds); -// Builds JWT credentials. +// Builds Service Account JWT Access credentials. // json_key is the JSON key string containing the client's private key. // token_lifetime_seconds is the lifetime in seconds of each Json Web Token // (JWT) created with this credentials. It should not exceed // grpc_max_auth_token_lifetime or will be cropped to this value. -std::shared_ptr<Credentials> JWTCredentials(const grpc::string& json_key, - long token_lifetime_seconds); +std::shared_ptr<Credentials> ServiceAccountJWTAccessCredentials( + const grpc::string& json_key, long token_lifetime_seconds); // Builds refresh token credentials. // json_refresh_token is the JSON string containing the refresh token along diff --git a/include/grpc++/dynamic_thread_pool.h b/include/grpc++/dynamic_thread_pool.h new file mode 100644 index 0000000000..f0cd35940f --- /dev/null +++ b/include/grpc++/dynamic_thread_pool.h @@ -0,0 +1,82 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPCXX_DYNAMIC_THREAD_POOL_H +#define GRPCXX_DYNAMIC_THREAD_POOL_H + +#include <grpc++/config.h> + +#include <grpc++/impl/sync.h> +#include <grpc++/impl/thd.h> +#include <grpc++/thread_pool_interface.h> + +#include <list> +#include <memory> +#include <queue> + +namespace grpc { + +class DynamicThreadPool GRPC_FINAL : public ThreadPoolInterface { + public: + explicit DynamicThreadPool(int reserve_threads); + ~DynamicThreadPool(); + + void Add(const std::function<void()>& callback) GRPC_OVERRIDE; + + private: + class DynamicThread { + public: + DynamicThread(DynamicThreadPool *pool); + ~DynamicThread(); + private: + DynamicThreadPool *pool_; + std::unique_ptr<grpc::thread> thd_; + void ThreadFunc(); + }; + grpc::mutex mu_; + grpc::condition_variable cv_; + grpc::condition_variable shutdown_cv_; + bool shutdown_; + std::queue<std::function<void()>> callbacks_; + int reserve_threads_; + int nthreads_; + int threads_waiting_; + std::list<DynamicThread*> dead_threads_; + + void ThreadFunc(); + static void ReapThreads(std::list<DynamicThread*>* tlist); +}; + +} // namespace grpc + +#endif // GRPCXX_DYNAMIC_THREAD_POOL_H diff --git a/include/grpc++/impl/sync_no_cxx11.h b/include/grpc++/impl/sync_no_cxx11.h index dda939bf71..5869b04c76 100644 --- a/include/grpc++/impl/sync_no_cxx11.h +++ b/include/grpc++/impl/sync_no_cxx11.h @@ -87,7 +87,7 @@ class condition_variable { ~condition_variable() { gpr_cv_destroy(&cv_); } void wait(lock_guard<mutex> &mu) { mu.locked = false; - gpr_cv_wait(&cv_, &mu.mu_.mu_, gpr_inf_future); + gpr_cv_wait(&cv_, &mu.mu_.mu_, gpr_inf_future(GPR_CLOCK_REALTIME)); mu.locked = true; } void notify_one() { gpr_cv_signal(&cv_); } diff --git a/include/grpc++/server_context.h b/include/grpc++/server_context.h index 268cd7ffc3..2296835461 100644 --- a/include/grpc++/server_context.h +++ b/include/grpc++/server_context.h @@ -46,9 +46,11 @@ struct gpr_timespec; struct grpc_metadata; struct grpc_call; +struct census_context; namespace grpc { +class ClientContext; template <class W, class R> class ServerAsyncReader; template <class W> @@ -104,18 +106,34 @@ class ServerContext { return client_metadata_; } - grpc_compression_level get_compression_level() const { + grpc_compression_level compression_level() const { return compression_level_; } void set_compression_level(grpc_compression_level level); - grpc_compression_algorithm get_compression_algorithm() const { + grpc_compression_algorithm compression_algorithm() const { return compression_algorithm_; } void set_compression_algorithm(grpc_compression_algorithm algorithm); std::shared_ptr<const AuthContext> auth_context() const; + // Return the peer uri in a string. + // WARNING: this value is never authenticated or subject to any security + // related code. It must not be used for any authentication related + // functionality. Instead, use auth_context. + grpc::string peer() const; + + const struct census_context* census_context() const; + + // Async only. Has to be called before the rpc starts. + // Returns the tag in completion queue when the rpc finishes. + // IsCancelled() can then be called to check whether the rpc was cancelled. + void AsyncNotifyWhenDone(void* tag) { + has_notify_when_done_tag_ = true; + async_notify_when_done_tag_ = tag; + } + private: friend class ::grpc::testing::InteropServerContextInspector; friend class ::grpc::Server; @@ -141,6 +159,7 @@ class ServerContext { friend class ServerStreamingHandler; template <class ServiceType, class RequestType, class ResponseType> friend class BidiStreamingHandler; + friend class ::grpc::ClientContext; // Prevent copying. ServerContext(const ServerContext&); @@ -156,6 +175,8 @@ class ServerContext { void set_call(grpc_call* call); CompletionOp* completion_op_; + bool has_notify_when_done_tag_; + void* async_notify_when_done_tag_; gpr_timespec deadline_; grpc_call* call_; diff --git a/include/grpc++/server_credentials.h b/include/grpc++/server_credentials.h index 83ae9fd1eb..11acd67e8a 100644 --- a/include/grpc++/server_credentials.h +++ b/include/grpc++/server_credentials.h @@ -58,12 +58,15 @@ class ServerCredentials { // Options to create ServerCredentials with SSL struct SslServerCredentialsOptions { + SslServerCredentialsOptions() : force_client_auth(false) {} + struct PemKeyCertPair { grpc::string private_key; grpc::string cert_chain; }; grpc::string pem_root_certs; std::vector<PemKeyCertPair> pem_key_cert_pairs; + bool force_client_auth; }; // Builds SSL ServerCredentials given SSL specific options diff --git a/include/grpc++/stream.h b/include/grpc++/stream.h index 3903f2ec06..bc0c3c0f3b 100644 --- a/include/grpc++/stream.h +++ b/include/grpc++/stream.h @@ -54,7 +54,11 @@ class ClientStreamingInterface { // client side declares it has no more message to send, either implicitly or // by calling WritesDone, it needs to make sure there is no more message to // be received from the server, either implicitly or by getting a false from - // a Read(). Otherwise, this implicitly cancels the stream. + // a Read(). + // This function will return either: + // - when all incoming messages have been read and the server has returned + // status + // - OR when the server has returned a non-OK status virtual Status Finish() = 0; }; diff --git a/include/grpc++/stub_options.h b/include/grpc++/stub_options.h new file mode 100644 index 0000000000..c7c16dcd55 --- /dev/null +++ b/include/grpc++/stub_options.h @@ -0,0 +1,43 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPCXX_STUB_OPTIONS_H +#define GRPCXX_STUB_OPTIONS_H + +namespace grpc { + +class StubOptions {}; + +} // namespace grpc + +#endif // GRPCXX_STUB_OPTIONS_H |