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 <grpc/support/port_platform.h>
16
17 #include "src/core/lib/resource_quota/api.h"
18
19 #include <stdint.h>
20
21 #include <atomic>
22 #include <memory>
23 #include <string>
24 #include <utility>
25
26 #include "absl/strings/str_cat.h"
27
28 #include <grpc/grpc.h>
29
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gprpp/ref_counted_ptr.h"
32 #include "src/core/lib/iomgr/exec_ctx.h"
33 #include "src/core/lib/resource_quota/memory_quota.h"
34 #include "src/core/lib/resource_quota/resource_quota.h"
35 #include "src/core/lib/resource_quota/thread_quota.h"
36
37 namespace grpc_core {
38
ResourceQuotaFromChannelArgs(const grpc_channel_args * args)39 ResourceQuotaRefPtr ResourceQuotaFromChannelArgs(
40 const grpc_channel_args* args) {
41 return grpc_channel_args_find_pointer<ResourceQuota>(args,
42 GRPC_ARG_RESOURCE_QUOTA)
43 ->Ref();
44 }
45
ResourceQuotaFromEndpointConfig(const grpc_event_engine::experimental::EndpointConfig & config)46 ResourceQuotaRefPtr ResourceQuotaFromEndpointConfig(
47 const grpc_event_engine::experimental::EndpointConfig& config) {
48 void* value = config.GetVoidPointer(GRPC_ARG_RESOURCE_QUOTA);
49 if (value != nullptr) {
50 return reinterpret_cast<ResourceQuota*>(value)->Ref();
51 }
52 return nullptr;
53 }
54
EnsureResourceQuotaInChannelArgs(const ChannelArgs & args)55 ChannelArgs EnsureResourceQuotaInChannelArgs(const ChannelArgs& args) {
56 if (args.GetObject<ResourceQuota>() != nullptr) return args;
57 // If there's no existing quota, add it to the default one - shared between
58 // all channel args declared thusly. This prevents us from accidentally not
59 // sharing subchannels due to their channel args not specifying a quota.
60 return args.SetObject(ResourceQuota::Default());
61 }
62
RegisterResourceQuota(CoreConfiguration::Builder * builder)63 void RegisterResourceQuota(CoreConfiguration::Builder* builder) {
64 builder->channel_args_preconditioning()->RegisterStage(
65 EnsureResourceQuotaInChannelArgs);
66 }
67
68 } // namespace grpc_core
69
grpc_resource_quota_arg_vtable()70 extern "C" const grpc_arg_pointer_vtable* grpc_resource_quota_arg_vtable() {
71 return grpc_core::ChannelArgTypeTraits<grpc_core::ResourceQuota>::VTable();
72 }
73
grpc_resource_quota_create(const char * name)74 extern "C" grpc_resource_quota* grpc_resource_quota_create(const char* name) {
75 static std::atomic<uintptr_t> anonymous_counter{0};
76 std::string quota_name =
77 name == nullptr
78 ? absl::StrCat("anonymous-quota-", anonymous_counter.fetch_add(1))
79 : name;
80 return (new grpc_core::ResourceQuota(std::move(quota_name)))->c_ptr();
81 }
82
grpc_resource_quota_ref(grpc_resource_quota * resource_quota)83 extern "C" void grpc_resource_quota_ref(grpc_resource_quota* resource_quota) {
84 grpc_core::ResourceQuota::FromC(resource_quota)->Ref().release();
85 }
86
grpc_resource_quota_unref(grpc_resource_quota * resource_quota)87 extern "C" void grpc_resource_quota_unref(grpc_resource_quota* resource_quota) {
88 grpc_core::ResourceQuota::FromC(resource_quota)->Unref();
89 }
90
grpc_resource_quota_resize(grpc_resource_quota * resource_quota,size_t new_size)91 extern "C" void grpc_resource_quota_resize(grpc_resource_quota* resource_quota,
92 size_t new_size) {
93 grpc_core::ExecCtx exec_ctx;
94 grpc_core::ResourceQuota::FromC(resource_quota)
95 ->memory_quota()
96 ->SetSize(new_size);
97 }
98
grpc_resource_quota_set_max_threads(grpc_resource_quota * resource_quota,int new_max_threads)99 extern "C" void grpc_resource_quota_set_max_threads(
100 grpc_resource_quota* resource_quota, int new_max_threads) {
101 grpc_core::ResourceQuota::FromC(resource_quota)
102 ->thread_quota()
103 ->SetMax(new_max_threads);
104 }
105