blob: b17f3d9a613dc91c30517c3b11a18b3a7b07dcc1 [file] [log] [blame]
Craig Tiller1a61b172015-02-16 11:53:47 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tiller1a61b172015-02-16 11:53:47 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Craig Tiller1a61b172015-02-16 11:53:47 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller1a61b172015-02-16 11:53:47 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Craig Tiller1a61b172015-02-16 11:53:47 -080016 *
17 */
18
mlumishb892a272014-12-09 16:28:23 -080019#include "channel.h"
20
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070021#include <ext/standard/php_var.h>
22#include <ext/standard/sha1.h>
23#if PHP_MAJOR_VERSION < 7
24#include <ext/standard/php_smart_str.h>
25#else
26#include <zend_smart_str.h>
27#endif
murgatroid998242ba72015-04-01 15:29:44 -070028#include <ext/spl/spl_exceptions.h>
murgatroid998242ba72015-04-01 15:29:44 -070029#include <zend_exceptions.h>
mlumishb892a272014-12-09 16:28:23 -080030
murgatroid998242ba72015-04-01 15:29:44 -070031#include <grpc/grpc_security.h>
Zhouyihai Dingd3b55242018-01-22 12:52:56 -080032#include <grpc/support/alloc.h>
mlumishb892a272014-12-09 16:28:23 -080033
Stanley Cheunga63fdd02015-08-11 15:11:11 -070034#include "completion_queue.h"
Stanley Cheung9c0b35e2015-10-21 17:07:56 -070035#include "channel_credentials.h"
Stanley Cheunga63fdd02015-08-11 15:11:11 -070036#include "timeval.h"
mlumishb892a272014-12-09 16:28:23 -080037
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080038zend_class_entry *grpc_ce_channel;
thinkeroufe037cb2018-05-16 12:51:28 +080039PHP_GRPC_DECLARE_OBJECT_HANDLER(channel_ce_handlers)
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070040static gpr_mu global_persistent_list_mu;
41int le_plink;
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -070042int le_bound;
ZhouyihaiDing5c770352018-04-09 13:20:31 -070043extern HashTable grpc_persistent_list;
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -070044extern HashTable grpc_target_upper_bound_map;
45
46void free_grpc_channel_wrapper(grpc_channel_wrapper* channel, bool free_channel) {
47 if (free_channel) {
48 grpc_channel_destroy(channel->wrapped);
49 channel->wrapped = NULL;
50 }
51 free(channel->target);
52 free(channel->args_hashstr);
53 free(channel->creds_hashstr);
54 free(channel->key);
55 channel->target = NULL;
56 channel->args_hashstr = NULL;
57 channel->creds_hashstr = NULL;
58 channel->key = NULL;
59}
60
61void php_grpc_channel_ref(grpc_channel_wrapper* wrapper) {
62 gpr_mu_lock(&wrapper->mu);
63 wrapper->ref_count += 1;
64 gpr_mu_unlock(&wrapper->mu);
65}
66
67void php_grpc_channel_unref(grpc_channel_wrapper* wrapper) {
68 gpr_mu_lock(&wrapper->mu);
69 wrapper->ref_count -= 1;
70 if (wrapper->ref_count == 0) {
71 free_grpc_channel_wrapper(wrapper, true);
72 gpr_mu_unlock(&wrapper->mu);
73 free(wrapper);
74 wrapper = NULL;
75 return;
76 }
77 gpr_mu_unlock(&wrapper->mu);
78}
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080079
mlumishb892a272014-12-09 16:28:23 -080080/* Frees and destroys an instance of wrapped_grpc_channel */
thinkerou011d1ef2016-07-27 09:44:49 +080081PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_channel)
ZhouyihaiDingb00a9252018-04-02 16:14:57 -070082 // In_persistent_list is used when the user don't close the channel,
83 // In this case, channels not in the list should be freed.
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070084 if (p->wrapper != NULL) {
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -070085 php_grpc_channel_unref(p->wrapper);
86 p->wrapper = NULL;
mlumishb892a272014-12-09 16:28:23 -080087 }
thinkerou011d1ef2016-07-27 09:44:49 +080088PHP_GRPC_FREE_WRAPPED_FUNC_END()
89
mlumishb892a272014-12-09 16:28:23 -080090/* Initializes an instance of wrapped_grpc_channel to be associated with an
91 * object of a class specified by class_type */
thinkeroudba5b0c2016-07-27 18:39:16 +080092php_grpc_zend_object create_wrapped_grpc_channel(zend_class_entry *class_type
93 TSRMLS_DC) {
thinkerouba75c012016-07-28 02:30:08 +080094 PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_channel);
mlumishb892a272014-12-09 16:28:23 -080095 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
96 object_properties_init(&intern->std, class_type);
thinkeroudc673c52016-07-28 09:49:38 +080097 PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_channel, channel_ce_handlers);
thinkeroudba5b0c2016-07-27 18:39:16 +080098}
thinkerou6f9d30b2016-07-27 03:19:03 +080099
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700100int php_grpc_read_args_array(zval *args_array,
101 grpc_channel_args *args TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -0800102 HashTable *array_hash;
mlumishb892a272014-12-09 16:28:23 -0800103 int args_index;
mlumishb892a272014-12-09 16:28:23 -0800104 array_hash = Z_ARRVAL_P(args_array);
thinkerou6f9d30b2016-07-27 03:19:03 +0800105 if (!array_hash) {
106 zend_throw_exception(spl_ce_InvalidArgumentException,
Stanley Cheungc1f25fb2016-07-29 13:41:22 -0700107 "array_hash is NULL", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700108 return FAILURE;
thinkerou6f9d30b2016-07-27 03:19:03 +0800109 }
mlumishb892a272014-12-09 16:28:23 -0800110 args->num_args = zend_hash_num_elements(array_hash);
111 args->args = ecalloc(args->num_args, sizeof(grpc_arg));
112 args_index = 0;
thinkerou6f9d30b2016-07-27 03:19:03 +0800113
thinkerouba75c012016-07-28 02:30:08 +0800114 char *key = NULL;
115 zval *data;
116 int key_type;
117
118 PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type, data)
119 if (key_type != HASH_KEY_IS_STRING) {
mlumishb892a272014-12-09 16:28:23 -0800120 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800121 "args keys must be strings", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700122 return FAILURE;
mlumishb892a272014-12-09 16:28:23 -0800123 }
124 args->args[args_index].key = key;
thinkeroua3730b72016-07-20 16:59:54 +0800125 switch (Z_TYPE_P(data)) {
126 case IS_LONG:
127 args->args[args_index].value.integer = (int)Z_LVAL_P(data);
128 args->args[args_index].type = GRPC_ARG_INTEGER;
129 break;
130 case IS_STRING:
131 args->args[args_index].value.string = Z_STRVAL_P(data);
132 args->args[args_index].type = GRPC_ARG_STRING;
133 break;
134 default:
135 zend_throw_exception(spl_ce_InvalidArgumentException,
thinkerouba75c012016-07-28 02:30:08 +0800136 "args values must be int or string", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700137 return FAILURE;
thinkeroua3730b72016-07-20 16:59:54 +0800138 }
139 args_index++;
thinkerouba75c012016-07-28 02:30:08 +0800140 PHP_GRPC_HASH_FOREACH_END()
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700141 return SUCCESS;
142}
143
144void generate_sha1_str(char *sha1str, char *str, php_grpc_int len) {
145 PHP_SHA1_CTX context;
146 unsigned char digest[20];
147 sha1str[0] = '\0';
148 PHP_SHA1Init(&context);
149 PHP_GRPC_SHA1Update(&context, str, len);
150 PHP_SHA1Final(digest, &context);
151 make_sha1_digest(sha1str, digest);
152}
153
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700154bool php_grpc_persistent_list_delete_unused_channel(
155 char* target,
156 target_bound_le_t* target_bound_status TSRMLS_DC) {
157 zval *data;
158 PHP_GRPC_HASH_FOREACH_VAL_START(&grpc_persistent_list, data)
159 php_grpc_zend_resource *rsrc = (php_grpc_zend_resource*) PHP_GRPC_HASH_VALPTR_TO_VAL(data)
160 if (rsrc == NULL) {
161 break;
162 }
163 channel_persistent_le_t* le = rsrc->ptr;
164 // Find the channel sharing the same target.
165 if (strcmp(le->channel->target, target) == 0) {
166 // ref_count=1 means that only the map holds the reference to the channel.
167 if (le->channel->ref_count == 1) {
168 php_grpc_delete_persistent_list_entry(le->channel->key,
169 strlen(le->channel->key)
170 TSRMLS_CC);
171 target_bound_status->current_count -= 1;
172 if (target_bound_status->current_count < target_bound_status->upper_bound) {
173 return true;
174 }
175 }
176 }
177 PHP_GRPC_HASH_FOREACH_END()
178 return false;
179}
180
181target_bound_le_t* update_and_get_target_upper_bound(char* target, int bound) {
182 php_grpc_zend_resource *rsrc;
183 target_bound_le_t* target_bound_status;
184 php_grpc_int key_len = strlen(target);
185 if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&grpc_target_upper_bound_map, target,
186 key_len, rsrc))) {
187 // Target is not not persisted.
188 php_grpc_zend_resource new_rsrc;
189 target_bound_status = malloc(sizeof(target_bound_le_t));
190 if (bound == -1) {
191 // If the bound is not set, use 1 as default.s
192 bound = 1;
193 }
194 target_bound_status->upper_bound = bound;
195 // Init current_count with 1. It should be add 1 when the channel is successfully
196 // created and minus 1 when it is removed from the persistent list.
197 target_bound_status->current_count = 0;
198 new_rsrc.type = le_bound;
199 new_rsrc.ptr = target_bound_status;
200 gpr_mu_lock(&global_persistent_list_mu);
201 PHP_GRPC_PERSISTENT_LIST_UPDATE(&grpc_target_upper_bound_map,
202 target, key_len, (void *)&new_rsrc);
203 gpr_mu_unlock(&global_persistent_list_mu);
204 } else {
205 // The target already in the map recording the upper bound.
206 // If no newer bound set, use the original now.
207 target_bound_status = (target_bound_le_t *)rsrc->ptr;
208 if (bound != -1) {
209 target_bound_status->upper_bound = bound;
210 }
211 }
212 return target_bound_status;
213}
214
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700215void create_channel(
216 wrapped_grpc_channel *channel,
217 char *target,
218 grpc_channel_args args,
219 wrapped_grpc_channel_credentials *creds) {
220 if (creds == NULL) {
221 channel->wrapper->wrapped = grpc_insecure_channel_create(target, &args,
222 NULL);
223 } else {
224 channel->wrapper->wrapped =
225 grpc_secure_channel_create(creds->wrapped, target, &args, NULL);
226 }
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700227 // There is an Grpc\Channel object refer to it.
228 php_grpc_channel_ref(channel->wrapper);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700229 efree(args.args);
230}
231
232void create_and_add_channel_to_persistent_list(
233 wrapped_grpc_channel *channel,
234 char *target,
235 grpc_channel_args args,
236 wrapped_grpc_channel_credentials *creds,
237 char *key,
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700238 php_grpc_int key_len,
239 int target_upper_bound TSRMLS_DC) {
240 target_bound_le_t* target_bound_status =
241 update_and_get_target_upper_bound(target, target_upper_bound);
242 // Check the upper bound status before inserting to the persistent map.
243 if (target_bound_status->current_count >=
244 target_bound_status->upper_bound) {
245 if (!php_grpc_persistent_list_delete_unused_channel(
246 target, target_bound_status TSRMLS_CC)) {
247 // If no channel can be deleted from the persistent map,
248 // do not persist this one.
249 create_channel(channel, target, args, creds);
250 php_printf("[Warning] The number of channel for the"
251 " target %s is maxed out bounded.\n", target);
252 php_printf("[Warning] Target upper bound: %d. Current size: %d.\n",
253 target_bound_status->upper_bound,
254 target_bound_status->current_count);
255 php_printf("[Warning] Target %s will not be persisted.\n", target);
256 return;
257 }
258 }
259 // There is space in the persistent map.
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700260 php_grpc_zend_resource new_rsrc;
261 channel_persistent_le_t *le;
262 // this links each persistent list entry to a destructor
263 new_rsrc.type = le_plink;
264 le = malloc(sizeof(channel_persistent_le_t));
265
266 create_channel(channel, target, args, creds);
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700267 target_bound_status->current_count += 1;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700268
269 le->channel = channel->wrapper;
270 new_rsrc.ptr = le;
271 gpr_mu_lock(&global_persistent_list_mu);
ZhouyihaiDing5c770352018-04-09 13:20:31 -0700272 PHP_GRPC_PERSISTENT_LIST_UPDATE(&grpc_persistent_list, key, key_len,
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700273 (void *)&new_rsrc);
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700274 // Persistent map refer to it.
275 php_grpc_channel_ref(channel->wrapper);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700276 gpr_mu_unlock(&global_persistent_list_mu);
thinkerou6f9d30b2016-07-27 03:19:03 +0800277}
thinkeroua3730b72016-07-20 16:59:54 +0800278
mlumishb892a272014-12-09 16:28:23 -0800279/**
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700280 * Construct an instance of the Channel class.
281 *
282 * By default, the underlying grpc_channel is "persistent". That is, given
283 * the same set of parameters passed to the constructor, the same underlying
284 * grpc_channel will be returned.
285 *
286 * If the $args array contains a "credentials" key mapping to a
287 * ChannelCredentials object, a secure channel will be created with those
288 * credentials.
289 *
290 * If the $args array contains a "force_new" key mapping to a boolean value
Stanley Cheung5d559482017-08-22 13:15:01 -0700291 * of "true", a new and separate underlying grpc_channel will be created
292 * and returned. This will not affect existing channels.
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700293 *
mlumishb892a272014-12-09 16:28:23 -0800294 * @param string $target The hostname to associate with this channel
thinkerouefbc9e72016-08-16 20:00:36 +0800295 * @param array $args_array The arguments to pass to the Channel
mlumishb892a272014-12-09 16:28:23 -0800296 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800297PHP_METHOD(Channel, __construct) {
thinkeroufe037cb2018-05-16 12:51:28 +0800298 wrapped_grpc_channel *channel =
299 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis());
thinkeroua3730b72016-07-20 16:59:54 +0800300 zval *creds_obj = NULL;
thinkeroua3730b72016-07-20 16:59:54 +0800301 char *target;
thinkerou19304682016-07-22 02:43:19 +0800302 php_grpc_int target_length;
mlumishb892a272014-12-09 16:28:23 -0800303 zval *args_array = NULL;
304 grpc_channel_args args;
305 HashTable *array_hash;
Stanley Cheungaeea1022015-10-21 17:00:49 -0700306 wrapped_grpc_channel_credentials *creds = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700307 php_grpc_zend_resource *rsrc;
308 bool force_new = false;
309 zval *force_new_obj = NULL;
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700310 int target_upper_bound = -1;
thinkeroua3730b72016-07-20 16:59:54 +0800311
Stanley Cheungf77a4ad2016-02-16 09:45:51 -0800312 /* "sa" == 1 string, 1 array */
313 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &target,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800314 &target_length, &args_array) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800315 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800316 "Channel expects a string and an array", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800317 return;
318 }
Stanley Cheungcccf9292016-02-12 16:37:19 -0800319 array_hash = Z_ARRVAL_P(args_array);
thinkerouba75c012016-07-28 02:30:08 +0800320 if (php_grpc_zend_hash_find(array_hash, "credentials", sizeof("credentials"),
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700321 (void **)&creds_obj) == SUCCESS) {
thinkerouba75c012016-07-28 02:30:08 +0800322 if (Z_TYPE_P(creds_obj) == IS_NULL) {
Stanley Cheungcccf9292016-02-12 16:37:19 -0800323 creds = NULL;
thinkerouba75c012016-07-28 02:30:08 +0800324 php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials"));
325 } else if (PHP_GRPC_GET_CLASS_ENTRY(creds_obj) !=
326 grpc_ce_channel_credentials) {
Stanley Cheungcccf9292016-02-12 16:37:19 -0800327 zend_throw_exception(spl_ce_InvalidArgumentException,
328 "credentials must be a ChannelCredentials object",
329 1 TSRMLS_CC);
330 return;
mlumishb892a272014-12-09 16:28:23 -0800331 } else {
thinkeroufe037cb2018-05-16 12:51:28 +0800332 creds = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel_credentials,
333 creds_obj);
thinkerouba75c012016-07-28 02:30:08 +0800334 php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials"));
thinkeroua3730b72016-07-20 16:59:54 +0800335 }
336 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700337 if (php_grpc_zend_hash_find(array_hash, "force_new", sizeof("force_new"),
338 (void **)&force_new_obj) == SUCCESS) {
339 if (PHP_GRPC_BVAL_IS_TRUE(force_new_obj)) {
340 force_new = true;
341 }
342 php_grpc_zend_hash_del(array_hash, "force_new", sizeof("force_new"));
Stanley Cheungcccf9292016-02-12 16:37:19 -0800343 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700344
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700345 if (php_grpc_zend_hash_find(array_hash, "grpc_target_persist_bound",
346 sizeof("grpc_target_persist_bound"),
347 (void **)&force_new_obj) == SUCCESS) {
348 if (Z_TYPE_P(force_new_obj) != IS_LONG) {
349 zend_throw_exception(spl_ce_InvalidArgumentException,
350 "plist_bound must be a number",
351 1 TSRMLS_CC);
352 }
353 target_upper_bound = (int)Z_LVAL_P(force_new_obj);
354 php_grpc_zend_hash_del(array_hash, "grpc_target_persist_bound",
355 sizeof("grpc_target_persist_bound"));
356 }
357
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700358 // parse the rest of the channel args array
359 if (php_grpc_read_args_array(args_array, &args TSRMLS_CC) == FAILURE) {
Zhouyihai Dingb6cdfca2018-01-13 16:25:28 -0800360 efree(args.args);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700361 return;
362 }
363
364 // Construct a hashkey for the persistent channel
365 // Currently, the hashkey contains 3 parts:
366 // 1. hostname
367 // 2. hash value of the channel args array (excluding "credentials"
368 // and "force_new")
369 // 3. (optional) hash value of the ChannelCredentials object
370 php_serialize_data_t var_hash;
371 smart_str buf = {0};
372 PHP_VAR_SERIALIZE_INIT(var_hash);
373 PHP_GRPC_VAR_SERIALIZE(&buf, args_array, &var_hash);
374 PHP_VAR_SERIALIZE_DESTROY(var_hash);
375
376 char sha1str[41];
377 generate_sha1_str(sha1str, PHP_GRPC_SERIALIZED_BUF_STR(buf),
378 PHP_GRPC_SERIALIZED_BUF_LEN(buf));
379
380 php_grpc_int key_len = target_length + strlen(sha1str);
381 if (creds != NULL && creds->hashstr != NULL) {
382 key_len += strlen(creds->hashstr);
383 }
384 char *key = malloc(key_len + 1);
385 strcpy(key, target);
386 strcat(key, sha1str);
387 if (creds != NULL && creds->hashstr != NULL) {
388 strcat(key, creds->hashstr);
389 }
390 channel->wrapper = malloc(sizeof(grpc_channel_wrapper));
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700391 channel->wrapper->ref_count = 0;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700392 channel->wrapper->key = key;
Stanley Cheung5d559482017-08-22 13:15:01 -0700393 channel->wrapper->target = strdup(target);
394 channel->wrapper->args_hashstr = strdup(sha1str);
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800395 channel->wrapper->creds_hashstr = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700396 if (creds != NULL && creds->hashstr != NULL) {
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800397 php_grpc_int creds_hashstr_len = strlen(creds->hashstr);
398 char *channel_creds_hashstr = malloc(creds_hashstr_len + 1);
399 strcpy(channel_creds_hashstr, creds->hashstr);
400 channel->wrapper->creds_hashstr = channel_creds_hashstr;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700401 }
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800402
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700403 gpr_mu_init(&channel->wrapper->mu);
404 smart_str_free(&buf);
Stanley Cheung5d559482017-08-22 13:15:01 -0700405 if (force_new || (creds != NULL && creds->has_call_creds)) {
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700406 // If the ChannelCredentials object was composed with a CallCredentials
407 // object, there is no way we can tell them apart. Do NOT persist
408 // them. They should be individually destroyed.
409 create_channel(channel, target, args, creds);
ZhouyihaiDing5c770352018-04-09 13:20:31 -0700410 } else if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&grpc_persistent_list, key,
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700411 key_len, rsrc))) {
412 create_and_add_channel_to_persistent_list(
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700413 channel, target, args, creds, key, key_len, target_upper_bound TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700414 } else {
415 // Found a previously stored channel in the persistent list
416 channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr;
417 if (strcmp(target, le->channel->target) != 0 ||
418 strcmp(sha1str, le->channel->args_hashstr) != 0 ||
419 (creds != NULL && creds->hashstr != NULL &&
420 strcmp(creds->hashstr, le->channel->creds_hashstr) != 0)) {
421 // somehow hash collision
422 create_and_add_channel_to_persistent_list(
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700423 channel, target, args, creds, key, key_len, target_upper_bound TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700424 } else {
Zhouyihai Dingb6cdfca2018-01-13 16:25:28 -0800425 efree(args.args);
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700426 free_grpc_channel_wrapper(channel->wrapper, false);
427 gpr_mu_destroy(&channel->wrapper->mu);
Zhouyihai Ding8a845932018-01-22 13:39:37 -0800428 free(channel->wrapper);
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700429 channel->wrapper = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700430 channel->wrapper = le->channel;
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700431 // One more Grpc\Channel object refer to it.
432 php_grpc_channel_ref(channel->wrapper);
433 update_and_get_target_upper_bound(target, target_upper_bound);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700434 }
435 }
mlumishb892a272014-12-09 16:28:23 -0800436}
437
438/**
Stanley Cheungdb98e082015-07-27 10:19:45 -0700439 * Get the endpoint this call/stream is connected to
440 * @return string The URI of the endpoint
441 */
442PHP_METHOD(Channel, getTarget) {
thinkeroufe037cb2018-05-16 12:51:28 +0800443 wrapped_grpc_channel *channel =
444 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis());
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700445 if (channel->wrapper == NULL) {
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700446 zend_throw_exception(spl_ce_RuntimeException,
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700447 "getTarget error."
448 "Channel is already closed.", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700449 return;
450 }
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700451 gpr_mu_lock(&channel->wrapper->mu);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700452 char *target = grpc_channel_get_target(channel->wrapper->wrapped);
453 gpr_mu_unlock(&channel->wrapper->mu);
Zhouyihai Dingd3b55242018-01-22 12:52:56 -0800454 PHP_GRPC_RETVAL_STRING(target, 1);
455 gpr_free(target);
Stanley Cheungdb98e082015-07-27 10:19:45 -0700456}
457
458/**
Stanley Cheunge63354a2015-08-10 15:46:42 -0700459 * Get the connectivity state of the channel
thinkerouefbc9e72016-08-16 20:00:36 +0800460 * @param bool $try_to_connect Try to connect on the channel (optional)
Stanley Cheunge63354a2015-08-10 15:46:42 -0700461 * @return long The grpc connectivity state
462 */
463PHP_METHOD(Channel, getConnectivityState) {
thinkeroufe037cb2018-05-16 12:51:28 +0800464 wrapped_grpc_channel *channel =
465 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis());
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700466 if (channel->wrapper == NULL) {
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700467 zend_throw_exception(spl_ce_RuntimeException,
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700468 "getConnectivityState error."
469 "Channel is already closed.", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700470 return;
471 }
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700472 gpr_mu_lock(&channel->wrapper->mu);
thinkeroua3730b72016-07-20 16:59:54 +0800473 bool try_to_connect = false;
Stanley Cheunge63354a2015-08-10 15:46:42 -0700474 /* "|b" == 1 optional bool */
thinkeroua3730b72016-07-20 16:59:54 +0800475 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect)
476 == FAILURE) {
Stanley Cheunge63354a2015-08-10 15:46:42 -0700477 zend_throw_exception(spl_ce_InvalidArgumentException,
478 "getConnectivityState expects a bool", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700479 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheunge63354a2015-08-10 15:46:42 -0700480 return;
481 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700482 int state = grpc_channel_check_connectivity_state(channel->wrapper->wrapped,
483 (int)try_to_connect);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700484 gpr_mu_unlock(&channel->wrapper->mu);
485 RETURN_LONG(state);
Stanley Cheunge63354a2015-08-10 15:46:42 -0700486}
487
488/**
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700489 * Watch the connectivity state of the channel until it changed
thinkerouefbc9e72016-08-16 20:00:36 +0800490 * @param long $last_state The previous connectivity state of the channel
491 * @param Timeval $deadline_obj The deadline this function should wait until
Stanley Cheung4c5c7b82015-08-12 16:28:58 -0700492 * @return bool If the connectivity state changes from last_state
493 * before deadline
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700494 */
495PHP_METHOD(Channel, watchConnectivityState) {
thinkeroufe037cb2018-05-16 12:51:28 +0800496 wrapped_grpc_channel *channel =
497 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis());
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700498 if (channel->wrapper == NULL) {
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700499 zend_throw_exception(spl_ce_RuntimeException,
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700500 "watchConnectivityState error"
501 "Channel is already closed.", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700502 return;
503 }
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700504 gpr_mu_lock(&channel->wrapper->mu);
thinkerou19304682016-07-22 02:43:19 +0800505 php_grpc_long last_state;
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700506 zval *deadline_obj;
thinkeroua3730b72016-07-20 16:59:54 +0800507
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700508 /* "lO" == 1 long 1 object */
509 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO",
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700510 &last_state, &deadline_obj,
511 grpc_ce_timeval) == FAILURE) {
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700512 zend_throw_exception(spl_ce_InvalidArgumentException,
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700513 "watchConnectivityState expects 1 long 1 timeval",
514 1 TSRMLS_CC);
515 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700516 return;
517 }
518
thinkeroufe037cb2018-05-16 12:51:28 +0800519 wrapped_grpc_timeval *deadline =
520 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, deadline_obj);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700521 grpc_channel_watch_connectivity_state(channel->wrapper->wrapped,
thinkeroua3730b72016-07-20 16:59:54 +0800522 (grpc_connectivity_state)last_state,
523 deadline->wrapped, completion_queue,
524 NULL);
525 grpc_event event =
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700526 grpc_completion_queue_pluck(completion_queue, NULL,
527 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
528 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheung1567c0c2015-08-13 11:12:54 -0700529 RETURN_BOOL(event.success);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700530}
531
532/**
mlumishb892a272014-12-09 16:28:23 -0800533 * Close the channel
thinkerou03dc2192016-08-16 19:31:44 +0800534 * @return void
mlumishb892a272014-12-09 16:28:23 -0800535 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800536PHP_METHOD(Channel, close) {
thinkeroufe037cb2018-05-16 12:51:28 +0800537 wrapped_grpc_channel *channel =
538 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis());
Zhouyihai Ding7c647d32018-01-22 13:11:40 -0800539 if (channel->wrapper != NULL) {
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700540 php_grpc_channel_unref(channel->wrapper);
541 channel->wrapper = NULL;
Stanley Cheung5d559482017-08-22 13:15:01 -0700542 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700543}
544
545// Delete an entry from the persistent list
546// Note: this does not destroy or close the underlying grpc_channel
547void php_grpc_delete_persistent_list_entry(char *key, php_grpc_int key_len
548 TSRMLS_DC) {
549 php_grpc_zend_resource *rsrc;
550 gpr_mu_lock(&global_persistent_list_mu);
ZhouyihaiDing5c770352018-04-09 13:20:31 -0700551 if (PHP_GRPC_PERSISTENT_LIST_FIND(&grpc_persistent_list, key,
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700552 key_len, rsrc)) {
ZhouyihaiDing5c770352018-04-09 13:20:31 -0700553 php_grpc_zend_hash_del(&grpc_persistent_list, key, key_len+1);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700554 }
555 gpr_mu_unlock(&global_persistent_list_mu);
556}
557
558// A destructor associated with each list entry from the persistent list
559static void php_grpc_channel_plink_dtor(php_grpc_zend_resource *rsrc
560 TSRMLS_DC) {
561 channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr;
ZhouyihaiDing5c770352018-04-09 13:20:31 -0700562 if (le == NULL) {
563 return;
564 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700565 if (le->channel != NULL) {
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700566 php_grpc_channel_unref(le->channel);
567 le->channel = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700568 }
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700569 free(le);
570 le = NULL;
mlumishb892a272014-12-09 16:28:23 -0800571}
572
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700573// A destructor associated with each list entry from the target_bound map
574static void php_grpc_target_bound_dtor(php_grpc_zend_resource *rsrc
575 TSRMLS_DC) {
576 target_bound_le_t *le = (target_bound_le_t *) rsrc->ptr;
577 if (le == NULL) {
578 return;
579 }
580 free(le);
581 le = NULL;
582}
583
584#ifdef GRPC_PHP_DEBUG
585
586/**
587* Clean all channels in the persistent. Test only.
588* @return void
589*/
590PHP_METHOD(Channel, cleanPersistentList) {
591 zend_hash_clean(&grpc_persistent_list);
592 zend_hash_clean(&grpc_target_upper_bound_map);
593}
594
595char *grpc_connectivity_state_name(grpc_connectivity_state state) {
596 switch (state) {
597 case GRPC_CHANNEL_IDLE:
598 return "IDLE";
599 case GRPC_CHANNEL_CONNECTING:
600 return "CONNECTING";
601 case GRPC_CHANNEL_READY:
602 return "READY";
603 case GRPC_CHANNEL_TRANSIENT_FAILURE:
604 return "TRANSIENT_FAILURE";
605 case GRPC_CHANNEL_SHUTDOWN:
606 return "SHUTDOWN";
607 }
608 return "UNKNOWN";
609}
610
611/**
612* Return the info about the current channel. Test only.
613* @return array
614*/
615PHP_METHOD(Channel, getChannelInfo) {
thinkeroufe037cb2018-05-16 12:51:28 +0800616 wrapped_grpc_channel *channel =
617 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, getThis());
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700618 array_init(return_value);
619 // Info about the target
620 PHP_GRPC_ADD_STRING_TO_ARRAY(return_value, "target",
621 sizeof("target"), channel->wrapper->target, true);
622 // Info about the upper bound for the target
623 target_bound_le_t* target_bound_status =
624 update_and_get_target_upper_bound(channel->wrapper->target, -1);
625 PHP_GRPC_ADD_LONG_TO_ARRAY(return_value, "target_upper_bound",
626 sizeof("target_upper_bound"), target_bound_status->upper_bound);
627 PHP_GRPC_ADD_LONG_TO_ARRAY(return_value, "target_current_size",
628 sizeof("target_current_size"), target_bound_status->current_count);
629 // Info about key
630 PHP_GRPC_ADD_STRING_TO_ARRAY(return_value, "key",
631 sizeof("key"), channel->wrapper->key, true);
632 // Info about persistent channel ref_count
633 PHP_GRPC_ADD_LONG_TO_ARRAY(return_value, "ref_count",
634 sizeof("ref_count"), channel->wrapper->ref_count);
635 // Info about connectivity status
636 int state =
637 grpc_channel_check_connectivity_state(channel->wrapper->wrapped, (int)0);
638 // It should be set to 'true' in PHP 5.6.33
639 PHP_GRPC_ADD_LONG_TO_ARRAY(return_value, "connectivity_status",
640 sizeof("connectivity_status"), state);
641 PHP_GRPC_ADD_STRING_TO_ARRAY(return_value, "ob",
642 sizeof("ob"),
643 grpc_connectivity_state_name(state), true);
644 // Info about the channel is closed or not
645 PHP_GRPC_ADD_BOOL_TO_ARRAY(return_value, "is_valid",
646 sizeof("is_valid"), (channel->wrapper == NULL));
647}
648
649/**
650* Return an array of all channels in the persistent list. Test only.
651* @return array
652*/
653PHP_METHOD(Channel, getPersistentList) {
654 array_init(return_value);
655 zval *data;
656 PHP_GRPC_HASH_FOREACH_VAL_START(&grpc_persistent_list, data)
657 php_grpc_zend_resource *rsrc =
658 (php_grpc_zend_resource*) PHP_GRPC_HASH_VALPTR_TO_VAL(data)
659 if (rsrc == NULL) {
660 break;
661 }
662 channel_persistent_le_t* le = rsrc->ptr;
663 zval* ret_arr;
664 PHP_GRPC_MAKE_STD_ZVAL(ret_arr);
665 array_init(ret_arr);
666 // Info about the target
667 PHP_GRPC_ADD_STRING_TO_ARRAY(ret_arr, "target",
668 sizeof("target"), le->channel->target, true);
669 // Info about the upper bound for the target
670 target_bound_le_t* target_bound_status =
671 update_and_get_target_upper_bound(le->channel->target, -1);
672 PHP_GRPC_ADD_LONG_TO_ARRAY(ret_arr, "target_upper_bound",
673 sizeof("target_upper_bound"), target_bound_status->upper_bound);
674 PHP_GRPC_ADD_LONG_TO_ARRAY(ret_arr, "target_current_size",
675 sizeof("target_current_size"), target_bound_status->current_count);
676 // Info about key
677 PHP_GRPC_ADD_STRING_TO_ARRAY(ret_arr, "key",
678 sizeof("key"), le->channel->key, true);
679 // Info about persistent channel ref_count
680 PHP_GRPC_ADD_LONG_TO_ARRAY(ret_arr, "ref_count",
681 sizeof("ref_count"), le->channel->ref_count);
682 // Info about connectivity status
683 int state =
684 grpc_channel_check_connectivity_state(le->channel->wrapped, (int)0);
685 // It should be set to 'true' in PHP 5.6.33
686 PHP_GRPC_ADD_LONG_TO_ARRAY(ret_arr, "connectivity_status",
687 sizeof("connectivity_status"), state);
688 PHP_GRPC_ADD_STRING_TO_ARRAY(ret_arr, "ob",
689 sizeof("ob"),
690 grpc_connectivity_state_name(state), true);
691 add_assoc_zval(return_value, le->channel->key, ret_arr);
692 PHP_GRPC_FREE_STD_ZVAL(ret_arr);
693 PHP_GRPC_HASH_FOREACH_END()
694}
695#endif
696
697
Stanley Cheung6a5c83d2017-02-16 12:25:32 -0800698ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 2)
699 ZEND_ARG_INFO(0, target)
700 ZEND_ARG_INFO(0, args)
701ZEND_END_ARG_INFO()
702
703ZEND_BEGIN_ARG_INFO_EX(arginfo_getTarget, 0, 0, 0)
704ZEND_END_ARG_INFO()
705
706ZEND_BEGIN_ARG_INFO_EX(arginfo_getConnectivityState, 0, 0, 0)
707 ZEND_ARG_INFO(0, try_to_connect)
708ZEND_END_ARG_INFO()
709
710ZEND_BEGIN_ARG_INFO_EX(arginfo_watchConnectivityState, 0, 0, 2)
711 ZEND_ARG_INFO(0, last_state)
712 ZEND_ARG_INFO(0, deadline)
713ZEND_END_ARG_INFO()
714
715ZEND_BEGIN_ARG_INFO_EX(arginfo_close, 0, 0, 0)
716ZEND_END_ARG_INFO()
717
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700718#ifdef GRPC_PHP_DEBUG
719ZEND_BEGIN_ARG_INFO_EX(arginfo_getChannelInfo, 0, 0, 0)
720ZEND_END_ARG_INFO()
721
722ZEND_BEGIN_ARG_INFO_EX(arginfo_cleanPersistentList, 0, 0, 0)
723ZEND_END_ARG_INFO()
724
725ZEND_BEGIN_ARG_INFO_EX(arginfo_getPersistentList, 0, 0, 0)
726ZEND_END_ARG_INFO()
727#endif
728
729
mlumishb892a272014-12-09 16:28:23 -0800730static zend_function_entry channel_methods[] = {
Stanley Cheung6a5c83d2017-02-16 12:25:32 -0800731 PHP_ME(Channel, __construct, arginfo_construct,
732 ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
733 PHP_ME(Channel, getTarget, arginfo_getTarget,
734 ZEND_ACC_PUBLIC)
735 PHP_ME(Channel, getConnectivityState, arginfo_getConnectivityState,
736 ZEND_ACC_PUBLIC)
737 PHP_ME(Channel, watchConnectivityState, arginfo_watchConnectivityState,
738 ZEND_ACC_PUBLIC)
739 PHP_ME(Channel, close, arginfo_close,
740 ZEND_ACC_PUBLIC)
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700741 #ifdef GRPC_PHP_DEBUG
742 PHP_ME(Channel, getChannelInfo, arginfo_getChannelInfo,
743 ZEND_ACC_PUBLIC)
744 PHP_ME(Channel, cleanPersistentList, arginfo_cleanPersistentList,
745 ZEND_ACC_PUBLIC)
746 PHP_ME(Channel, getPersistentList, arginfo_getPersistentList,
747 ZEND_ACC_PUBLIC)
748 #endif
thinkeroua3730b72016-07-20 16:59:54 +0800749 PHP_FE_END
750};
mlumishb892a272014-12-09 16:28:23 -0800751
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700752GRPC_STARTUP_FUNCTION(channel) {
mlumishb892a272014-12-09 16:28:23 -0800753 zend_class_entry ce;
754 INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods);
755 ce.create_object = create_wrapped_grpc_channel;
756 grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC);
Stanley Cheung5d559482017-08-22 13:15:01 -0700757 gpr_mu_init(&global_persistent_list_mu);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700758 le_plink = zend_register_list_destructors_ex(
759 NULL, php_grpc_channel_plink_dtor, "Persistent Channel", module_number);
ZhouyihaiDing5c770352018-04-09 13:20:31 -0700760 zend_hash_init_ex(&grpc_persistent_list, 20, NULL,
761 EG(persistent_list).pDestructor, 1, 0);
ZhouyihaiDing9ef881e2018-04-12 16:43:33 -0700762 // Register the target->upper_bound map.
763 le_bound = zend_register_list_destructors_ex(
764 NULL, php_grpc_target_bound_dtor, "Target Bound", module_number);
765 zend_hash_init_ex(&grpc_target_upper_bound_map, 20, NULL,
766 EG(persistent_list).pDestructor, 1, 0);
767
thinkerou5dafd822016-07-28 22:43:38 +0800768 PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel, channel_ce_handlers);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700769 return SUCCESS;
mlumishb892a272014-12-09 16:28:23 -0800770}