• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include "src/core/lib/iomgr/sockaddr.h"
20 
21 #include "test/core/util/passthru_endpoint.h"
22 
23 #include <inttypes.h>
24 #include <string.h>
25 
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29 
30 #include "src/core/lib/gpr/useful.h"
31 #include "src/core/lib/slice/slice_internal.h"
32 
33 #define WRITE_BUFFER_SIZE (2 * 1024 * 1024)
34 
35 typedef struct {
36   grpc_endpoint base;
37   double bytes_per_second;
38   grpc_endpoint* wrapped;
39   gpr_timespec last_write;
40 
41   gpr_mu mu;
42   grpc_slice_buffer write_buffer;
43   grpc_slice_buffer writing_buffer;
44   grpc_error* error;
45   bool writing;
46   grpc_closure* write_cb;
47 } trickle_endpoint;
48 
te_read(grpc_endpoint * ep,grpc_slice_buffer * slices,grpc_closure * cb,bool urgent)49 static void te_read(grpc_endpoint* ep, grpc_slice_buffer* slices,
50                     grpc_closure* cb, bool urgent) {
51   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
52   grpc_endpoint_read(te->wrapped, slices, cb, urgent);
53 }
54 
maybe_call_write_cb_locked(trickle_endpoint * te)55 static void maybe_call_write_cb_locked(trickle_endpoint* te) {
56   if (te->write_cb != nullptr &&
57       (te->error != GRPC_ERROR_NONE ||
58        te->write_buffer.length <= WRITE_BUFFER_SIZE)) {
59     grpc_core::ExecCtx::Run(DEBUG_LOCATION, te->write_cb,
60                             GRPC_ERROR_REF(te->error));
61     te->write_cb = nullptr;
62   }
63 }
64 
te_write(grpc_endpoint * ep,grpc_slice_buffer * slices,grpc_closure * cb,void *)65 static void te_write(grpc_endpoint* ep, grpc_slice_buffer* slices,
66                      grpc_closure* cb, void* /*arg*/) {
67   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
68   gpr_mu_lock(&te->mu);
69   GPR_ASSERT(te->write_cb == nullptr);
70   if (te->write_buffer.length == 0) {
71     te->last_write = gpr_now(GPR_CLOCK_MONOTONIC);
72   }
73   for (size_t i = 0; i < slices->count; i++) {
74     grpc_slice_buffer_add(&te->write_buffer,
75                           grpc_slice_copy(slices->slices[i]));
76   }
77   te->write_cb = cb;
78   maybe_call_write_cb_locked(te);
79   gpr_mu_unlock(&te->mu);
80 }
81 
te_add_to_pollset(grpc_endpoint * ep,grpc_pollset * pollset)82 static void te_add_to_pollset(grpc_endpoint* ep, grpc_pollset* pollset) {
83   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
84   grpc_endpoint_add_to_pollset(te->wrapped, pollset);
85 }
86 
te_add_to_pollset_set(grpc_endpoint * ep,grpc_pollset_set * pollset_set)87 static void te_add_to_pollset_set(grpc_endpoint* ep,
88                                   grpc_pollset_set* pollset_set) {
89   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
90   grpc_endpoint_add_to_pollset_set(te->wrapped, pollset_set);
91 }
92 
te_delete_from_pollset_set(grpc_endpoint * ep,grpc_pollset_set * pollset_set)93 static void te_delete_from_pollset_set(grpc_endpoint* ep,
94                                        grpc_pollset_set* pollset_set) {
95   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
96   grpc_endpoint_delete_from_pollset_set(te->wrapped, pollset_set);
97 }
98 
te_shutdown(grpc_endpoint * ep,grpc_error * why)99 static void te_shutdown(grpc_endpoint* ep, grpc_error* why) {
100   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
101   gpr_mu_lock(&te->mu);
102   if (te->error == GRPC_ERROR_NONE) {
103     te->error = GRPC_ERROR_REF(why);
104   }
105   maybe_call_write_cb_locked(te);
106   gpr_mu_unlock(&te->mu);
107   grpc_endpoint_shutdown(te->wrapped, why);
108 }
109 
te_destroy(grpc_endpoint * ep)110 static void te_destroy(grpc_endpoint* ep) {
111   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
112   grpc_endpoint_destroy(te->wrapped);
113   gpr_mu_destroy(&te->mu);
114   grpc_slice_buffer_destroy_internal(&te->write_buffer);
115   grpc_slice_buffer_destroy_internal(&te->writing_buffer);
116   GRPC_ERROR_UNREF(te->error);
117   gpr_free(te);
118 }
119 
te_get_resource_user(grpc_endpoint * ep)120 static grpc_resource_user* te_get_resource_user(grpc_endpoint* ep) {
121   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
122   return grpc_endpoint_get_resource_user(te->wrapped);
123 }
124 
te_get_peer(grpc_endpoint * ep)125 static char* te_get_peer(grpc_endpoint* ep) {
126   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
127   return grpc_endpoint_get_peer(te->wrapped);
128 }
129 
te_get_fd(grpc_endpoint * ep)130 static int te_get_fd(grpc_endpoint* ep) {
131   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
132   return grpc_endpoint_get_fd(te->wrapped);
133 }
134 
te_can_track_err(grpc_endpoint *)135 static bool te_can_track_err(grpc_endpoint* /*ep*/) { return false; }
136 
te_finish_write(void * arg,grpc_error *)137 static void te_finish_write(void* arg, grpc_error* /*error*/) {
138   trickle_endpoint* te = static_cast<trickle_endpoint*>(arg);
139   gpr_mu_lock(&te->mu);
140   te->writing = false;
141   grpc_slice_buffer_reset_and_unref(&te->writing_buffer);
142   gpr_mu_unlock(&te->mu);
143 }
144 
145 static const grpc_endpoint_vtable vtable = {te_read,
146                                             te_write,
147                                             te_add_to_pollset,
148                                             te_add_to_pollset_set,
149                                             te_delete_from_pollset_set,
150                                             te_shutdown,
151                                             te_destroy,
152                                             te_get_resource_user,
153                                             te_get_peer,
154                                             te_get_fd,
155                                             te_can_track_err};
156 
grpc_trickle_endpoint_create(grpc_endpoint * wrap,double bytes_per_second)157 grpc_endpoint* grpc_trickle_endpoint_create(grpc_endpoint* wrap,
158                                             double bytes_per_second) {
159   trickle_endpoint* te =
160       static_cast<trickle_endpoint*>(gpr_malloc(sizeof(*te)));
161   te->base.vtable = &vtable;
162   te->wrapped = wrap;
163   te->bytes_per_second = bytes_per_second;
164   te->write_cb = nullptr;
165   gpr_mu_init(&te->mu);
166   grpc_slice_buffer_init(&te->write_buffer);
167   grpc_slice_buffer_init(&te->writing_buffer);
168   te->error = GRPC_ERROR_NONE;
169   te->writing = false;
170   return &te->base;
171 }
172 
ts2dbl(gpr_timespec s)173 static double ts2dbl(gpr_timespec s) {
174   return static_cast<double>(s.tv_sec) + 1e-9 * static_cast<double>(s.tv_nsec);
175 }
176 
grpc_trickle_endpoint_trickle(grpc_endpoint * ep)177 size_t grpc_trickle_endpoint_trickle(grpc_endpoint* ep) {
178   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
179   gpr_mu_lock(&te->mu);
180   if (!te->writing && te->write_buffer.length > 0) {
181     gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC);
182     double elapsed = ts2dbl(gpr_time_sub(now, te->last_write));
183     size_t bytes = static_cast<size_t>(te->bytes_per_second * elapsed);
184     // gpr_log(GPR_DEBUG, "%lf elapsed --> %" PRIdPTR " bytes", elapsed, bytes);
185     if (bytes > 0) {
186       grpc_slice_buffer_move_first(&te->write_buffer,
187                                    GPR_MIN(bytes, te->write_buffer.length),
188                                    &te->writing_buffer);
189       te->writing = true;
190       te->last_write = now;
191       grpc_endpoint_write(
192           te->wrapped, &te->writing_buffer,
193           GRPC_CLOSURE_CREATE(te_finish_write, te, grpc_schedule_on_exec_ctx),
194           nullptr);
195       maybe_call_write_cb_locked(te);
196     }
197   }
198   size_t backlog = te->write_buffer.length;
199   gpr_mu_unlock(&te->mu);
200   return backlog;
201 }
202 
grpc_trickle_get_backlog(grpc_endpoint * ep)203 size_t grpc_trickle_get_backlog(grpc_endpoint* ep) {
204   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
205   gpr_mu_lock(&te->mu);
206   size_t backlog = te->write_buffer.length;
207   gpr_mu_unlock(&te->mu);
208   return backlog;
209 }
210