gRPCは、環境変数 http_proxy、https_proxyがセットされていると "Connect Failed" のエラーになる


python で呼ぶ時、クライアント側で

    channel = grpc.insecure_channel('localhost:50051')
    stub = hoge_pb2_grpc.AVRGatewayStub(channel)
    response = stub.Reply(hoge_pb2.ReplyRequest(message='hoge'))

みたいに呼ぶと、

  File "grpc_client.py", line 10, in run
    response = stub.Reply(hoge_pb2.ReplyRequest(message='hoge'))
  File "/Users/xxxxxx/anaconda/lib/python2.7/site-packages/grpc/_channel.py", line 562, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/Users/xxxxxx/anaconda/lib/python2.7/site-packages/grpc/_channel.py", line 466, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
    status = StatusCode.UNAVAILABLE
    details = "Connect Failed"
    debug_error_string = "{"created":"@1555458254.922345000","description":"Failed to create subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":2715,"referenced_errors":[{"created":"@1555458254.922342000","description":"failed to connect to all addresses","file":"src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_line":456,"referenced_errors":[{"created":"@1555458254.922322000","description":"Connect Failed","file":"src/core/ext/filters/client_channel/subchannel.cc","file_line":963,"grpc_status":14,"referenced_errors":[{"created":"@1555458254.922221000","description":"HTTP proxy returned response code 403","file":"src/core/ext/filters/client_channel/http_connect_handshaker.cc","file_line":220}]}]}]}"
>

みたいなエラーになる。これは、http_proxyとhttps_proxyが設定されている時にlocalhostに接続しに行くとこうなるみたいで、

% unset http_proxy
% unset https_proxy

としてから実行するとエラーにならない。

python の中で対応するには、

import os

してから、

os.environ.pop('http_proxy', None)
os.environ.pop('https_proxy', None)

とすると良い。

gRPCのバージョンは、

$ pip list | grep grpc
grpc                               0.3.post19
grpcio                             1.20.0
grpcio-tools                       1.20.0

gRPCのサンプルコードは、

を使ってます。

# ググると結構出てくるので、知っている人は知っている、という感じかな?正確には、「localhostに接続する時はエラーになる」という事なので、本当に分散環境でやる時はちゃんと考えないといけない。