aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/grpcio/grpc/_channel.py
blob: d9315d2e6cd4d055c39ab2338c8653c0982abde8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
# Copyright 2016, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Invocation-side implementation of gRPC Python."""

import sys
import threading
import time

import grpc
from grpc import _common
from grpc import _grpcio_metadata
from grpc.framework.foundation import callable_util
from grpc._cython import cygrpc

_USER_AGENT = 'Python-gRPC-{}'.format(_grpcio_metadata.__version__)

_EMPTY_FLAGS = 0
_INFINITE_FUTURE = cygrpc.Timespec(float('+inf'))
_EMPTY_METADATA = cygrpc.Metadata(())

_UNARY_UNARY_INITIAL_DUE = (
    cygrpc.OperationType.send_initial_metadata,
    cygrpc.OperationType.send_message,
    cygrpc.OperationType.send_close_from_client,
    cygrpc.OperationType.receive_initial_metadata,
    cygrpc.OperationType.receive_message,
    cygrpc.OperationType.receive_status_on_client,
)
_UNARY_STREAM_INITIAL_DUE = (
    cygrpc.OperationType.send_initial_metadata,
    cygrpc.OperationType.send_message,
    cygrpc.OperationType.send_close_from_client,
    cygrpc.OperationType.receive_initial_metadata,
    cygrpc.OperationType.receive_status_on_client,
)
_STREAM_UNARY_INITIAL_DUE = (
    cygrpc.OperationType.send_initial_metadata,
    cygrpc.OperationType.receive_initial_metadata,
    cygrpc.OperationType.receive_message,
    cygrpc.OperationType.receive_status_on_client,
)
_STREAM_STREAM_INITIAL_DUE = (
    cygrpc.OperationType.send_initial_metadata,
    cygrpc.OperationType.receive_initial_metadata,
    cygrpc.OperationType.receive_status_on_client,
)

_CHANNEL_SUBSCRIPTION_CALLBACK_ERROR_LOG_MESSAGE = (
    'Exception calling channel subscription callback!')


def _deadline(timeout):
  if timeout is None:
    return None, _INFINITE_FUTURE
  else:
    deadline = time.time() + timeout
    return deadline, cygrpc.Timespec(deadline)


def _unknown_code_details(unknown_cygrpc_code, details):
  return 'Server sent unknown code {} and details "{}"'.format(
      unknown_cygrpc_code, details)


def _wait_once_until(condition, until):
  if until is None:
    condition.wait()
  else:
    remaining = until - time.time()
    if remaining < 0:
      raise grpc.FutureTimeoutError()
    else:
      condition.wait(timeout=remaining)


class _RPCState(object):

  def __init__(self, due, initial_metadata, trailing_metadata, code, details):
    self.condition = threading.Condition()
    # The cygrpc.OperationType objects representing events due from the RPC's
    # completion queue.
    self.due = set(due)
    self.initial_metadata = initial_metadata
    self.response = None
    self.trailing_metadata = trailing_metadata
    self.code = code
    self.details = details
    # The semantics of grpc.Future.cancel and grpc.Future.cancelled are
    # slightly wonky, so they have to be tracked separately from the rest of the
    # result of the RPC. This field tracks whether cancellation was requested
    # prior to termination of the RPC.
    self.cancelled = False
    self.callbacks = []


def _abort(state, code, details):
  if state.code is None:
    state.code = code
    state.details = details
    if state.initial_metadata is None:
      state.initial_metadata = _EMPTY_METADATA
    state.trailing_metadata = _EMPTY_METADATA


def _handle_event(event, state, response_deserializer):
  callbacks = []
  for batch_operation in event.batch_operations:
    operation_type = batch_operation.type
    state.due.remove(operation_type)
    if operation_type == cygrpc.OperationType.receive_initial_metadata:
      state.initial_metadata = batch_operation.received_metadata
    elif operation_type == cygrpc.OperationType.receive_message:
      serialized_response = batch_operation.received_message.bytes()
      if serialized_response is not None:
        response = _common.deserialize(
            serialized_response, response_deserializer)
        if response is None:
          details = 'Exception deserializing response!'
          _abort(state, grpc.StatusCode.INTERNAL, details)
        else:
          state.response = response
    elif operation_type == cygrpc.OperationType.receive_status_on_client:
      state.trailing_metadata = batch_operation.received_metadata
      if state.code is None:
        code = _common.CYGRPC_STATUS_CODE_TO_STATUS_CODE.get(
            batch_operation.received_status_code)
        if code is None:
          state.code = grpc.StatusCode.UNKNOWN
          state.details = _unknown_code_details(
              batch_operation.received_status_code,
              batch_operation.received_status_details)
        else:
          state.code = code
          state.details = batch_operation.received_status_details
      callbacks.extend(state.callbacks)
      state.callbacks = None
  return callbacks


def _event_handler(state, call, response_deserializer):
  def handle_event(event):
    with state.condition:
      callbacks = _handle_event(event, state, response_deserializer)
      state.condition.notify_all()
      done = not state.due
    for callback in callbacks:
      callback()
    return call if done else None
  return handle_event


def _consume_request_iterator(
    request_iterator, state, call, request_serializer):
  event_handler = _event_handler(state, call, None)
  def consume_request_iterator():
    for request in request_iterator:
      serialized_request = _common.serialize(request, request_serializer)
      with state.condition:
        if state.code is None and not state.cancelled:
          if serialized_request is None:
            call.cancel()
            details = 'Exception serializing request!'
            _abort(state, grpc.StatusCode.INTERNAL, details)
            return
          else:
            operations = (
                cygrpc.operation_send_message(
                    serialized_request, _EMPTY_FLAGS),
            )
            call.start_batch(cygrpc.Operations(operations), event_handler)
            state.due.add(cygrpc.OperationType.send_message)
            while True:
              state.condition.wait()
              if state.code is None:
                if cygrpc.OperationType.send_message not in state.due:
                  break
              else:
                return
        else:
          return
    with state.condition:
      if state.code is None:
        operations = (
            cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
        )
        call.start_batch(cygrpc.Operations(operations), event_handler)
        state.due.add(cygrpc.OperationType.send_close_from_client)
  thread = threading.Thread(target=consume_request_iterator)
  thread.start()


class _Rendezvous(grpc.RpcError, grpc.Future, grpc.Call):

  def __init__(self, state, call, response_deserializer, deadline):
    super(_Rendezvous, self).__init__()
    self._state = state
    self._call = call
    self._response_deserializer = response_deserializer
    self._deadline = deadline

  def cancel(self):
    with self._state.condition:
      if self._state.code is None:
        self._call.cancel()
        self._state.cancelled = True
        _abort(self._state, grpc.StatusCode.CANCELLED, 'Cancelled!')
        self._state.condition.notify_all()
      return False

  def cancelled(self):
    with self._state.condition:
      return self._state.cancelled

  def running(self):
    with self._state.condition:
      return self._state.code is None

  def done(self):
    with self._state.condition:
      return self._state.code is not None

  def result(self, timeout=None):
    until = None if timeout is None else time.time() + timeout
    with self._state.condition:
      while True:
        if self._state.code is None:
          _wait_once_until(self._state.condition, until)
        elif self._state.code is grpc.StatusCode.OK:
          return self._state.response
        elif self._state.cancelled:
          raise grpc.FutureCancelledError()
        else:
          raise self

  def exception(self, timeout=None):
    until = None if timeout is None else time.time() + timeout
    with self._state.condition:
      while True:
        if self._state.code is None:
          _wait_once_until(self._state.condition, until)
        elif self._state.code is grpc.StatusCode.OK:
          return None
        elif self._state.cancelled:
          raise grpc.FutureCancelledError()
        else:
          return self

  def traceback(self, timeout=None):
    until = None if timeout is None else time.time() + timeout
    with self._state.condition:
      while True:
        if self._state.code is None:
          _wait_once_until(self._state.condition, until)
        elif self._state.code is grpc.StatusCode.OK:
          return None
        elif self._state.cancelled:
          raise grpc.FutureCancelledError()
        else:
          try:
            raise self
          except grpc.RpcError:
            return sys.exc_info()[2]

  def add_done_callback(self, fn):
    with self._state.condition:
      if self._state.code is None:
        self._state.callbacks.append(lambda: fn(self))
        return

    fn(self)

  def _next(self):
    with self._state.condition:
      if self._state.code is None:
        event_handler = _event_handler(
            self._state, self._call, self._response_deserializer)
        self._call.start_batch(
            cygrpc.Operations(
                (cygrpc.operation_receive_message(_EMPTY_FLAGS),)),
            event_handler)
        self._state.due.add(cygrpc.OperationType.receive_message)
      elif self._state.code is grpc.StatusCode.OK:
        raise StopIteration()
      else:
        raise self
      while True:
        self._state.condition.wait()
        if self._state.response is not None:
          response = self._state.response
          self._state.response = None
          return response
        elif cygrpc.OperationType.receive_message not in self._state.due:
          if self._state.code is grpc.StatusCode.OK:
            raise StopIteration()
          elif self._state.code is not None:
            raise self

  def __iter__(self):
    return self

  def __next__(self):
    return self._next()

  def next(self):
    return self._next()

  def is_active(self):
    with self._state.condition:
      return self._state.code is None

  def time_remaining(self):
    if self._deadline is None:
      return None
    else:
      return max(self._deadline - time.time(), 0)

  def add_cancellation_callback(self, callback):
    with self._state.condition:
      if self._state.callbacks is None:
        return False
      else:
        self._state.callbacks.append(lambda unused_future: callback())
        return True

  def initial_metadata(self):
    with self._state.condition:
      while self._state.initial_metadata is None:
        self._state.condition.wait()
      return self._state.initial_metadata

  def trailing_metadata(self):
    with self._state.condition:
      while self._state.trailing_metadata is None:
        self._state.condition.wait()
      return self._state.trailing_metadata

  def code(self):
    with self._state.condition:
      while self._state.code is None:
        self._state.condition.wait()
      return self._state.code

  def details(self):
    with self._state.condition:
      while self._state.details is None:
        self._state.condition.wait()
      return self._state.details

  def _repr(self):
    with self._state.condition:
      if self._state.code is None:
        return '<_Rendezvous object of in-flight RPC>'
      else:
        return '<_Rendezvous of RPC that terminated with ({}, {})>'.format(
            self._state.code, self._state.details)

  def __repr__(self):
    return self._repr()

  def __str__(self):
    return self._repr()

  def __del__(self):
    with self._state.condition:
      if self._state.code is None:
        self._call.cancel()
        self._state.cancelled = True
        self._state.code = grpc.StatusCode.CANCELLED
        self._state.condition.notify_all()


def _start_unary_request(request, timeout, request_serializer):
  deadline, deadline_timespec = _deadline(timeout)
  serialized_request = _common.serialize(request, request_serializer)
  if serialized_request is None:
    state = _RPCState(
        (), _EMPTY_METADATA, _EMPTY_METADATA, grpc.StatusCode.INTERNAL,
        'Exception serializing request!')
    rendezvous = _Rendezvous(state, None, None, deadline)
    return deadline, deadline_timespec, None, rendezvous
  else:
    return deadline, deadline_timespec, serialized_request, None


def _end_unary_response_blocking(state, with_call, deadline):
  if state.code is grpc.StatusCode.OK:
    if with_call:
      rendezvous = _Rendezvous(state, None, None, deadline)
      return state.response, rendezvous
    else:
      return state.response
  else:
    raise _Rendezvous(state, None, None, deadline)


class _UnaryUnaryMultiCallable(grpc.UnaryUnaryMultiCallable):

  def __init__(
      self, channel, create_managed_call, method, request_serializer,
      response_deserializer):
    self._channel = channel
    self._create_managed_call = create_managed_call
    self._method = method
    self._request_serializer = request_serializer
    self._response_deserializer = response_deserializer

  def _prepare(self, request, timeout, metadata):
    deadline, deadline_timespec, serialized_request, rendezvous = (
        _start_unary_request(request, timeout, self._request_serializer))
    if serialized_request is None:
      return None, None, None, None, rendezvous
    else:
      state = _RPCState(_UNARY_UNARY_INITIAL_DUE, None, None, None, None)
      operations = (
          cygrpc.operation_send_initial_metadata(
              _common.metadata(metadata), _EMPTY_FLAGS),
          cygrpc.operation_send_message(serialized_request, _EMPTY_FLAGS),
          cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
          cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
          cygrpc.operation_receive_message(_EMPTY_FLAGS),
          cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
      )
      return state, operations, deadline, deadline_timespec, None

  def __call__(
      self, request, timeout=None, metadata=None, credentials=None,
      with_call=False):
    state, operations, deadline, deadline_timespec, rendezvous = self._prepare(
        request, timeout, metadata)
    if rendezvous:
      raise rendezvous
    else:
      completion_queue = cygrpc.CompletionQueue()
      call = self._channel.create_call(
          None, 0, completion_queue, self._method, None, deadline_timespec)
      if credentials is not None:
        call.set_credentials(credentials._credentials)
      call.start_batch(cygrpc.Operations(operations), None)
      _handle_event(completion_queue.poll(), state, self._response_deserializer)
      return _end_unary_response_blocking(state, with_call, deadline)

  def future(self, request, timeout=None, metadata=None, credentials=None):
    state, operations, deadline, deadline_timespec, rendezvous = self._prepare(
        request, timeout, metadata)
    if rendezvous:
      return rendezvous
    else:
      call = self._create_managed_call(
          None, 0, self._method, None, deadline_timespec)
      if credentials is not None:
        call.set_credentials(credentials._credentials)
      event_handler = _event_handler(state, call, self._response_deserializer)
      with state.condition:
        call.start_batch(cygrpc.Operations(operations), event_handler)
      return _Rendezvous(state, call, self._response_deserializer, deadline)


class _UnaryStreamMultiCallable(grpc.UnaryStreamMultiCallable):

  def __init__(
      self, channel, create_managed_call, method, request_serializer,
      response_deserializer):
    self._channel = channel
    self._create_managed_call = create_managed_call
    self._method = method
    self._request_serializer = request_serializer
    self._response_deserializer = response_deserializer

  def __call__(self, request, timeout=None, metadata=None, credentials=None):
    deadline, deadline_timespec, serialized_request, rendezvous = (
        _start_unary_request(request, timeout, self._request_serializer))
    if serialized_request is None:
      raise rendezvous
    else:
      state = _RPCState(_UNARY_STREAM_INITIAL_DUE, None, None, None, None)
      call = self._create_managed_call(
          None, 0, self._method, None, deadline_timespec)
      if credentials is not None:
        call.set_credentials(credentials._credentials)
      event_handler = _event_handler(state, call, self._response_deserializer)
      with state.condition:
        call.start_batch(
            cygrpc.Operations(
                (cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),)),
            event_handler)
        operations = (
            cygrpc.operation_send_initial_metadata(
                _common.metadata(metadata), _EMPTY_FLAGS),
            cygrpc.operation_send_message(serialized_request, _EMPTY_FLAGS),
            cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
            cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
        )
        call.start_batch(cygrpc.Operations(operations), event_handler)
      return _Rendezvous(state, call, self._response_deserializer, deadline)


class _StreamUnaryMultiCallable(grpc.StreamUnaryMultiCallable):

  def __init__(
      self, channel, create_managed_call, method, request_serializer,
      response_deserializer):
    self._channel = channel
    self._create_managed_call = create_managed_call
    self._method = method
    self._request_serializer = request_serializer
    self._response_deserializer = response_deserializer

  def __call__(
      self, request_iterator, timeout=None, metadata=None, credentials=None,
      with_call=False):
    deadline, deadline_timespec = _deadline(timeout)
    state = _RPCState(_STREAM_UNARY_INITIAL_DUE, None, None, None, None)
    completion_queue = cygrpc.CompletionQueue()
    call = self._channel.create_call(
        None, 0, completion_queue, self._method, None, deadline_timespec)
    if credentials is not None:
      call.set_credentials(credentials._credentials)
    with state.condition:
      call.start_batch(
          cygrpc.Operations(
              (cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),)),
          None)
      operations = (
          cygrpc.operation_send_initial_metadata(
              _common.metadata(metadata), _EMPTY_FLAGS),
          cygrpc.operation_receive_message(_EMPTY_FLAGS),
          cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
      )
      call.start_batch(cygrpc.Operations(operations), None)
      _consume_request_iterator(
          request_iterator, state, call, self._request_serializer)
    while True:
      event = completion_queue.poll()
      with state.condition:
        _handle_event(event, state, self._response_deserializer)
        state.condition.notify_all()
        if not state.due:
          break
    return _end_unary_response_blocking(state, with_call, deadline)

  def future(
      self, request_iterator, timeout=None, metadata=None, credentials=None):
    deadline, deadline_timespec = _deadline(timeout)
    state = _RPCState(_STREAM_UNARY_INITIAL_DUE, None, None, None, None)
    call = self._create_managed_call(
        None, 0, self._method, None, deadline_timespec)
    if credentials is not None:
      call.set_credentials(credentials._credentials)
    event_handler = _event_handler(state, call, self._response_deserializer)
    with state.condition:
      call.start_batch(
          cygrpc.Operations(
              (cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),)),
          event_handler)
      operations = (
          cygrpc.operation_send_initial_metadata(
              _common.metadata(metadata), _EMPTY_FLAGS),
          cygrpc.operation_receive_message(_EMPTY_FLAGS),
          cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
      )
      call.start_batch(cygrpc.Operations(operations), event_handler)
      _consume_request_iterator(
          request_iterator, state, call, self._request_serializer)
    return _Rendezvous(state, call, self._response_deserializer, deadline)


class _StreamStreamMultiCallable(grpc.StreamStreamMultiCallable):

  def __init__(
      self, channel, create_managed_call, method, request_serializer,
      response_deserializer):
    self._channel = channel
    self._create_managed_call = create_managed_call
    self._method = method
    self._request_serializer = request_serializer
    self._response_deserializer = response_deserializer

  def __call__(
      self, request_iterator, timeout=None, metadata=None, credentials=None):
    deadline, deadline_timespec = _deadline(timeout)
    state = _RPCState(_STREAM_STREAM_INITIAL_DUE, None, None, None, None)
    call = self._create_managed_call(
        None, 0, self._method, None, deadline_timespec)
    if credentials is not None:
      call.set_credentials(credentials._credentials)
    event_handler = _event_handler(state, call, self._response_deserializer)
    with state.condition:
      call.start_batch(
          cygrpc.Operations(
              (cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),)),
          event_handler)
      operations = (
          cygrpc.operation_send_initial_metadata(
              _common.metadata(metadata), _EMPTY_FLAGS),
          cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
      )
      call.start_batch(cygrpc.Operations(operations), event_handler)
      _consume_request_iterator(
          request_iterator, state, call, self._request_serializer)
    return _Rendezvous(state, call, self._response_deserializer, deadline)


class _ChannelCallState(object):

  def __init__(self, channel):
    self.lock = threading.Lock()
    self.channel = channel
    self.completion_queue = cygrpc.CompletionQueue()
    self.managed_calls = None


def _call_spin(state):
  while True:
    event = state.completion_queue.poll()
    completed_call = event.tag(event)
    if completed_call is not None:
      with state.lock:
        state.managed_calls.remove(completed_call)
        if not state.managed_calls:
          state.managed_calls = None
          return


def _create_channel_managed_call(state):
  def create_channel_managed_call(parent, flags, method, host, deadline):
    """Creates a managed cygrpc.Call.

    Callers of this function must conduct at least one operation on the returned
    call. The tags associated with operations conducted on the returned call
    must be no-argument callables that return None to indicate that this channel
    should continue polling for events associated with the call and return the
    call itself to indicate that no more events associated with the call will be
    generated.

    Args:
      parent: A cygrpc.Call to be used as the parent of the created call.
      flags: An integer bitfield of call flags.
      method: The RPC method.
      host: A host string for the created call.
      deadline: A cygrpc.Timespec to be the deadline of the created call.

    Returns:
      A cygrpc.Call with which to conduct an RPC.
    """
    with state.lock:
      call = state.channel.create_call(
          parent, flags, state.completion_queue, method, host, deadline)
      if state.managed_calls is None:
        state.managed_calls = set((call,))
        spin_thread = threading.Thread(target=_call_spin, args=(state,))
        spin_thread.start()
      else:
        state.managed_calls.add(call)
      return call
  return create_channel_managed_call


class _ChannelConnectivityState(object):

  def __init__(self, channel):
    self.lock = threading.Lock()
    self.channel = channel
    self.polling = False
    self.connectivity = None
    self.try_to_connect = False
    self.callbacks_and_connectivities = []
    self.delivering = False


def _deliveries(state):
  callbacks_needing_update = []
  for callback_and_connectivity in state.callbacks_and_connectivities:
    callback, callback_connectivity, = callback_and_connectivity
    if callback_connectivity is not state.connectivity:
      callbacks_needing_update.append(callback)
      callback_and_connectivity[1] = state.connectivity
  return callbacks_needing_update


def _deliver(state, initial_connectivity, initial_callbacks):
  connectivity = initial_connectivity
  callbacks = initial_callbacks
  while True:
    for callback in callbacks:
      callable_util.call_logging_exceptions(
          callback, _CHANNEL_SUBSCRIPTION_CALLBACK_ERROR_LOG_MESSAGE,
          connectivity)
    with state.lock:
      callbacks = _deliveries(state)
      if callbacks:
        connectivity = state.connectivity
      else:
        state.delivering = False
        return


def _spawn_delivery(state, callbacks):
  delivering_thread = threading.Thread(
      target=_deliver, args=(state, state.connectivity, callbacks,))
  delivering_thread.start()
  state.delivering = True


# NOTE(https://github.com/grpc/grpc/issues/3064): We'd rather not poll.
def _poll_connectivity(state, channel, initial_try_to_connect):
  try_to_connect = initial_try_to_connect
  connectivity = channel.check_connectivity_state(try_to_connect)
  with state.lock:
    state.connectivity = (
        _common.CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITY[
            connectivity])
    callbacks = tuple(
        callback for callback, unused_but_known_to_be_none_connectivity
        in state.callbacks_and_connectivities)
    for callback_and_connectivity in state.callbacks_and_connectivities:
      callback_and_connectivity[1] = state.connectivity
    if callbacks:
      _spawn_delivery(state, callbacks)
  completion_queue = cygrpc.CompletionQueue()
  while True:
    channel.watch_connectivity_state(
        connectivity, cygrpc.Timespec(time.time() + 0.2),
        completion_queue, None)
    event = completion_queue.poll()
    with state.lock:
      if not state.callbacks_and_connectivities and not state.try_to_connect:
        state.polling = False
        state.connectivity = None
        break
      try_to_connect = state.try_to_connect
      state.try_to_connect = False
    if event.success or try_to_connect:
      connectivity = channel.check_connectivity_state(try_to_connect)
      with state.lock:
        state.connectivity = (
            _common.CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITY[
                connectivity])
        if not state.delivering:
          callbacks = _deliveries(state)
          if callbacks:
            _spawn_delivery(state, callbacks)


def _subscribe(state, callback, try_to_connect):
  with state.lock:
    if not state.callbacks_and_connectivities and not state.polling:
      polling_thread = threading.Thread(
          target=_poll_connectivity,
          args=(state, state.channel, bool(try_to_connect)))
      polling_thread.start()
      state.polling = True
      state.callbacks_and_connectivities.append([callback, None])
    elif not state.delivering and state.connectivity is not None:
      _spawn_delivery(state, (callback,))
      state.try_to_connect |= bool(try_to_connect)
      state.callbacks_and_connectivities.append(
          [callback, state.connectivity])
    else:
      state.try_to_connect |= bool(try_to_connect)
      state.callbacks_and_connectivities.append([callback, None])


def _unsubscribe(state, callback):
  with state.lock:
    for index, (subscribed_callback, unused_connectivity) in enumerate(
        state.callbacks_and_connectivities):
      if callback == subscribed_callback:
        state.callbacks_and_connectivities.pop(index)
        break


def _moot(state):
  with state.lock:
    del state.callbacks_and_connectivities[:]


def _options(options):
  if options is None:
    pairs = ((cygrpc.ChannelArgKey.primary_user_agent_string, _USER_AGENT),)
  else:
    pairs = list(options) + [
        (cygrpc.ChannelArgKey.primary_user_agent_string, _USER_AGENT)]
  return cygrpc.ChannelArgs(
      cygrpc.ChannelArg(arg_name, arg_value) for arg_name, arg_value in pairs)


class Channel(grpc.Channel):

  def __init__(self, target, options, credentials):
    self._channel = cygrpc.Channel(target, _options(options), credentials)
    self._call_state = _ChannelCallState(self._channel)
    self._connectivity_state = _ChannelConnectivityState(self._channel)

  def subscribe(self, callback, try_to_connect=None):
    _subscribe(self._connectivity_state, callback, try_to_connect)

  def unsubscribe(self, callback):
    _unsubscribe(self._connectivity_state, callback)

  def unary_unary(
      self, method, request_serializer=None, response_deserializer=None):
    return _UnaryUnaryMultiCallable(
        self._channel, _create_channel_managed_call(self._call_state), method,
        request_serializer, response_deserializer)

  def unary_stream(
      self, method, request_serializer=None, response_deserializer=None):
    return _UnaryStreamMultiCallable(
        self._channel, _create_channel_managed_call(self._call_state), method,
        request_serializer, response_deserializer)

  def stream_unary(
      self, method, request_serializer=None, response_deserializer=None):
    return _StreamUnaryMultiCallable(
        self._channel, _create_channel_managed_call(self._call_state), method,
        request_serializer, response_deserializer)

  def stream_stream(
      self, method, request_serializer=None, response_deserializer=None):
    return _StreamStreamMultiCallable(
        self._channel, _create_channel_managed_call(self._call_state), method,
        request_serializer, response_deserializer)

  def __del__(self):
    _moot(self._connectivity_state)