• 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)49 static void te_read(grpc_endpoint* ep, grpc_slice_buffer* slices,
50                     grpc_closure* cb) {
51   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
52   grpc_endpoint_read(te->wrapped, slices, cb);
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_CLOSURE_SCHED(te->write_cb, GRPC_ERROR_REF(te->error));
60     te->write_cb = nullptr;
61   }
62 }
63 
te_write(grpc_endpoint * ep,grpc_slice_buffer * slices,grpc_closure * cb,void * arg)64 static void te_write(grpc_endpoint* ep, grpc_slice_buffer* slices,
65                      grpc_closure* cb, void* arg) {
66   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
67   gpr_mu_lock(&te->mu);
68   GPR_ASSERT(te->write_cb == nullptr);
69   if (te->write_buffer.length == 0) {
70     te->last_write = gpr_now(GPR_CLOCK_MONOTONIC);
71   }
72   for (size_t i = 0; i < slices->count; i++) {
73     grpc_slice_buffer_add(&te->write_buffer,
74                           grpc_slice_copy(slices->slices[i]));
75   }
76   te->write_cb = cb;
77   maybe_call_write_cb_locked(te);
78   gpr_mu_unlock(&te->mu);
79 }
80 
te_add_to_pollset(grpc_endpoint * ep,grpc_pollset * pollset)81 static void te_add_to_pollset(grpc_endpoint* ep, grpc_pollset* pollset) {
82   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
83   grpc_endpoint_add_to_pollset(te->wrapped, pollset);
84 }
85 
te_add_to_pollset_set(grpc_endpoint * ep,grpc_pollset_set * pollset_set)86 static void te_add_to_pollset_set(grpc_endpoint* ep,
87                                   grpc_pollset_set* pollset_set) {
88   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
89   grpc_endpoint_add_to_pollset_set(te->wrapped, pollset_set);
90 }
91 
te_delete_from_pollset_set(grpc_endpoint * ep,grpc_pollset_set * pollset_set)92 static void te_delete_from_pollset_set(grpc_endpoint* ep,
93                                        grpc_pollset_set* pollset_set) {
94   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
95   grpc_endpoint_delete_from_pollset_set(te->wrapped, pollset_set);
96 }
97 
te_shutdown(grpc_endpoint * ep,grpc_error * why)98 static void te_shutdown(grpc_endpoint* ep, grpc_error* why) {
99   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
100   gpr_mu_lock(&te->mu);
101   if (te->error == GRPC_ERROR_NONE) {
102     te->error = GRPC_ERROR_REF(why);
103   }
104   maybe_call_write_cb_locked(te);
105   gpr_mu_unlock(&te->mu);
106   grpc_endpoint_shutdown(te->wrapped, why);
107 }
108 
te_destroy(grpc_endpoint * ep)109 static void te_destroy(grpc_endpoint* ep) {
110   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
111   grpc_endpoint_destroy(te->wrapped);
112   gpr_mu_destroy(&te->mu);
113   grpc_slice_buffer_destroy_internal(&te->write_buffer);
114   grpc_slice_buffer_destroy_internal(&te->writing_buffer);
115   GRPC_ERROR_UNREF(te->error);
116   gpr_free(te);
117 }
118 
te_get_resource_user(grpc_endpoint * ep)119 static grpc_resource_user* te_get_resource_user(grpc_endpoint* ep) {
120   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
121   return grpc_endpoint_get_resource_user(te->wrapped);
122 }
123 
te_get_peer(grpc_endpoint * ep)124 static char* te_get_peer(grpc_endpoint* ep) {
125   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
126   return grpc_endpoint_get_peer(te->wrapped);
127 }
128 
te_get_fd(grpc_endpoint * ep)129 static int te_get_fd(grpc_endpoint* ep) {
130   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
131   return grpc_endpoint_get_fd(te->wrapped);
132 }
133 
te_finish_write(void * arg,grpc_error * error)134 static void te_finish_write(void* arg, grpc_error* error) {
135   trickle_endpoint* te = static_cast<trickle_endpoint*>(arg);
136   gpr_mu_lock(&te->mu);
137   te->writing = false;
138   grpc_slice_buffer_reset_and_unref(&te->writing_buffer);
139   gpr_mu_unlock(&te->mu);
140 }
141 
142 static const grpc_endpoint_vtable vtable = {te_read,
143                                             te_write,
144                                             te_add_to_pollset,
145                                             te_add_to_pollset_set,
146                                             te_delete_from_pollset_set,
147                                             te_shutdown,
148                                             te_destroy,
149                                             te_get_resource_user,
150                                             te_get_peer,
151                                             te_get_fd};
152 
grpc_trickle_endpoint_create(grpc_endpoint * wrap,double bytes_per_second)153 grpc_endpoint* grpc_trickle_endpoint_create(grpc_endpoint* wrap,
154                                             double bytes_per_second) {
155   trickle_endpoint* te =
156       static_cast<trickle_endpoint*>(gpr_malloc(sizeof(*te)));
157   te->base.vtable = &vtable;
158   te->wrapped = wrap;
159   te->bytes_per_second = bytes_per_second;
160   te->write_cb = nullptr;
161   gpr_mu_init(&te->mu);
162   grpc_slice_buffer_init(&te->write_buffer);
163   grpc_slice_buffer_init(&te->writing_buffer);
164   te->error = GRPC_ERROR_NONE;
165   te->writing = false;
166   return &te->base;
167 }
168 
ts2dbl(gpr_timespec s)169 static double ts2dbl(gpr_timespec s) {
170   return static_cast<double>(s.tv_sec) + 1e-9 * static_cast<double>(s.tv_nsec);
171 }
172 
grpc_trickle_endpoint_trickle(grpc_endpoint * ep)173 size_t grpc_trickle_endpoint_trickle(grpc_endpoint* ep) {
174   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
175   gpr_mu_lock(&te->mu);
176   if (!te->writing && te->write_buffer.length > 0) {
177     gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC);
178     double elapsed = ts2dbl(gpr_time_sub(now, te->last_write));
179     size_t bytes = static_cast<size_t>(te->bytes_per_second * elapsed);
180     // gpr_log(GPR_DEBUG, "%lf elapsed --> %" PRIdPTR " bytes", elapsed, bytes);
181     if (bytes > 0) {
182       grpc_slice_buffer_move_first(&te->write_buffer,
183                                    GPR_MIN(bytes, te->write_buffer.length),
184                                    &te->writing_buffer);
185       te->writing = true;
186       te->last_write = now;
187       grpc_endpoint_write(
188           te->wrapped, &te->writing_buffer,
189           GRPC_CLOSURE_CREATE(te_finish_write, te, grpc_schedule_on_exec_ctx),
190           nullptr);
191       maybe_call_write_cb_locked(te);
192     }
193   }
194   size_t backlog = te->write_buffer.length;
195   gpr_mu_unlock(&te->mu);
196   return backlog;
197 }
198 
grpc_trickle_get_backlog(grpc_endpoint * ep)199 size_t grpc_trickle_get_backlog(grpc_endpoint* ep) {
200   trickle_endpoint* te = reinterpret_cast<trickle_endpoint*>(ep);
201   gpr_mu_lock(&te->mu);
202   size_t backlog = te->write_buffer.length;
203   gpr_mu_unlock(&te->mu);
204   return backlog;
205 }
206