• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/core/lib/resource_quota/api.h"
16 
17 #include <grpc/grpc.h>
18 #include <grpc/impl/channel_arg_names.h>
19 #include <grpc/support/port_platform.h>
20 #include <stdint.h>
21 
22 #include <atomic>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 
27 #include "absl/strings/str_cat.h"
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/iomgr/exec_ctx.h"
30 #include "src/core/lib/resource_quota/memory_quota.h"
31 #include "src/core/lib/resource_quota/resource_quota.h"
32 #include "src/core/lib/resource_quota/thread_quota.h"
33 #include "src/core/util/ref_counted_ptr.h"
34 
35 namespace grpc_core {
36 
ResourceQuotaFromChannelArgs(const grpc_channel_args * args)37 ResourceQuotaRefPtr ResourceQuotaFromChannelArgs(
38     const grpc_channel_args* args) {
39   return grpc_channel_args_find_pointer<ResourceQuota>(args,
40                                                        GRPC_ARG_RESOURCE_QUOTA)
41       ->Ref();
42 }
43 
ResourceQuotaFromEndpointConfig(const grpc_event_engine::experimental::EndpointConfig & config)44 ResourceQuotaRefPtr ResourceQuotaFromEndpointConfig(
45     const grpc_event_engine::experimental::EndpointConfig& config) {
46   void* value = config.GetVoidPointer(GRPC_ARG_RESOURCE_QUOTA);
47   if (value != nullptr) {
48     return reinterpret_cast<ResourceQuota*>(value)->Ref();
49   }
50   return nullptr;
51 }
52 
EnsureResourceQuotaInChannelArgs(const ChannelArgs & args)53 ChannelArgs EnsureResourceQuotaInChannelArgs(const ChannelArgs& args) {
54   if (args.GetObject<ResourceQuota>() != nullptr) return args;
55   // If there's no existing quota, add it to the default one - shared between
56   // all channel args declared thusly. This prevents us from accidentally not
57   // sharing subchannels due to their channel args not specifying a quota.
58   return args.SetObject(ResourceQuota::Default());
59 }
60 
RegisterResourceQuota(CoreConfiguration::Builder * builder)61 void RegisterResourceQuota(CoreConfiguration::Builder* builder) {
62   builder->channel_args_preconditioning()->RegisterStage(
63       EnsureResourceQuotaInChannelArgs);
64 }
65 
66 }  // namespace grpc_core
67 
grpc_resource_quota_arg_vtable()68 extern "C" const grpc_arg_pointer_vtable* grpc_resource_quota_arg_vtable() {
69   return grpc_core::ChannelArgTypeTraits<grpc_core::ResourceQuota>::VTable();
70 }
71 
grpc_resource_quota_create(const char * name)72 extern "C" grpc_resource_quota* grpc_resource_quota_create(const char* name) {
73   static std::atomic<uintptr_t> anonymous_counter{0};
74   std::string quota_name =
75       name == nullptr
76           ? absl::StrCat("anonymous-quota-", anonymous_counter.fetch_add(1))
77           : name;
78   return (new grpc_core::ResourceQuota(std::move(quota_name)))->c_ptr();
79 }
80 
grpc_resource_quota_ref(grpc_resource_quota * resource_quota)81 extern "C" void grpc_resource_quota_ref(grpc_resource_quota* resource_quota) {
82   grpc_core::ResourceQuota::FromC(resource_quota)->Ref().release();
83 }
84 
grpc_resource_quota_unref(grpc_resource_quota * resource_quota)85 extern "C" void grpc_resource_quota_unref(grpc_resource_quota* resource_quota) {
86   grpc_core::ResourceQuota::FromC(resource_quota)->Unref();
87 }
88 
grpc_resource_quota_resize(grpc_resource_quota * resource_quota,size_t new_size)89 extern "C" void grpc_resource_quota_resize(grpc_resource_quota* resource_quota,
90                                            size_t new_size) {
91   grpc_core::ExecCtx exec_ctx;
92   grpc_core::ResourceQuota::FromC(resource_quota)
93       ->memory_quota()
94       ->SetSize(new_size);
95 }
96 
grpc_resource_quota_set_max_threads(grpc_resource_quota * resource_quota,int new_max_threads)97 extern "C" void grpc_resource_quota_set_max_threads(
98     grpc_resource_quota* resource_quota, int new_max_threads) {
99   grpc_core::ResourceQuota::FromC(resource_quota)
100       ->thread_quota()
101       ->SetMax(new_max_threads);
102 }
103