channel_create.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <grpc/grpc.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "src/core/channel/census_filter.h"
  37. #include "src/core/channel/channel_args.h"
  38. #include "src/core/channel/client_channel.h"
  39. #include "src/core/channel/client_setup.h"
  40. #include "src/core/channel/connected_channel.h"
  41. #include "src/core/channel/http_client_filter.h"
  42. #include "src/core/channel/http_filter.h"
  43. #include "src/core/endpoint/endpoint.h"
  44. #include "src/core/iomgr/resolve_address.h"
  45. #include "src/core/iomgr/tcp_client.h"
  46. #include "src/core/surface/channel.h"
  47. #include "src/core/surface/client.h"
  48. #include "src/core/transport/chttp2_transport.h"
  49. #include <grpc/support/alloc.h>
  50. #include <grpc/support/log.h>
  51. #include <grpc/support/string.h>
  52. #include <grpc/support/sync.h>
  53. #include <grpc/support/useful.h>
  54. typedef struct setup setup;
  55. /* A single setup request (started via initiate) */
  56. typedef struct {
  57. grpc_client_setup_request *cs_request;
  58. setup *setup;
  59. /* Resolved addresses, or null if resolution not yet completed */
  60. grpc_resolved_addresses *resolved;
  61. /* which address in resolved should we pick for the next connection attempt */
  62. size_t resolved_index;
  63. } request;
  64. /* Global setup logic (may be running many simultaneous setup requests, but
  65. with only one 'active' */
  66. struct setup {
  67. const char *target;
  68. grpc_transport_setup_callback setup_callback;
  69. void *setup_user_data;
  70. };
  71. static int maybe_try_next_resolved(request *r);
  72. static void done(request *r, int was_successful) {
  73. grpc_client_setup_request_finish(r->cs_request, was_successful);
  74. if (r->resolved) {
  75. grpc_resolved_addresses_destroy(r->resolved);
  76. }
  77. gpr_free(r);
  78. }
  79. /* connection callback: tcp is either valid, or null on error */
  80. static void on_connect(void *rp, grpc_endpoint *tcp) {
  81. request *r = rp;
  82. if (!grpc_client_setup_request_should_continue(r->cs_request)) {
  83. if (tcp) {
  84. grpc_endpoint_shutdown(tcp);
  85. grpc_endpoint_destroy(tcp);
  86. }
  87. done(r, 0);
  88. return;
  89. }
  90. if (!tcp) {
  91. if (!maybe_try_next_resolved(r)) {
  92. done(r, 0);
  93. return;
  94. } else {
  95. return;
  96. }
  97. } else {
  98. grpc_create_chttp2_transport(
  99. r->setup->setup_callback, r->setup->setup_user_data,
  100. grpc_client_setup_get_channel_args(r->cs_request), tcp, NULL, 0,
  101. grpc_client_setup_get_mdctx(r->cs_request), 1);
  102. done(r, 1);
  103. return;
  104. }
  105. }
  106. /* attempt to connect to the next available resolved address */
  107. static int maybe_try_next_resolved(request *r) {
  108. grpc_resolved_address *addr;
  109. if (!r->resolved) return 0;
  110. if (r->resolved_index == r->resolved->naddrs) return 0;
  111. addr = &r->resolved->addrs[r->resolved_index++];
  112. grpc_tcp_client_connect(on_connect, r, (struct sockaddr *)&addr->addr,
  113. addr->len,
  114. grpc_client_setup_request_deadline(r->cs_request));
  115. return 1;
  116. }
  117. /* callback for when our target address has been resolved */
  118. static void on_resolved(void *rp, grpc_resolved_addresses *resolved) {
  119. request *r = rp;
  120. /* if we're not still the active request, abort */
  121. if (!grpc_client_setup_request_should_continue(r->cs_request)) {
  122. if (resolved) {
  123. grpc_resolved_addresses_destroy(resolved);
  124. }
  125. done(r, 0);
  126. return;
  127. }
  128. if (!resolved) {
  129. done(r, 0);
  130. return;
  131. } else {
  132. r->resolved = resolved;
  133. r->resolved_index = 0;
  134. if (!maybe_try_next_resolved(r)) {
  135. done(r, 0);
  136. }
  137. }
  138. }
  139. static void initiate_setup(void *sp, grpc_client_setup_request *cs_request) {
  140. request *r = gpr_malloc(sizeof(request));
  141. r->setup = sp;
  142. r->cs_request = cs_request;
  143. r->resolved = NULL;
  144. r->resolved_index = 0;
  145. /* TODO(klempner): Make grpc_resolve_address respect deadline */
  146. grpc_resolve_address(r->setup->target, "http", on_resolved, r);
  147. }
  148. static void done_setup(void *sp) {
  149. setup *s = sp;
  150. gpr_free((void *)s->target);
  151. gpr_free(s);
  152. }
  153. static grpc_transport_setup_result complete_setup(void *channel_stack,
  154. grpc_transport *transport,
  155. grpc_mdctx *mdctx) {
  156. static grpc_channel_filter const *extra_filters[] = {&grpc_http_client_filter,
  157. &grpc_http_filter};
  158. return grpc_client_channel_transport_setup_complete(
  159. channel_stack, transport, extra_filters, GPR_ARRAY_SIZE(extra_filters),
  160. mdctx);
  161. }
  162. /* Create a client channel:
  163. Asynchronously: - resolve target
  164. - connect to it (trying alternatives as presented)
  165. - perform handshakes */
  166. grpc_channel *grpc_channel_create(const char *target,
  167. const grpc_channel_args *args) {
  168. setup *s = gpr_malloc(sizeof(setup));
  169. grpc_mdctx *mdctx = grpc_mdctx_create();
  170. grpc_channel *channel = NULL;
  171. #define MAX_FILTERS 3
  172. const grpc_channel_filter *filters[MAX_FILTERS];
  173. int n = 0;
  174. filters[n++] = &grpc_client_surface_filter;
  175. if (grpc_channel_args_is_census_enabled(args)) {
  176. filters[n++] = &grpc_client_census_filter;
  177. }
  178. filters[n++] = &grpc_client_channel_filter;
  179. GPR_ASSERT(n <= MAX_FILTERS);
  180. channel = grpc_channel_create_from_filters(filters, n, args, mdctx, 1);
  181. s->target = gpr_strdup(target);
  182. s->setup_callback = complete_setup;
  183. s->setup_user_data = grpc_channel_get_channel_stack(channel);
  184. grpc_client_setup_create_and_attach(grpc_channel_get_channel_stack(channel),
  185. args, mdctx, initiate_setup, done_setup,
  186. s);
  187. return channel;
  188. }