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