aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/channel/channel_stack_builder.c
blob: df45f13c8130abcab47df5c2b6fd3e76a2820155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 *
 * Copyright 2016 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "src/core/lib/channel/channel_stack_builder.h"

#include <string.h>

#include <grpc/support/alloc.h>
#include <grpc/support/string_util.h>

grpc_tracer_flag grpc_trace_channel_stack_builder =
    GRPC_TRACER_INITIALIZER(false, "channel_stack_builder");

typedef struct filter_node {
  struct filter_node *next;
  struct filter_node *prev;
  const grpc_channel_filter *filter;
  grpc_post_filter_create_init_func init;
  void *init_arg;
} filter_node;

struct grpc_channel_stack_builder {
  // sentinel nodes for filters that have been added
  filter_node begin;
  filter_node end;
  // various set/get-able parameters
  grpc_channel_args *args;
  grpc_transport *transport;
  char *target;
  const char *name;
};

struct grpc_channel_stack_builder_iterator {
  grpc_channel_stack_builder *builder;
  filter_node *node;
};

grpc_channel_stack_builder *grpc_channel_stack_builder_create(void) {
  grpc_channel_stack_builder *b =
      (grpc_channel_stack_builder *)gpr_zalloc(sizeof(*b));

  b->begin.filter = NULL;
  b->end.filter = NULL;
  b->begin.next = &b->end;
  b->begin.prev = &b->end;
  b->end.next = &b->begin;
  b->end.prev = &b->begin;

  return b;
}

void grpc_channel_stack_builder_set_target(grpc_channel_stack_builder *b,
                                           const char *target) {
  gpr_free(b->target);
  b->target = gpr_strdup(target);
}

const char *grpc_channel_stack_builder_get_target(
    grpc_channel_stack_builder *b) {
  return b->target;
}

static grpc_channel_stack_builder_iterator *create_iterator_at_filter_node(
    grpc_channel_stack_builder *builder, filter_node *node) {
  grpc_channel_stack_builder_iterator *it =
      (grpc_channel_stack_builder_iterator *)gpr_malloc(sizeof(*it));
  it->builder = builder;
  it->node = node;
  return it;
}

void grpc_channel_stack_builder_iterator_destroy(
    grpc_channel_stack_builder_iterator *it) {
  gpr_free(it);
}

grpc_channel_stack_builder_iterator *
grpc_channel_stack_builder_create_iterator_at_first(
    grpc_channel_stack_builder *builder) {
  return create_iterator_at_filter_node(builder, &builder->begin);
}

grpc_channel_stack_builder_iterator *
grpc_channel_stack_builder_create_iterator_at_last(
    grpc_channel_stack_builder *builder) {
  return create_iterator_at_filter_node(builder, &builder->end);
}

bool grpc_channel_stack_builder_iterator_is_end(
    grpc_channel_stack_builder_iterator *iterator) {
  return iterator->node == &iterator->builder->end;
}

const char *grpc_channel_stack_builder_iterator_filter_name(
    grpc_channel_stack_builder_iterator *iterator) {
  if (iterator->node->filter == NULL) return NULL;
  return iterator->node->filter->name;
}

bool grpc_channel_stack_builder_move_next(
    grpc_channel_stack_builder_iterator *iterator) {
  if (iterator->node == &iterator->builder->end) return false;
  iterator->node = iterator->node->next;
  return true;
}

bool grpc_channel_stack_builder_move_prev(
    grpc_channel_stack_builder_iterator *iterator) {
  if (iterator->node == &iterator->builder->begin) return false;
  iterator->node = iterator->node->prev;
  return true;
}

grpc_channel_stack_builder_iterator *grpc_channel_stack_builder_iterator_find(
    grpc_channel_stack_builder *builder, const char *filter_name) {
  GPR_ASSERT(filter_name != NULL);
  grpc_channel_stack_builder_iterator *it =
      grpc_channel_stack_builder_create_iterator_at_first(builder);
  while (grpc_channel_stack_builder_move_next(it)) {
    if (grpc_channel_stack_builder_iterator_is_end(it)) break;
    const char *filter_name_at_it =
        grpc_channel_stack_builder_iterator_filter_name(it);
    if (strcmp(filter_name, filter_name_at_it) == 0) break;
  }
  return it;
}

bool grpc_channel_stack_builder_move_prev(
    grpc_channel_stack_builder_iterator *iterator);

void grpc_channel_stack_builder_set_name(grpc_channel_stack_builder *builder,
                                         const char *name) {
  GPR_ASSERT(builder->name == NULL);
  builder->name = name;
}

void grpc_channel_stack_builder_set_channel_arguments(
    grpc_exec_ctx *exec_ctx, grpc_channel_stack_builder *builder,
    const grpc_channel_args *args) {
  if (builder->args != NULL) {
    grpc_channel_args_destroy(exec_ctx, builder->args);
  }
  builder->args = grpc_channel_args_copy(args);
}

void grpc_channel_stack_builder_set_transport(
    grpc_channel_stack_builder *builder, grpc_transport *transport) {
  GPR_ASSERT(builder->transport == NULL);
  builder->transport = transport;
}

grpc_transport *grpc_channel_stack_builder_get_transport(
    grpc_channel_stack_builder *builder) {
  return builder->transport;
}

const grpc_channel_args *grpc_channel_stack_builder_get_channel_arguments(
    grpc_channel_stack_builder *builder) {
  return builder->args;
}

bool grpc_channel_stack_builder_append_filter(
    grpc_channel_stack_builder *builder, const grpc_channel_filter *filter,
    grpc_post_filter_create_init_func post_init_func, void *user_data) {
  grpc_channel_stack_builder_iterator *it =
      grpc_channel_stack_builder_create_iterator_at_last(builder);
  bool ok = grpc_channel_stack_builder_add_filter_before(
      it, filter, post_init_func, user_data);
  grpc_channel_stack_builder_iterator_destroy(it);
  return ok;
}

bool grpc_channel_stack_builder_remove_filter(
    grpc_channel_stack_builder *builder, const char *filter_name) {
  grpc_channel_stack_builder_iterator *it =
      grpc_channel_stack_builder_iterator_find(builder, filter_name);
  if (grpc_channel_stack_builder_iterator_is_end(it)) {
    grpc_channel_stack_builder_iterator_destroy(it);
    return false;
  }
  it->node->prev->next = it->node->next;
  it->node->next->prev = it->node->prev;
  gpr_free(it->node);
  grpc_channel_stack_builder_iterator_destroy(it);
  return true;
}

bool grpc_channel_stack_builder_prepend_filter(
    grpc_channel_stack_builder *builder, const grpc_channel_filter *filter,
    grpc_post_filter_create_init_func post_init_func, void *user_data) {
  grpc_channel_stack_builder_iterator *it =
      grpc_channel_stack_builder_create_iterator_at_first(builder);
  bool ok = grpc_channel_stack_builder_add_filter_after(
      it, filter, post_init_func, user_data);
  grpc_channel_stack_builder_iterator_destroy(it);
  return ok;
}

static void add_after(filter_node *before, const grpc_channel_filter *filter,
                      grpc_post_filter_create_init_func post_init_func,
                      void *user_data) {
  filter_node *new = (filter_node *)gpr_malloc(sizeof(*new));
  new->next = before->next;
  new->prev = before;
  new->next->prev = new->prev->next = new;
  new->filter = filter;
  new->init = post_init_func;
  new->init_arg = user_data;
}

bool grpc_channel_stack_builder_add_filter_before(
    grpc_channel_stack_builder_iterator *iterator,
    const grpc_channel_filter *filter,
    grpc_post_filter_create_init_func post_init_func, void *user_data) {
  if (iterator->node == &iterator->builder->begin) return false;
  add_after(iterator->node->prev, filter, post_init_func, user_data);
  return true;
}

bool grpc_channel_stack_builder_add_filter_after(
    grpc_channel_stack_builder_iterator *iterator,
    const grpc_channel_filter *filter,
    grpc_post_filter_create_init_func post_init_func, void *user_data) {
  if (iterator->node == &iterator->builder->end) return false;
  add_after(iterator->node, filter, post_init_func, user_data);
  return true;
}

void grpc_channel_stack_builder_destroy(grpc_exec_ctx *exec_ctx,
                                        grpc_channel_stack_builder *builder) {
  filter_node *p = builder->begin.next;
  while (p != &builder->end) {
    filter_node *next = p->next;
    gpr_free(p);
    p = next;
  }
  if (builder->args != NULL) {
    grpc_channel_args_destroy(exec_ctx, builder->args);
  }
  gpr_free(builder->target);
  gpr_free(builder);
}

grpc_error *grpc_channel_stack_builder_finish(
    grpc_exec_ctx *exec_ctx, grpc_channel_stack_builder *builder,
    size_t prefix_bytes, int initial_refs, grpc_iomgr_cb_func destroy,
    void *destroy_arg, void **result) {
  // count the number of filters
  size_t num_filters = 0;
  for (filter_node *p = builder->begin.next; p != &builder->end; p = p->next) {
    num_filters++;
  }

  // create an array of filters
  const grpc_channel_filter **filters =
      gpr_malloc(sizeof(*filters) * num_filters);
  size_t i = 0;
  for (filter_node *p = builder->begin.next; p != &builder->end; p = p->next) {
    filters[i++] = p->filter;
  }

  // calculate the size of the channel stack
  size_t channel_stack_size = grpc_channel_stack_size(filters, num_filters);

  // allocate memory, with prefix_bytes followed by channel_stack_size
  *result = gpr_zalloc(prefix_bytes + channel_stack_size);
  // fetch a pointer to the channel stack
  grpc_channel_stack *channel_stack =
      (grpc_channel_stack *)((char *)(*result) + prefix_bytes);
  // and initialize it
  grpc_error *error = grpc_channel_stack_init(
      exec_ctx, initial_refs, destroy,
      destroy_arg == NULL ? *result : destroy_arg, filters, num_filters,
      builder->args, builder->transport, builder->name, channel_stack);

  if (error != GRPC_ERROR_NONE) {
    grpc_channel_stack_destroy(exec_ctx, channel_stack);
    gpr_free(*result);
    *result = NULL;
  } else {
    // run post-initialization functions
    i = 0;
    for (filter_node *p = builder->begin.next; p != &builder->end;
         p = p->next) {
      if (p->init != NULL) {
        p->init(channel_stack, grpc_channel_stack_element(channel_stack, i),
                p->init_arg);
      }
      i++;
    }
  }

  grpc_channel_stack_builder_destroy(exec_ctx, builder);
  gpr_free((grpc_channel_filter **)filters);

  return error;
}