blob: 3104b1d00d58784ae5938ba3d3b1443fa34f0d19 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Nicolas "Pixel" Nobled5a99852015-01-24 01:27:48 -080034#include "src/core/iomgr/sockaddr.h"
35
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036#include <grpc/grpc.h>
37
38#include <stdlib.h>
39#include <string.h>
40
41#include "src/core/channel/census_filter.h"
42#include "src/core/channel/channel_args.h"
43#include "src/core/channel/client_channel.h"
44#include "src/core/channel/client_setup.h"
45#include "src/core/channel/connected_channel.h"
46#include "src/core/channel/http_client_filter.h"
47#include "src/core/channel/http_filter.h"
ctiller2bbb6c42014-12-17 09:44:44 -080048#include "src/core/iomgr/endpoint.h"
ctiller18b49ab2014-12-09 14:39:16 -080049#include "src/core/iomgr/resolve_address.h"
50#include "src/core/iomgr/tcp_client.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051#include "src/core/surface/channel.h"
52#include "src/core/surface/client.h"
Craig Tiller485d7762015-01-23 12:54:05 -080053#include "src/core/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054#include "src/core/transport/chttp2_transport.h"
55#include <grpc/support/alloc.h>
56#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057#include <grpc/support/sync.h>
58#include <grpc/support/useful.h>
59
60typedef struct setup setup;
61
62/* A single setup request (started via initiate) */
63typedef struct {
64 grpc_client_setup_request *cs_request;
65 setup *setup;
66 /* Resolved addresses, or null if resolution not yet completed */
67 grpc_resolved_addresses *resolved;
68 /* which address in resolved should we pick for the next connection attempt */
69 size_t resolved_index;
70} request;
71
72/* Global setup logic (may be running many simultaneous setup requests, but
73 with only one 'active' */
74struct setup {
75 const char *target;
76 grpc_transport_setup_callback setup_callback;
77 void *setup_user_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078};
79
80static int maybe_try_next_resolved(request *r);
81
82static void done(request *r, int was_successful) {
83 grpc_client_setup_request_finish(r->cs_request, was_successful);
84 if (r->resolved) {
85 grpc_resolved_addresses_destroy(r->resolved);
86 }
87 gpr_free(r);
88}
89
90/* connection callback: tcp is either valid, or null on error */
91static void on_connect(void *rp, grpc_endpoint *tcp) {
92 request *r = rp;
93
94 if (!grpc_client_setup_request_should_continue(r->cs_request)) {
95 if (tcp) {
96 grpc_endpoint_shutdown(tcp);
97 grpc_endpoint_destroy(tcp);
98 }
99 done(r, 0);
100 return;
101 }
102
103 if (!tcp) {
104 if (!maybe_try_next_resolved(r)) {
105 done(r, 0);
106 return;
107 } else {
108 return;
109 }
Craig Tiller43a2b172015-02-18 17:17:51 -0800110 } else if (grpc_client_setup_cb_begin(r->cs_request)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111 grpc_create_chttp2_transport(
112 r->setup->setup_callback, r->setup->setup_user_data,
113 grpc_client_setup_get_channel_args(r->cs_request), tcp, NULL, 0,
114 grpc_client_setup_get_mdctx(r->cs_request), 1);
Craig Tiller43a2b172015-02-18 17:17:51 -0800115 grpc_client_setup_cb_end(r->cs_request);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116 done(r, 1);
117 return;
Craig Tiller43a2b172015-02-18 17:17:51 -0800118 } else {
119 done(r, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120 }
121}
122
123/* attempt to connect to the next available resolved address */
124static int maybe_try_next_resolved(request *r) {
125 grpc_resolved_address *addr;
126 if (!r->resolved) return 0;
127 if (r->resolved_index == r->resolved->naddrs) return 0;
128 addr = &r->resolved->addrs[r->resolved_index++];
ctiller18b49ab2014-12-09 14:39:16 -0800129 grpc_tcp_client_connect(on_connect, r, (struct sockaddr *)&addr->addr,
130 addr->len,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131 grpc_client_setup_request_deadline(r->cs_request));
132 return 1;
133}
134
135/* callback for when our target address has been resolved */
136static void on_resolved(void *rp, grpc_resolved_addresses *resolved) {
137 request *r = rp;
138
139 /* if we're not still the active request, abort */
140 if (!grpc_client_setup_request_should_continue(r->cs_request)) {
141 if (resolved) {
142 grpc_resolved_addresses_destroy(resolved);
143 }
144 done(r, 0);
145 return;
146 }
147
148 if (!resolved) {
149 done(r, 0);
150 return;
151 } else {
152 r->resolved = resolved;
153 r->resolved_index = 0;
154 if (!maybe_try_next_resolved(r)) {
155 done(r, 0);
156 }
157 }
158}
159
160static void initiate_setup(void *sp, grpc_client_setup_request *cs_request) {
161 request *r = gpr_malloc(sizeof(request));
162 r->setup = sp;
163 r->cs_request = cs_request;
164 r->resolved = NULL;
165 r->resolved_index = 0;
166 /* TODO(klempner): Make grpc_resolve_address respect deadline */
167 grpc_resolve_address(r->setup->target, "http", on_resolved, r);
168}
169
170static void done_setup(void *sp) {
171 setup *s = sp;
172 gpr_free((void *)s->target);
173 gpr_free(s);
174}
175
176static grpc_transport_setup_result complete_setup(void *channel_stack,
177 grpc_transport *transport,
178 grpc_mdctx *mdctx) {
179 static grpc_channel_filter const *extra_filters[] = {&grpc_http_client_filter,
180 &grpc_http_filter};
181 return grpc_client_channel_transport_setup_complete(
182 channel_stack, transport, extra_filters, GPR_ARRAY_SIZE(extra_filters),
183 mdctx);
184}
185
186/* Create a client channel:
187 Asynchronously: - resolve target
188 - connect to it (trying alternatives as presented)
189 - perform handshakes */
190grpc_channel *grpc_channel_create(const char *target,
191 const grpc_channel_args *args) {
192 setup *s = gpr_malloc(sizeof(setup));
193 grpc_mdctx *mdctx = grpc_mdctx_create();
194 grpc_channel *channel = NULL;
195#define MAX_FILTERS 3
196 const grpc_channel_filter *filters[MAX_FILTERS];
197 int n = 0;
198 filters[n++] = &grpc_client_surface_filter;
199 if (grpc_channel_args_is_census_enabled(args)) {
200 filters[n++] = &grpc_client_census_filter;
201 }
202 filters[n++] = &grpc_client_channel_filter;
203 GPR_ASSERT(n <= MAX_FILTERS);
204 channel = grpc_channel_create_from_filters(filters, n, args, mdctx, 1);
205
206 s->target = gpr_strdup(target);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800207 s->setup_callback = complete_setup;
208 s->setup_user_data = grpc_channel_get_channel_stack(channel);
209
210 grpc_client_setup_create_and_attach(grpc_channel_get_channel_stack(channel),
211 args, mdctx, initiate_setup, done_setup,
ctiller18b49ab2014-12-09 14:39:16 -0800212 s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213
214 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800215}