nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Nicolas "Pixel" Noble | d51d121 | 2016-01-31 11:33:19 +0100 | [diff] [blame] | 19 | #include <ruby/ruby.h> |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 20 | #include <ruby/thread.h> |
Nicolas "Pixel" Noble | 9fcdc87 | 2016-05-05 06:15:34 +0200 | [diff] [blame] | 21 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 22 | #include "rb_byte_buffer.h" |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 23 | #include "rb_channel.h" |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 24 | #include "rb_grpc_imports.generated.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 25 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 26 | #include <grpc/grpc.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 27 | #include <grpc/grpc_security.h> |
Tim Emiola | 623a74d | 2015-08-11 09:24:20 -0700 | [diff] [blame] | 28 | #include <grpc/support/alloc.h> |
murgatroid99 | 9acc40f | 2016-06-30 13:54:09 -0700 | [diff] [blame] | 29 | #include <grpc/support/log.h> |
murgatroid99 | 91633cb | 2016-07-19 10:36:01 -0700 | [diff] [blame] | 30 | #include <grpc/support/time.h> |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 31 | #include "rb_call.h" |
| 32 | #include "rb_channel_args.h" |
Tim Emiola | 9332ea6 | 2015-10-27 23:48:29 -0700 | [diff] [blame] | 33 | #include "rb_channel_credentials.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 34 | #include "rb_completion_queue.h" |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 35 | #include "rb_grpc.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 36 | #include "rb_server.h" |
| 37 | |
| 38 | /* id_channel is the name of the hidden ivar that preserves a reference to the |
| 39 | * channel on a call, so that calls are not GCed before their channel. */ |
| 40 | static ID id_channel; |
| 41 | |
| 42 | /* id_target is the name of the hidden ivar that preserves a reference to the |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 43 | * target string used to create the call, preserved so that it does not get |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 44 | * GCed before the channel */ |
| 45 | static ID id_target; |
| 46 | |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 47 | /* id_insecure_channel is used to indicate that a channel is insecure */ |
| 48 | static VALUE id_insecure_channel; |
| 49 | |
Yuki Yugui Sonoda | 3c88e5d | 2015-04-16 20:09:00 +0900 | [diff] [blame] | 50 | /* grpc_rb_cChannel is the ruby class that proxies grpc_channel. */ |
| 51 | static VALUE grpc_rb_cChannel = Qnil; |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 52 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 53 | /* Used during the conversion of a hash to channel args during channel setup */ |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 54 | static VALUE grpc_rb_cChannelArgs; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 55 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 56 | typedef struct bg_watched_channel { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 57 | grpc_channel* channel; |
Alexander Polcyn | 032f398 | 2017-05-17 13:22:55 -0700 | [diff] [blame] | 58 | // these fields must only be accessed under global_connection_polling_mu |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 59 | struct bg_watched_channel* next; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 60 | int channel_destroyed; |
Alexander Polcyn | 032f398 | 2017-05-17 13:22:55 -0700 | [diff] [blame] | 61 | int refcount; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 62 | } bg_watched_channel; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 63 | |
murgatroid99 | a00d7c0 | 2016-03-14 15:51:56 -0700 | [diff] [blame] | 64 | /* grpc_rb_channel wraps a grpc_channel. */ |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 65 | typedef struct grpc_rb_channel { |
murgatroid99 | a00d7c0 | 2016-03-14 15:51:56 -0700 | [diff] [blame] | 66 | VALUE credentials; |
| 67 | |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 68 | /* The actual channel (protected in a wrapper to tell when it's safe to |
| 69 | * destroy) */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 70 | bg_watched_channel* bg_wrapped; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 71 | } grpc_rb_channel; |
| 72 | |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 73 | typedef enum { CONTINUOUS_WATCH, WATCH_STATE_API } watch_state_op_type; |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 74 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 75 | typedef struct watch_state_op { |
| 76 | watch_state_op_type op_type; |
| 77 | // from event.success |
| 78 | union { |
| 79 | struct { |
| 80 | int success; |
| 81 | // has been called back due to a cq next call |
| 82 | int called_back; |
| 83 | } api_callback_args; |
| 84 | struct { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 85 | bg_watched_channel* bg; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 86 | } continuous_watch_callback_args; |
| 87 | } op; |
| 88 | } watch_state_op; |
| 89 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 90 | static bg_watched_channel* bg_watched_channel_list_head = NULL; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 91 | |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 92 | static void grpc_rb_channel_try_register_connection_polling( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 93 | bg_watched_channel* bg); |
| 94 | static void* wait_until_channel_polling_thread_started_no_gil(void*); |
| 95 | static void wait_until_channel_polling_thread_started_unblocking_func(void*); |
| 96 | static void* channel_init_try_register_connection_polling_without_gil( |
| 97 | void* arg); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 98 | |
| 99 | typedef struct channel_init_try_register_stack { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 100 | grpc_channel* channel; |
| 101 | grpc_rb_channel* wrapper; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 102 | } channel_init_try_register_stack; |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 103 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 104 | static grpc_completion_queue* channel_polling_cq; |
Alexander Polcyn | be30114 | 2017-03-14 16:33:44 -0700 | [diff] [blame] | 105 | static gpr_mu global_connection_polling_mu; |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 106 | static gpr_cv global_connection_polling_cv; |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 107 | static int abort_channel_polling = 0; |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 108 | static int channel_polling_thread_started = 0; |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 109 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 110 | static int bg_watched_channel_list_lookup(bg_watched_channel* bg); |
| 111 | static bg_watched_channel* bg_watched_channel_list_create_and_add( |
| 112 | grpc_channel* channel); |
| 113 | static void bg_watched_channel_list_free_and_remove(bg_watched_channel* bg); |
| 114 | static void run_poll_channels_loop_unblocking_func(void* arg); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 115 | |
| 116 | // Needs to be called under global_connection_polling_mu |
Alexander Polcyn | 032f398 | 2017-05-17 13:22:55 -0700 | [diff] [blame] | 117 | static void grpc_rb_channel_watch_connection_state_op_complete( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 118 | watch_state_op* op, int success) { |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 119 | GPR_ASSERT(!op->op.api_callback_args.called_back); |
| 120 | op->op.api_callback_args.called_back = 1; |
| 121 | op->op.api_callback_args.success = success; |
| 122 | // wake up the watch API call thats waiting on this op |
| 123 | gpr_cv_broadcast(&global_connection_polling_cv); |
| 124 | } |
| 125 | |
| 126 | /* Avoids destroying a channel twice. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 127 | static void grpc_rb_channel_safe_destroy(bg_watched_channel* bg) { |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 128 | gpr_mu_lock(&global_connection_polling_mu); |
| 129 | GPR_ASSERT(bg_watched_channel_list_lookup(bg)); |
| 130 | if (!bg->channel_destroyed) { |
| 131 | grpc_channel_destroy(bg->channel); |
| 132 | bg->channel_destroyed = 1; |
| 133 | } |
| 134 | bg->refcount--; |
| 135 | if (bg->refcount == 0) { |
| 136 | bg_watched_channel_list_free_and_remove(bg); |
| 137 | } |
| 138 | gpr_mu_unlock(&global_connection_polling_mu); |
| 139 | } |
| 140 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 141 | static void* channel_safe_destroy_without_gil(void* arg) { |
| 142 | grpc_rb_channel_safe_destroy((bg_watched_channel*)arg); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 143 | return NULL; |
| 144 | } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 145 | |
| 146 | /* Destroys Channel instances. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 147 | static void grpc_rb_channel_free(void* p) { |
| 148 | grpc_rb_channel* ch = NULL; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 149 | if (p == NULL) { |
| 150 | return; |
| 151 | }; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 152 | ch = (grpc_rb_channel*)p; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 153 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 154 | if (ch->bg_wrapped != NULL) { |
| 155 | /* assumption made here: it's ok to directly gpr_mu_lock the global |
| 156 | * connection polling mutex becuse we're in a finalizer, |
Alexander Polcyn | 032f398 | 2017-05-17 13:22:55 -0700 | [diff] [blame] | 157 | * and we can count on this thread to not be interrupted or |
| 158 | * yield the gil. */ |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 159 | grpc_rb_channel_safe_destroy(ch->bg_wrapped); |
| 160 | ch->bg_wrapped = NULL; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | xfree(p); |
| 164 | } |
| 165 | |
| 166 | /* Protects the mark object from GC */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 167 | static void grpc_rb_channel_mark(void* p) { |
| 168 | grpc_rb_channel* channel = NULL; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 169 | if (p == NULL) { |
| 170 | return; |
| 171 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 172 | channel = (grpc_rb_channel*)p; |
murgatroid99 | a00d7c0 | 2016-03-14 15:51:56 -0700 | [diff] [blame] | 173 | if (channel->credentials != Qnil) { |
| 174 | rb_gc_mark(channel->credentials); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 178 | static rb_data_type_t grpc_channel_data_type = {"grpc_channel", |
| 179 | {grpc_rb_channel_mark, |
| 180 | grpc_rb_channel_free, |
| 181 | GRPC_RB_MEMSIZE_UNAVAILABLE, |
| 182 | {NULL, NULL}}, |
| 183 | NULL, |
| 184 | NULL, |
Tim Emiola | 9161a82 | 2015-11-11 15:58:44 -0800 | [diff] [blame] | 185 | #ifdef RUBY_TYPED_FREE_IMMEDIATELY |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 186 | RUBY_TYPED_FREE_IMMEDIATELY |
Tim Emiola | 9161a82 | 2015-11-11 15:58:44 -0800 | [diff] [blame] | 187 | #endif |
Yuki Yugui Sonoda | c9b7d1c | 2015-04-11 14:50:09 +0900 | [diff] [blame] | 188 | }; |
| 189 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 190 | /* Allocates grpc_rb_channel instances. */ |
| 191 | static VALUE grpc_rb_channel_alloc(VALUE cls) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 192 | grpc_rb_channel* wrapper = ALLOC(grpc_rb_channel); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 193 | wrapper->bg_wrapped = NULL; |
murgatroid99 | a00d7c0 | 2016-03-14 15:51:56 -0700 | [diff] [blame] | 194 | wrapper->credentials = Qnil; |
Yuki Yugui Sonoda | c9b7d1c | 2015-04-11 14:50:09 +0900 | [diff] [blame] | 195 | return TypedData_Wrap_Struct(cls, &grpc_channel_data_type, wrapper); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 196 | } |
| 197 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 198 | /* |
| 199 | call-seq: |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 200 | insecure_channel = Channel:new("myhost:8080", {'arg1': 'value1'}, |
| 201 | :this_channel_is_insecure) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 202 | creds = ... |
| 203 | secure_channel = Channel:new("myhost:443", {'arg1': 'value1'}, creds) |
| 204 | |
| 205 | Creates channel instances. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 206 | static VALUE grpc_rb_channel_init(int argc, VALUE* argv, VALUE self) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 207 | VALUE channel_args = Qnil; |
| 208 | VALUE credentials = Qnil; |
| 209 | VALUE target = Qnil; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 210 | grpc_rb_channel* wrapper = NULL; |
| 211 | grpc_channel* ch = NULL; |
| 212 | grpc_channel_credentials* creds = NULL; |
| 213 | char* target_chars = NULL; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 214 | grpc_channel_args args; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 215 | channel_init_try_register_stack stack; |
Alexander Polcyn | 82fef0b | 2017-05-17 23:42:38 -0700 | [diff] [blame] | 216 | int stop_waiting_for_thread_start = 0; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 217 | MEMZERO(&args, grpc_channel_args, 1); |
| 218 | |
Alexander Polcyn | 2a9b5d7 | 2017-04-14 12:10:55 -0700 | [diff] [blame] | 219 | grpc_ruby_once_init(); |
murgatroid99 | ce67bff | 2017-04-19 15:54:27 -0700 | [diff] [blame] | 220 | rb_thread_call_without_gvl( |
Alexander Polcyn | 82fef0b | 2017-05-17 23:42:38 -0700 | [diff] [blame] | 221 | wait_until_channel_polling_thread_started_no_gil, |
| 222 | &stop_waiting_for_thread_start, |
| 223 | wait_until_channel_polling_thread_started_unblocking_func, |
| 224 | &stop_waiting_for_thread_start); |
Alexander Polcyn | 2a9b5d7 | 2017-04-14 12:10:55 -0700 | [diff] [blame] | 225 | |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 226 | /* "3" == 3 mandatory args */ |
| 227 | rb_scan_args(argc, argv, "3", &target, &channel_args, &credentials); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 228 | |
Yuki Yugui Sonoda | c9b7d1c | 2015-04-11 14:50:09 +0900 | [diff] [blame] | 229 | TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 230 | target_chars = StringValueCStr(target); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 231 | grpc_rb_hash_convert_to_channel_args(channel_args, &args); |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 232 | if (TYPE(credentials) == T_SYMBOL) { |
| 233 | if (id_insecure_channel != SYM2ID(credentials)) { |
| 234 | rb_raise(rb_eTypeError, |
| 235 | "bad creds symbol, want :this_channel_is_insecure"); |
| 236 | return Qnil; |
| 237 | } |
Nicolas "Pixel" Noble | 9d72b14 | 2015-08-08 01:45:38 +0200 | [diff] [blame] | 238 | ch = grpc_insecure_channel_create(target_chars, &args, NULL); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 239 | } else { |
murgatroid99 | a00d7c0 | 2016-03-14 15:51:56 -0700 | [diff] [blame] | 240 | wrapper->credentials = credentials; |
Tim Emiola | 9332ea6 | 2015-10-27 23:48:29 -0700 | [diff] [blame] | 241 | creds = grpc_rb_get_wrapped_channel_credentials(credentials); |
Julien Boeuf | c5b570f | 2015-08-25 17:47:55 -0700 | [diff] [blame] | 242 | ch = grpc_secure_channel_create(creds, target_chars, &args, NULL); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 243 | } |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 244 | |
| 245 | GPR_ASSERT(ch); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 246 | stack.channel = ch; |
| 247 | stack.wrapper = wrapper; |
| 248 | rb_thread_call_without_gvl( |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 249 | channel_init_try_register_connection_polling_without_gil, &stack, NULL, |
| 250 | NULL); |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 251 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 252 | if (args.args != NULL) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 253 | xfree(args.args); /* Allocated by grpc_rb_hash_convert_to_channel_args */ |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 254 | } |
| 255 | if (ch == NULL) { |
| 256 | rb_raise(rb_eRuntimeError, "could not create an rpc channel to target:%s", |
| 257 | target_chars); |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 258 | return Qnil; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 259 | } |
| 260 | rb_ivar_set(self, id_target, target); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 261 | return self; |
| 262 | } |
| 263 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 264 | typedef struct get_state_stack { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 265 | bg_watched_channel* bg; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 266 | int try_to_connect; |
| 267 | int out; |
| 268 | } get_state_stack; |
| 269 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 270 | static void* get_state_without_gil(void* arg) { |
| 271 | get_state_stack* stack = (get_state_stack*)arg; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 272 | |
| 273 | gpr_mu_lock(&global_connection_polling_mu); |
| 274 | GPR_ASSERT(abort_channel_polling || channel_polling_thread_started); |
Alexander Polcyn | d7455ab | 2017-05-17 21:39:09 -0700 | [diff] [blame] | 275 | if (stack->bg->channel_destroyed) { |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 276 | stack->out = GRPC_CHANNEL_SHUTDOWN; |
| 277 | } else { |
Alexander Polcyn | d7455ab | 2017-05-17 21:39:09 -0700 | [diff] [blame] | 278 | stack->out = grpc_channel_check_connectivity_state(stack->bg->channel, |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 279 | stack->try_to_connect); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 280 | } |
| 281 | gpr_mu_unlock(&global_connection_polling_mu); |
| 282 | |
| 283 | return NULL; |
| 284 | } |
| 285 | |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 286 | /* |
| 287 | call-seq: |
Alexander Polcyn | e57cd90 | 2017-03-02 16:13:46 -0800 | [diff] [blame] | 288 | ch.connectivity_state -> state |
| 289 | ch.connectivity_state(true) -> state |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 290 | |
Alexander Polcyn | e57cd90 | 2017-03-02 16:13:46 -0800 | [diff] [blame] | 291 | Indicates the current state of the channel, whose value is one of the |
| 292 | constants defined in GRPC::Core::ConnectivityStates. |
| 293 | |
| 294 | It also tries to connect if the chennel is idle in the second form. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 295 | static VALUE grpc_rb_channel_get_connectivity_state(int argc, VALUE* argv, |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 296 | VALUE self) { |
Alexander Polcyn | e57cd90 | 2017-03-02 16:13:46 -0800 | [diff] [blame] | 297 | VALUE try_to_connect_param = Qfalse; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 298 | grpc_rb_channel* wrapper = NULL; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 299 | get_state_stack stack; |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 300 | |
| 301 | /* "01" == 0 mandatory args, 1 (try_to_connect) is optional */ |
Alexander Polcyn | e57cd90 | 2017-03-02 16:13:46 -0800 | [diff] [blame] | 302 | rb_scan_args(argc, argv, "01", &try_to_connect_param); |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 303 | |
| 304 | TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 305 | if (wrapper->bg_wrapped == NULL) { |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 306 | rb_raise(rb_eRuntimeError, "closed!"); |
| 307 | return Qnil; |
| 308 | } |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 309 | |
Alexander Polcyn | d7455ab | 2017-05-17 21:39:09 -0700 | [diff] [blame] | 310 | stack.bg = wrapper->bg_wrapped; |
Alexander Polcyn | 032f398 | 2017-05-17 13:22:55 -0700 | [diff] [blame] | 311 | stack.try_to_connect = RTEST(try_to_connect_param) ? 1 : 0; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 312 | rb_thread_call_without_gvl(get_state_without_gil, &stack, NULL, NULL); |
| 313 | |
| 314 | return LONG2NUM(stack.out); |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Alexander Polcyn | f3147b3 | 2017-03-15 11:34:08 -0700 | [diff] [blame] | 317 | typedef struct watch_state_stack { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 318 | grpc_channel* channel; |
Alexander Polcyn | f3147b3 | 2017-03-15 11:34:08 -0700 | [diff] [blame] | 319 | gpr_timespec deadline; |
Alexander Polcyn | 563ec53 | 2017-03-15 15:54:49 -0700 | [diff] [blame] | 320 | int last_state; |
Alexander Polcyn | f3147b3 | 2017-03-15 11:34:08 -0700 | [diff] [blame] | 321 | } watch_state_stack; |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 322 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 323 | static void* wait_for_watch_state_op_complete_without_gvl(void* arg) { |
| 324 | watch_state_stack* stack = (watch_state_stack*)arg; |
| 325 | watch_state_op* op = NULL; |
| 326 | void* success = (void*)0; |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 327 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 328 | gpr_mu_lock(&global_connection_polling_mu); |
| 329 | // its unsafe to do a "watch" after "channel polling abort" because the cq has |
| 330 | // been shut down. |
| 331 | if (abort_channel_polling) { |
| 332 | gpr_mu_unlock(&global_connection_polling_mu); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 333 | return (void*)0; |
Alexander Polcyn | 5b88146 | 2017-03-21 18:31:29 -0700 | [diff] [blame] | 334 | } |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 335 | op = gpr_zalloc(sizeof(watch_state_op)); |
| 336 | op->op_type = WATCH_STATE_API; |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 337 | grpc_channel_watch_connectivity_state(stack->channel, stack->last_state, |
| 338 | stack->deadline, channel_polling_cq, |
| 339 | op); |
Alexander Polcyn | 5b88146 | 2017-03-21 18:31:29 -0700 | [diff] [blame] | 340 | |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 341 | while (!op->op.api_callback_args.called_back) { |
| 342 | gpr_cv_wait(&global_connection_polling_cv, &global_connection_polling_mu, |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 343 | gpr_inf_future(GPR_CLOCK_REALTIME)); |
Alexander Polcyn | 563ec53 | 2017-03-15 15:54:49 -0700 | [diff] [blame] | 344 | } |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 345 | if (op->op.api_callback_args.success) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 346 | success = (void*)1; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 347 | } |
| 348 | gpr_free(op); |
| 349 | gpr_mu_unlock(&global_connection_polling_mu); |
Alexander Polcyn | 5b88146 | 2017-03-21 18:31:29 -0700 | [diff] [blame] | 350 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 351 | return success; |
Alexander Polcyn | f3147b3 | 2017-03-15 11:34:08 -0700 | [diff] [blame] | 352 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 353 | static void wait_for_watch_state_op_complete_unblocking_func(void* arg) { |
| 354 | bg_watched_channel* bg = (bg_watched_channel*)arg; |
Alexander Polcyn | c24d53b | 2017-05-17 20:25:38 -0700 | [diff] [blame] | 355 | gpr_mu_lock(&global_connection_polling_mu); |
| 356 | if (!bg->channel_destroyed) { |
| 357 | grpc_channel_destroy(bg->channel); |
| 358 | bg->channel_destroyed = 1; |
| 359 | } |
| 360 | gpr_mu_unlock(&global_connection_polling_mu); |
Alexander Polcyn | f3147b3 | 2017-03-15 11:34:08 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Alexander Polcyn | fcad579 | 2017-03-14 12:26:17 -0700 | [diff] [blame] | 363 | /* Wait until the channel's connectivity state becomes different from |
| 364 | * "last_state", or "deadline" expires. |
| 365 | * Returns true if the the channel's connectivity state becomes |
| 366 | * different from "last_state" within "deadline". |
| 367 | * Returns false if "deadline" expires before the channel's connectivity |
| 368 | * state changes from "last_state". |
| 369 | * */ |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 370 | static VALUE grpc_rb_channel_watch_connectivity_state(VALUE self, |
| 371 | VALUE last_state, |
murgatroid99 | 91633cb | 2016-07-19 10:36:01 -0700 | [diff] [blame] | 372 | VALUE deadline) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 373 | grpc_rb_channel* wrapper = NULL; |
Alexander Polcyn | f3147b3 | 2017-03-15 11:34:08 -0700 | [diff] [blame] | 374 | watch_state_stack stack; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 375 | void* op_success = 0; |
murgatroid99 | 91633cb | 2016-07-19 10:36:01 -0700 | [diff] [blame] | 376 | |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 377 | TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper); |
Alexander Polcyn | fcad579 | 2017-03-14 12:26:17 -0700 | [diff] [blame] | 378 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 379 | if (wrapper->bg_wrapped == NULL) { |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 380 | rb_raise(rb_eRuntimeError, "closed!"); |
| 381 | return Qnil; |
| 382 | } |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 383 | |
Alexander Polcyn | fcad579 | 2017-03-14 12:26:17 -0700 | [diff] [blame] | 384 | if (!FIXNUM_P(last_state)) { |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 385 | rb_raise( |
| 386 | rb_eTypeError, |
| 387 | "bad type for last_state. want a GRPC::Core::ChannelState constant"); |
Alexander Polcyn | fcad579 | 2017-03-14 12:26:17 -0700 | [diff] [blame] | 388 | return Qnil; |
murgatroid99 | 91633cb | 2016-07-19 10:36:01 -0700 | [diff] [blame] | 389 | } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 390 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 391 | stack.channel = wrapper->bg_wrapped->channel; |
| 392 | stack.deadline = grpc_rb_time_timeval(deadline, 0), |
Alexander Polcyn | 563ec53 | 2017-03-15 15:54:49 -0700 | [diff] [blame] | 393 | stack.last_state = NUM2LONG(last_state); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 394 | |
| 395 | op_success = rb_thread_call_without_gvl( |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 396 | wait_for_watch_state_op_complete_without_gvl, &stack, |
Alexander Polcyn | c24d53b | 2017-05-17 20:25:38 -0700 | [diff] [blame] | 397 | wait_for_watch_state_op_complete_unblocking_func, wrapper->bg_wrapped); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 398 | |
| 399 | return op_success ? Qtrue : Qfalse; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* Create a call given a grpc_channel, in order to call method. The request |
| 403 | is not sent until grpc_call_invoke is called. */ |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 404 | static VALUE grpc_rb_channel_create_call(VALUE self, VALUE parent, VALUE mask, |
| 405 | VALUE method, VALUE host, |
| 406 | VALUE deadline) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 407 | VALUE res = Qnil; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 408 | grpc_rb_channel* wrapper = NULL; |
| 409 | grpc_call* call = NULL; |
| 410 | grpc_call* parent_call = NULL; |
| 411 | grpc_completion_queue* cq = NULL; |
Tim Emiola | fde3dbf | 2015-08-11 16:35:41 -0700 | [diff] [blame] | 412 | int flags = GRPC_PROPAGATE_DEFAULTS; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 413 | grpc_slice method_slice; |
| 414 | grpc_slice host_slice; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 415 | grpc_slice* host_slice_ptr = NULL; |
| 416 | char* tmp_str = NULL; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 417 | |
Tim Emiola | d42c1b7 | 2015-08-11 10:50:21 -0700 | [diff] [blame] | 418 | if (host != Qnil) { |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 419 | host_slice = |
| 420 | grpc_slice_from_copied_buffer(RSTRING_PTR(host), RSTRING_LEN(host)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 421 | host_slice_ptr = &host_slice; |
Tim Emiola | d42c1b7 | 2015-08-11 10:50:21 -0700 | [diff] [blame] | 422 | } |
Tim Emiola | fde3dbf | 2015-08-11 16:35:41 -0700 | [diff] [blame] | 423 | if (mask != Qnil) { |
| 424 | flags = NUM2UINT(mask); |
| 425 | } |
| 426 | if (parent != Qnil) { |
| 427 | parent_call = grpc_rb_get_wrapped_call(parent); |
| 428 | } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 429 | |
Sree Kuchibhotla | 8d8bb7a | 2017-03-22 12:37:29 -0700 | [diff] [blame] | 430 | cq = grpc_completion_queue_create_for_pluck(NULL); |
Yuki Yugui Sonoda | c9b7d1c | 2015-04-11 14:50:09 +0900 | [diff] [blame] | 431 | TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 432 | if (wrapper->bg_wrapped == NULL) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 433 | rb_raise(rb_eRuntimeError, "closed!"); |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 434 | return Qnil; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 437 | method_slice = |
| 438 | grpc_slice_from_copied_buffer(RSTRING_PTR(method), RSTRING_LEN(method)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 439 | |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 440 | call = grpc_channel_create_call(wrapper->bg_wrapped->channel, parent_call, |
| 441 | flags, cq, method_slice, host_slice_ptr, |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 442 | grpc_rb_time_timeval(deadline, |
| 443 | /* absolute time */ 0), |
| 444 | NULL); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 445 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 446 | if (call == NULL) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 447 | tmp_str = grpc_slice_to_c_string(method_slice); |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 448 | rb_raise(rb_eRuntimeError, "cannot create call with method %s", tmp_str); |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 449 | return Qnil; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 450 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 451 | |
| 452 | grpc_slice_unref(method_slice); |
| 453 | if (host_slice_ptr != NULL) { |
| 454 | grpc_slice_unref(host_slice); |
| 455 | } |
| 456 | |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 457 | res = grpc_rb_wrap_call(call, cq); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 458 | |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 459 | /* Make this channel an instance attribute of the call so that it is not GCed |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 460 | * before the call. */ |
| 461 | rb_ivar_set(res, id_channel, self); |
| 462 | return res; |
| 463 | } |
| 464 | |
| 465 | /* Closes the channel, calling it's destroy method */ |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 466 | /* Note this is an API-level call; a wrapped channel's finalizer doesn't call |
| 467 | * this */ |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 468 | static VALUE grpc_rb_channel_destroy(VALUE self) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 469 | grpc_rb_channel* wrapper = NULL; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 470 | |
Yuki Yugui Sonoda | c9b7d1c | 2015-04-11 14:50:09 +0900 | [diff] [blame] | 471 | TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 472 | if (wrapper->bg_wrapped != NULL) { |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 473 | rb_thread_call_without_gvl(channel_safe_destroy_without_gil, |
| 474 | wrapper->bg_wrapped, NULL, NULL); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 475 | wrapper->bg_wrapped = NULL; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | return Qnil; |
| 479 | } |
| 480 | |
Tim Emiola | 623a74d | 2015-08-11 09:24:20 -0700 | [diff] [blame] | 481 | /* Called to obtain the target that this channel accesses. */ |
| 482 | static VALUE grpc_rb_channel_get_target(VALUE self) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 483 | grpc_rb_channel* wrapper = NULL; |
Tim Emiola | 623a74d | 2015-08-11 09:24:20 -0700 | [diff] [blame] | 484 | VALUE res = Qnil; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 485 | char* target = NULL; |
Tim Emiola | 623a74d | 2015-08-11 09:24:20 -0700 | [diff] [blame] | 486 | |
| 487 | TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 488 | target = grpc_channel_get_target(wrapper->bg_wrapped->channel); |
Tim Emiola | 623a74d | 2015-08-11 09:24:20 -0700 | [diff] [blame] | 489 | res = rb_str_new2(target); |
| 490 | gpr_free(target); |
| 491 | |
| 492 | return res; |
| 493 | } |
| 494 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 495 | /* Needs to be called under global_connection_polling_mu */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 496 | static int bg_watched_channel_list_lookup(bg_watched_channel* target) { |
| 497 | bg_watched_channel* cur = bg_watched_channel_list_head; |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 498 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 499 | while (cur != NULL) { |
| 500 | if (cur == target) { |
| 501 | return 1; |
| 502 | } |
| 503 | cur = cur->next; |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 504 | } |
Alexander Polcyn | fcad579 | 2017-03-14 12:26:17 -0700 | [diff] [blame] | 505 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 506 | return 0; |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 509 | /* Needs to be called under global_connection_polling_mu */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 510 | static bg_watched_channel* bg_watched_channel_list_create_and_add( |
| 511 | grpc_channel* channel) { |
| 512 | bg_watched_channel* watched = gpr_zalloc(sizeof(bg_watched_channel)); |
Alexander Polcyn | 5b88146 | 2017-03-21 18:31:29 -0700 | [diff] [blame] | 513 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 514 | watched->channel = channel; |
| 515 | watched->next = bg_watched_channel_list_head; |
| 516 | watched->refcount = 1; |
| 517 | bg_watched_channel_list_head = watched; |
| 518 | return watched; |
| 519 | } |
| 520 | |
| 521 | /* Needs to be called under global_connection_polling_mu */ |
Alexander Polcyn | 032f398 | 2017-05-17 13:22:55 -0700 | [diff] [blame] | 522 | static void bg_watched_channel_list_free_and_remove( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 523 | bg_watched_channel* target) { |
| 524 | bg_watched_channel* bg = NULL; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 525 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 526 | GPR_ASSERT(bg_watched_channel_list_lookup(target)); |
| 527 | GPR_ASSERT(target->channel_destroyed && target->refcount == 0); |
| 528 | if (bg_watched_channel_list_head == target) { |
| 529 | bg_watched_channel_list_head = target->next; |
| 530 | gpr_free(target); |
| 531 | return; |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 532 | } |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 533 | bg = bg_watched_channel_list_head; |
| 534 | while (bg != NULL && bg->next != NULL) { |
| 535 | if (bg->next == target) { |
| 536 | bg->next = bg->next->next; |
| 537 | gpr_free(target); |
| 538 | return; |
| 539 | } |
| 540 | bg = bg->next; |
| 541 | } |
| 542 | GPR_ASSERT(0); |
| 543 | } |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 544 | |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 545 | /* Initialize a grpc_rb_channel's "protected grpc_channel" and try to push |
| 546 | * it onto the background thread for constant watches. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 547 | static void* channel_init_try_register_connection_polling_without_gil( |
| 548 | void* arg) { |
| 549 | channel_init_try_register_stack* stack = |
| 550 | (channel_init_try_register_stack*)arg; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 551 | |
| 552 | gpr_mu_lock(&global_connection_polling_mu); |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 553 | stack->wrapper->bg_wrapped = |
| 554 | bg_watched_channel_list_create_and_add(stack->channel); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 555 | grpc_rb_channel_try_register_connection_polling(stack->wrapper->bg_wrapped); |
| 556 | gpr_mu_unlock(&global_connection_polling_mu); |
| 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | // Needs to be called under global_connection_poolling_mu |
Alexander Polcyn | 032f398 | 2017-05-17 13:22:55 -0700 | [diff] [blame] | 561 | static void grpc_rb_channel_try_register_connection_polling( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 562 | bg_watched_channel* bg) { |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 563 | grpc_connectivity_state conn_state; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 564 | watch_state_op* op = NULL; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 565 | |
| 566 | GPR_ASSERT(channel_polling_thread_started || abort_channel_polling); |
| 567 | |
| 568 | if (bg->refcount == 0) { |
| 569 | GPR_ASSERT(bg->channel_destroyed); |
| 570 | bg_watched_channel_list_free_and_remove(bg); |
| 571 | return; |
| 572 | } |
| 573 | GPR_ASSERT(bg->refcount == 1); |
Alexander Polcyn | c24d53b | 2017-05-17 20:25:38 -0700 | [diff] [blame] | 574 | if (bg->channel_destroyed || abort_channel_polling) { |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 575 | return; |
| 576 | } |
| 577 | |
| 578 | conn_state = grpc_channel_check_connectivity_state(bg->channel, 0); |
| 579 | if (conn_state == GRPC_CHANNEL_SHUTDOWN) { |
| 580 | return; |
| 581 | } |
| 582 | GPR_ASSERT(bg_watched_channel_list_lookup(bg)); |
| 583 | // prevent bg from being free'd by GC while background thread is watching it |
| 584 | bg->refcount++; |
| 585 | |
| 586 | op = gpr_zalloc(sizeof(watch_state_op)); |
| 587 | op->op_type = CONTINUOUS_WATCH; |
| 588 | op->op.continuous_watch_callback_args.bg = bg; |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 589 | grpc_channel_watch_connectivity_state(bg->channel, conn_state, |
| 590 | gpr_inf_future(GPR_CLOCK_REALTIME), |
| 591 | channel_polling_cq, op); |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Alexander Polcyn | 6971908 | 2017-03-13 12:00:42 -0700 | [diff] [blame] | 594 | // Note this loop breaks out with a single call of |
Alexander Polcyn | deeed7f | 2017-04-16 13:31:58 -0700 | [diff] [blame] | 595 | // "run_poll_channels_loop_no_gil". |
Alexander Polcyn | 6971908 | 2017-03-13 12:00:42 -0700 | [diff] [blame] | 596 | // This assumes that a ruby call the unblocking func |
| 597 | // indicates process shutdown. |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 598 | // In the worst case, this stops polling channel connectivity |
| 599 | // early and falls back to current behavior. |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 600 | static void* run_poll_channels_loop_no_gil(void* arg) { |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 601 | grpc_event event; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 602 | watch_state_op* op = NULL; |
| 603 | bg_watched_channel* bg = NULL; |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 604 | (void)arg; |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 605 | gpr_log(GPR_DEBUG, "GRPC_RUBY: run_poll_channels_loop_no_gil - begin"); |
| 606 | |
| 607 | gpr_mu_lock(&global_connection_polling_mu); |
| 608 | GPR_ASSERT(!channel_polling_thread_started); |
| 609 | channel_polling_thread_started = 1; |
Alexander Polcyn | deeed7f | 2017-04-16 13:31:58 -0700 | [diff] [blame] | 610 | gpr_cv_broadcast(&global_connection_polling_cv); |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 611 | gpr_mu_unlock(&global_connection_polling_mu); |
| 612 | |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 613 | for (;;) { |
| 614 | event = grpc_completion_queue_next( |
| 615 | channel_polling_cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 616 | if (event.type == GRPC_QUEUE_SHUTDOWN) { |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 617 | break; |
| 618 | } |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 619 | gpr_mu_lock(&global_connection_polling_mu); |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 620 | if (event.type == GRPC_OP_COMPLETE) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 621 | op = (watch_state_op*)event.tag; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 622 | if (op->op_type == CONTINUOUS_WATCH) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 623 | bg = (bg_watched_channel*)op->op.continuous_watch_callback_args.bg; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 624 | bg->refcount--; |
| 625 | grpc_rb_channel_try_register_connection_polling(bg); |
| 626 | gpr_free(op); |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 627 | } else if (op->op_type == WATCH_STATE_API) { |
| 628 | grpc_rb_channel_watch_connection_state_op_complete( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 629 | (watch_state_op*)event.tag, event.success); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 630 | } else { |
| 631 | GPR_ASSERT(0); |
| 632 | } |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 633 | } |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 634 | gpr_mu_unlock(&global_connection_polling_mu); |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 635 | } |
| 636 | grpc_completion_queue_destroy(channel_polling_cq); |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 637 | gpr_log(GPR_DEBUG, |
| 638 | "GRPC_RUBY: run_poll_channels_loop_no_gil - exit connection polling " |
| 639 | "loop"); |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 640 | return NULL; |
| 641 | } |
| 642 | |
| 643 | // Notify the channel polling loop to cleanup and shutdown. |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 644 | static void run_poll_channels_loop_unblocking_func(void* arg) { |
| 645 | bg_watched_channel* bg = NULL; |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 646 | (void)arg; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 647 | |
Alexander Polcyn | be30114 | 2017-03-14 16:33:44 -0700 | [diff] [blame] | 648 | gpr_mu_lock(&global_connection_polling_mu); |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 649 | gpr_log(GPR_DEBUG, |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 650 | "GRPC_RUBY: run_poll_channels_loop_unblocking_func - begin aborting " |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 651 | "connection polling"); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 652 | // early out after first time through |
| 653 | if (abort_channel_polling) { |
| 654 | gpr_mu_unlock(&global_connection_polling_mu); |
| 655 | return; |
| 656 | } |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 657 | abort_channel_polling = 1; |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 658 | |
| 659 | // force pending watches to end by switching to shutdown state |
| 660 | bg = bg_watched_channel_list_head; |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 661 | while (bg != NULL) { |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 662 | if (!bg->channel_destroyed) { |
| 663 | grpc_channel_destroy(bg->channel); |
| 664 | bg->channel_destroyed = 1; |
| 665 | } |
| 666 | bg = bg->next; |
| 667 | } |
| 668 | |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 669 | grpc_completion_queue_shutdown(channel_polling_cq); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 670 | gpr_cv_broadcast(&global_connection_polling_cv); |
Alexander Polcyn | be30114 | 2017-03-14 16:33:44 -0700 | [diff] [blame] | 671 | gpr_mu_unlock(&global_connection_polling_mu); |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 672 | gpr_log(GPR_DEBUG, |
| 673 | "GRPC_RUBY: run_poll_channels_loop_unblocking_func - end aborting " |
| 674 | "connection polling"); |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // Poll channel connectivity states in background thread without the GIL. |
| 678 | static VALUE run_poll_channels_loop(VALUE arg) { |
| 679 | (void)arg; |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 680 | gpr_log( |
| 681 | GPR_DEBUG, |
| 682 | "GRPC_RUBY: run_poll_channels_loop - create connection polling thread"); |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 683 | rb_thread_call_without_gvl(run_poll_channels_loop_no_gil, NULL, |
Alexander Polcyn | deeed7f | 2017-04-16 13:31:58 -0700 | [diff] [blame] | 684 | run_poll_channels_loop_unblocking_func, NULL); |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 685 | |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 686 | return Qnil; |
| 687 | } |
| 688 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 689 | static void* wait_until_channel_polling_thread_started_no_gil(void* arg) { |
| 690 | int* stop_waiting = (int*)arg; |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 691 | gpr_log(GPR_DEBUG, "GRPC_RUBY: wait for channel polling thread to start"); |
| 692 | gpr_mu_lock(&global_connection_polling_mu); |
Alexander Polcyn | 82fef0b | 2017-05-17 23:42:38 -0700 | [diff] [blame] | 693 | while (!channel_polling_thread_started && !abort_channel_polling && |
| 694 | !*stop_waiting) { |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 695 | gpr_cv_wait(&global_connection_polling_cv, &global_connection_polling_mu, |
| 696 | gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 697 | } |
| 698 | gpr_mu_unlock(&global_connection_polling_mu); |
| 699 | |
| 700 | return NULL; |
| 701 | } |
| 702 | |
murgatroid99 | ce67bff | 2017-04-19 15:54:27 -0700 | [diff] [blame] | 703 | static void wait_until_channel_polling_thread_started_unblocking_func( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 704 | void* arg) { |
| 705 | int* stop_waiting = (int*)arg; |
Alexander Polcyn | deeed7f | 2017-04-16 13:31:58 -0700 | [diff] [blame] | 706 | gpr_mu_lock(&global_connection_polling_mu); |
murgatroid99 | ce67bff | 2017-04-19 15:54:27 -0700 | [diff] [blame] | 707 | gpr_log(GPR_DEBUG, |
Alexander Polcyn | 82fef0b | 2017-05-17 23:42:38 -0700 | [diff] [blame] | 708 | "GRPC_RUBY: interrupt wait for channel polling thread to start"); |
| 709 | *stop_waiting = 1; |
| 710 | gpr_cv_broadcast(&global_connection_polling_cv); |
| 711 | gpr_mu_unlock(&global_connection_polling_mu); |
| 712 | } |
| 713 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 714 | static void* set_abort_channel_polling_without_gil(void* arg) { |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 715 | (void)arg; |
| 716 | gpr_mu_lock(&global_connection_polling_mu); |
Alexander Polcyn | deeed7f | 2017-04-16 13:31:58 -0700 | [diff] [blame] | 717 | abort_channel_polling = 1; |
| 718 | gpr_cv_broadcast(&global_connection_polling_cv); |
| 719 | gpr_mu_unlock(&global_connection_polling_mu); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 720 | return NULL; |
Alexander Polcyn | deeed7f | 2017-04-16 13:31:58 -0700 | [diff] [blame] | 721 | } |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 722 | |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 723 | /* Temporary fix for |
| 724 | * https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues/899. |
| 725 | * Transports in idle channels can get destroyed. Normally c-core re-connects, |
| 726 | * but in grpc-ruby core never gets a thread until an RPC is made, because ruby |
| 727 | * only calls c-core's "completion_queu_pluck" API. |
| 728 | * This uses a global background thread that calls |
| 729 | * "completion_queue_next" on registered "watch_channel_connectivity_state" |
| 730 | * calls - so that c-core can reconnect if needed, when there aren't any RPC's. |
| 731 | * TODO(apolcyn) remove this when core handles new RPCs on dead connections. |
| 732 | */ |
Alexander Polcyn | 2a9b5d7 | 2017-04-14 12:10:55 -0700 | [diff] [blame] | 733 | void grpc_rb_channel_polling_thread_start() { |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 734 | VALUE background_thread = Qnil; |
| 735 | |
Alexander Polcyn | 2a9b5d7 | 2017-04-14 12:10:55 -0700 | [diff] [blame] | 736 | GPR_ASSERT(!abort_channel_polling); |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 737 | GPR_ASSERT(!channel_polling_thread_started); |
Alexander Polcyn | 2a9b5d7 | 2017-04-14 12:10:55 -0700 | [diff] [blame] | 738 | GPR_ASSERT(channel_polling_cq == NULL); |
| 739 | |
Alexander Polcyn | be30114 | 2017-03-14 16:33:44 -0700 | [diff] [blame] | 740 | gpr_mu_init(&global_connection_polling_mu); |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 741 | gpr_cv_init(&global_connection_polling_cv); |
| 742 | |
murgatroid99 | ce67bff | 2017-04-19 15:54:27 -0700 | [diff] [blame] | 743 | channel_polling_cq = grpc_completion_queue_create_for_next(NULL); |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 744 | background_thread = rb_thread_create(run_poll_channels_loop, NULL); |
| 745 | |
| 746 | if (!RTEST(background_thread)) { |
| 747 | gpr_log(GPR_DEBUG, "GRPC_RUBY: failed to spawn channel polling thread"); |
Alexander Polcyn | 7e44480 | 2017-05-17 13:13:55 -0700 | [diff] [blame] | 748 | rb_thread_call_without_gvl(set_abort_channel_polling_without_gil, NULL, |
| 749 | NULL, NULL); |
Alexander Polcyn | c7fcebe | 2017-04-15 16:37:44 -0700 | [diff] [blame] | 750 | } |
Alexander Polcyn | 9f49866 | 2017-03-02 19:20:07 -0800 | [diff] [blame] | 751 | } |
| 752 | |
Tim Emiola | be88526 | 2015-08-11 17:43:48 -0700 | [diff] [blame] | 753 | static void Init_grpc_propagate_masks() { |
| 754 | /* Constants representing call propagation masks in grpc.h */ |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 755 | VALUE grpc_rb_mPropagateMasks = |
| 756 | rb_define_module_under(grpc_rb_mGrpcCore, "PropagateMasks"); |
Tim Emiola | be88526 | 2015-08-11 17:43:48 -0700 | [diff] [blame] | 757 | rb_define_const(grpc_rb_mPropagateMasks, "DEADLINE", |
| 758 | UINT2NUM(GRPC_PROPAGATE_DEADLINE)); |
| 759 | rb_define_const(grpc_rb_mPropagateMasks, "CENSUS_STATS_CONTEXT", |
| 760 | UINT2NUM(GRPC_PROPAGATE_CENSUS_STATS_CONTEXT)); |
| 761 | rb_define_const(grpc_rb_mPropagateMasks, "CENSUS_TRACING_CONTEXT", |
| 762 | UINT2NUM(GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT)); |
| 763 | rb_define_const(grpc_rb_mPropagateMasks, "CANCELLATION", |
| 764 | UINT2NUM(GRPC_PROPAGATE_CANCELLATION)); |
| 765 | rb_define_const(grpc_rb_mPropagateMasks, "DEFAULTS", |
| 766 | UINT2NUM(GRPC_PROPAGATE_DEFAULTS)); |
| 767 | } |
| 768 | |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 769 | static void Init_grpc_connectivity_states() { |
| 770 | /* Constants representing call propagation masks in grpc.h */ |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 771 | VALUE grpc_rb_mConnectivityStates = |
| 772 | rb_define_module_under(grpc_rb_mGrpcCore, "ConnectivityStates"); |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 773 | rb_define_const(grpc_rb_mConnectivityStates, "IDLE", |
| 774 | LONG2NUM(GRPC_CHANNEL_IDLE)); |
| 775 | rb_define_const(grpc_rb_mConnectivityStates, "CONNECTING", |
| 776 | LONG2NUM(GRPC_CHANNEL_CONNECTING)); |
| 777 | rb_define_const(grpc_rb_mConnectivityStates, "READY", |
| 778 | LONG2NUM(GRPC_CHANNEL_READY)); |
| 779 | rb_define_const(grpc_rb_mConnectivityStates, "TRANSIENT_FAILURE", |
| 780 | LONG2NUM(GRPC_CHANNEL_TRANSIENT_FAILURE)); |
| 781 | rb_define_const(grpc_rb_mConnectivityStates, "FATAL_FAILURE", |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 782 | LONG2NUM(GRPC_CHANNEL_SHUTDOWN)); |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame] | 785 | void Init_grpc_channel() { |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 786 | grpc_rb_cChannelArgs = rb_define_class("TmpChannelArgs", rb_cObject); |
| 787 | grpc_rb_cChannel = |
| 788 | rb_define_class_under(grpc_rb_mGrpcCore, "Channel", rb_cObject); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 789 | |
| 790 | /* Allocates an object managed by the ruby runtime */ |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 791 | rb_define_alloc_func(grpc_rb_cChannel, grpc_rb_channel_alloc); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 792 | |
| 793 | /* Provides a ruby constructor and support for dup/clone. */ |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 794 | rb_define_method(grpc_rb_cChannel, "initialize", grpc_rb_channel_init, -1); |
| 795 | rb_define_method(grpc_rb_cChannel, "initialize_copy", |
murgatroid99 | cc0f4e1 | 2016-06-02 16:00:54 -0700 | [diff] [blame] | 796 | grpc_rb_cannot_init_copy, 1); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 797 | |
| 798 | /* Add ruby analogues of the Channel methods. */ |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 799 | rb_define_method(grpc_rb_cChannel, "connectivity_state", |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 800 | grpc_rb_channel_get_connectivity_state, -1); |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 801 | rb_define_method(grpc_rb_cChannel, "watch_connectivity_state", |
Alexander Polcyn | fcad579 | 2017-03-14 12:26:17 -0700 | [diff] [blame] | 802 | grpc_rb_channel_watch_connectivity_state, 2); |
Sree Kuchibhotla | 98ab520 | 2017-03-03 18:28:47 -0800 | [diff] [blame] | 803 | rb_define_method(grpc_rb_cChannel, "create_call", grpc_rb_channel_create_call, |
| 804 | 5); |
Tim Emiola | 623a74d | 2015-08-11 09:24:20 -0700 | [diff] [blame] | 805 | rb_define_method(grpc_rb_cChannel, "target", grpc_rb_channel_get_target, 0); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 806 | rb_define_method(grpc_rb_cChannel, "destroy", grpc_rb_channel_destroy, 0); |
| 807 | rb_define_alias(grpc_rb_cChannel, "close", "destroy"); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 808 | |
| 809 | id_channel = rb_intern("__channel"); |
| 810 | id_target = rb_intern("__target"); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 811 | rb_define_const(grpc_rb_cChannel, "SSL_TARGET", |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 812 | ID2SYM(rb_intern(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG))); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 813 | rb_define_const(grpc_rb_cChannel, "ENABLE_CENSUS", |
temiola | 71bb137 | 2014-12-11 11:27:25 -0800 | [diff] [blame] | 814 | ID2SYM(rb_intern(GRPC_ARG_ENABLE_CENSUS))); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 815 | rb_define_const(grpc_rb_cChannel, "MAX_CONCURRENT_STREAMS", |
temiola | 71bb137 | 2014-12-11 11:27:25 -0800 | [diff] [blame] | 816 | ID2SYM(rb_intern(GRPC_ARG_MAX_CONCURRENT_STREAMS))); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 817 | rb_define_const(grpc_rb_cChannel, "MAX_MESSAGE_LENGTH", |
Mark D. Roth | 7331a7a | 2016-08-31 13:09:27 -0700 | [diff] [blame] | 818 | ID2SYM(rb_intern(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH))); |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 819 | id_insecure_channel = rb_intern("this_channel_is_insecure"); |
Tim Emiola | be88526 | 2015-08-11 17:43:48 -0700 | [diff] [blame] | 820 | Init_grpc_propagate_masks(); |
Tim Emiola | 046094d | 2015-08-12 16:55:57 -0700 | [diff] [blame] | 821 | Init_grpc_connectivity_states(); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* Gets the wrapped channel from the ruby wrapper */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 825 | grpc_channel* grpc_rb_get_wrapped_channel(VALUE v) { |
| 826 | grpc_rb_channel* wrapper = NULL; |
Yuki Yugui Sonoda | c9b7d1c | 2015-04-11 14:50:09 +0900 | [diff] [blame] | 827 | TypedData_Get_Struct(v, grpc_rb_channel, &grpc_channel_data_type, wrapper); |
Alexander Polcyn | b2c0b7b | 2017-04-27 00:26:25 -0700 | [diff] [blame] | 828 | return wrapper->bg_wrapped->channel; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 829 | } |