1616
1717from typing import Callable
1818
19- import grpc # type: ignore
20- from grpc .experimental import aio # type: ignore
21-
2219from google .cloud .bigtable .data ._cross_sync import CrossSync
2320
21+ from grpc import ChannelConnectivity
22+
23+ if CrossSync .is_async :
24+ from grpc .aio import Call
25+ from grpc .aio import Channel
26+ from grpc .aio import UnaryUnaryMultiCallable
27+ from grpc .aio import UnaryStreamMultiCallable
28+ from grpc .aio import StreamUnaryMultiCallable
29+ from grpc .aio import StreamStreamMultiCallable
30+ else :
31+ from grpc import Call
32+ from grpc import Channel
33+ from grpc import UnaryUnaryMultiCallable
34+ from grpc import UnaryStreamMultiCallable
35+ from grpc import StreamUnaryMultiCallable
36+ from grpc import StreamStreamMultiCallable
37+
38+ __CROSS_SYNC_OUTPUT__ = "google.cloud.bigtable.data._sync_autogen._replaceable_channel"
39+
40+ @CrossSync .convert_class
2441class _WrappedMultiCallable :
2542 """
2643 Wrapper class that implements the grpc MultiCallable interface.
2744 Allows generic functions that return calls to pass checks for
2845 MultiCallable objects.
2946 """
3047
31- def __init__ (self , call_factory : Callable [..., aio . Call ]):
48+ def __init__ (self , call_factory : Callable [..., Call ]):
3249 self ._call_factory = call_factory
3350
34- def __call__ (self , * args , ** kwargs ) -> aio . Call :
51+ def __call__ (self , * args , ** kwargs ) -> Call :
3552 return self ._call_factory (* args , ** kwargs )
3653
3754
3855class WrappedUnaryUnaryMultiCallable (
39- _WrappedMultiCallable , aio . UnaryUnaryMultiCallable
56+ _WrappedMultiCallable , UnaryUnaryMultiCallable
4057):
4158 pass
4259
4360
4461class WrappedUnaryStreamMultiCallable (
45- _WrappedMultiCallable , aio . UnaryStreamMultiCallable
62+ _WrappedMultiCallable , UnaryStreamMultiCallable
4663):
4764 pass
4865
4966
5067class WrappedStreamUnaryMultiCallable (
51- _WrappedMultiCallable , aio . StreamUnaryMultiCallable
68+ _WrappedMultiCallable , StreamUnaryMultiCallable
5269):
5370 pass
5471
5572
5673class WrappedStreamStreamMultiCallable (
57- _WrappedMultiCallable , aio . StreamStreamMultiCallable
74+ _WrappedMultiCallable , StreamStreamMultiCallable
5875):
5976 pass
6077
6178
62- class _AsyncWrappedChannel (aio .Channel ):
79+ @CrossSync .convert_class (sync_name = "_WrappedChannel" , rm_aio = True )
80+ class _AsyncWrappedChannel (Channel ):
6381 """
6482 A wrapper around a gRPC channel. All methods are passed
6583 through to the underlying channel.
6684 """
6785
68- def __init__ (self , channel : aio . Channel ):
86+ def __init__ (self , channel : Channel ):
6987 self ._channel = channel
7088
71- def unary_unary (self , * args , ** kwargs ) -> grpc . aio . UnaryUnaryMultiCallable :
89+ def unary_unary (self , * args , ** kwargs ) -> UnaryUnaryMultiCallable :
7290 return WrappedUnaryUnaryMultiCallable (
7391 lambda * call_args , ** call_kwargs : self ._channel .unary_unary (
7492 * args , ** kwargs
7593 )(* call_args , ** call_kwargs )
7694 )
7795
78- def unary_stream (self , * args , ** kwargs ) -> grpc . aio . UnaryStreamMultiCallable :
96+ def unary_stream (self , * args , ** kwargs ) -> UnaryStreamMultiCallable :
7997 return WrappedUnaryStreamMultiCallable (
8098 lambda * call_args , ** call_kwargs : self ._channel .unary_stream (
8199 * args , ** kwargs
82100 )(* call_args , ** call_kwargs )
83101 )
84102
85- def stream_unary (self , * args , ** kwargs ) -> grpc . aio . StreamUnaryMultiCallable :
103+ def stream_unary (self , * args , ** kwargs ) -> StreamUnaryMultiCallable :
86104 return WrappedStreamUnaryMultiCallable (
87105 lambda * call_args , ** call_kwargs : self ._channel .stream_unary (
88106 * args , ** kwargs
89107 )(* call_args , ** call_kwargs )
90108 )
91109
92- def stream_stream (self , * args , ** kwargs ) -> grpc . aio . StreamStreamMultiCallable :
110+ def stream_stream (self , * args , ** kwargs ) -> StreamStreamMultiCallable :
93111 return WrappedStreamStreamMultiCallable (
94112 lambda * call_args , ** call_kwargs : self ._channel .stream_stream (
95113 * args , ** kwargs
@@ -102,14 +120,16 @@ async def close(self, grace=None):
102120 async def channel_ready (self ):
103121 return await self ._channel .channel_ready ()
104122
123+ @CrossSync .convert (sync_name = "__enter__" , replace_symbols = {"__aenter__" : "__enter__" })
105124 async def __aenter__ (self ):
106125 await self ._channel .__aenter__ ()
107126 return self
108127
128+ @CrossSync .convert (sync_name = "__exit__" , replace_symbols = {"__aexit__" : "__exit__" })
109129 async def __aexit__ (self , exc_type , exc_val , exc_tb ):
110130 return await self ._channel .__aexit__ (exc_type , exc_val , exc_tb )
111131
112- def get_state (self , try_to_connect : bool = False ) -> grpc . ChannelConnectivity :
132+ def get_state (self , try_to_connect : bool = False ) -> ChannelConnectivity :
113133 return self ._channel .get_state (try_to_connect = try_to_connect )
114134
115135 async def wait_for_state_change (self , last_observed_state ):
@@ -118,32 +138,26 @@ async def wait_for_state_change(self, last_observed_state):
118138 def __getattr__ (self , name ):
119139 return getattr (self ._channel , name )
120140
121-
141+ @ CrossSync . convert_class ( sync_name = "_ReplaceableChannel" , replace_symbols = { "_AsyncWrappedChannel" : "_WrappedChannel" })
122142class _AsyncReplaceableChannel (_AsyncWrappedChannel ):
123143
124- def __init__ (self , channel_fn : Callable [[], aio . Channel ]):
144+ def __init__ (self , channel_fn : Callable [[], Channel ]):
125145 self ._channel_fn = channel_fn
126146 self ._channel = channel_fn ()
127147
128- def create_channel (self ) -> aio .Channel :
129- return self ._channel_fn ()
130-
131- async def replace_wrapped_channel (self , new_channel : aio .Channel , grace_period : float | None , copy_async_interceptors : bool = True ) -> aio .Channel :
132- old_channel = self ._channel
133- if CrossSync .is_async and copy_async_interceptors :
148+ def create_channel (self ) -> Channel :
149+ new_channel = self ._channel_fn ()
150+ if CrossSync .is_async :
134151 # copy over interceptors
135152 # this is needed because of how gapic attaches the LoggingClientAIOInterceptor
136153 # sync channels add interceptors by wrapping, so this step isn't needed
137- new_channel ._unary_unary_interceptors = old_channel ._unary_unary_interceptors
138- new_channel ._unary_stream_interceptors = old_channel ._unary_stream_interceptors
139- new_channel ._stream_unary_interceptors = old_channel ._stream_unary_interceptors
140- new_channel ._stream_stream_interceptors = old_channel ._stream_stream_interceptors
154+ new_channel ._unary_unary_interceptors = self ._channel ._unary_unary_interceptors
155+ new_channel ._unary_stream_interceptors = self ._channel ._unary_stream_interceptors
156+ new_channel ._stream_unary_interceptors = self ._channel ._stream_unary_interceptors
157+ new_channel ._stream_stream_interceptors = self ._channel ._stream_stream_interceptors
158+ return new_channel
159+
160+ def replace_wrapped_channel (self , new_channel : Channel ) -> Channel :
161+ old_channel = self ._channel
141162 self ._channel = new_channel
142- # give old_channel a chance to complete existing rpcs
143- if CrossSync .is_async :
144- await old_channel .close (grace_period )
145- else :
146- if grace_period :
147- self ._is_closed .wait (grace_period ) # type: ignore
148- old_channel .close () # type: ignore
149163 return old_channel
0 commit comments