เซิร์ฟเวอร์ gRPC API ผ่าน Python ไม่สามารถทำงานได้

Aug 25 2020

ฉันได้พัฒนาเซิร์ฟเวอร์ gRPC API ผ่าน Python จากนั้นสร้างลงในคอนเทนเนอร์และปรับใช้บน Cloud Run

เซิร์ฟเวอร์ gRPC ของฉันกำลังรวมเข้ากับแอปพลิเคชัน TensorFlow จริงๆแล้วเทนเซอร์โฟลว์สำหรับ ML รูปภาพ แต่นี่เป็นโค้ดตัวอย่างสำหรับการพูดคุย

class Calculator(calculator_pb2_grpc.CalculatorServiecer):
    def Calculate(self, request, context):
        try: 
            # Processing
            return #protobuf message    
        except Exception as e:
            logging.error(e)     

def main():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    calculator_pb2_grpc.add_CalculatorServiecer_to_server(Calculator(), server)
    server.add_insecure_port('[::]:8080')
    server.start()
    logging.info("Listening on %s.", '8080')
    server.wait_for_termination()

if __name__ == '__main__':
    main() 

แต่ฉันมักจะได้รับข้อผิดพลาดเมื่อเรียก gRPC เช่น

gRPC status code 14

ERROR:root:<_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Socket closed"

ERROR:root:<_InactiveRpcError of RPC that terminated with:
    status = StatusCode.UNAVAILABLE
    details = "Connection reset by peer"

ใครมีความคิด? สาเหตุเมื่อฉันทดสอบบน localhost ทุกอย่างทำงานได้แม้ในคอนเทนเนอร์นักเทียบท่าก็ยังทำงานได้ แต่เมื่อทุกอย่างบน Cloud Run พังทลาย

ไม่แน่ใจว่าอะไรคือสาเหตุของปัญหานี้ มันอาจจะคงอยู่หรือหมดเวลา?

ฉันลองหลาย ๆ อย่างที่ช่องการเชื่อมต่อฉันเปลี่ยนชื่อโฮสต์เป็น

 {HOST}:443

กรุณาช่วยขอบคุณ

ตัวอย่างรหัสลูกค้า

ฉันสร้างแอปพลิเคชันไคลเอนต์เป็นเซิร์ฟเวอร์ REST API สำหรับการเรียก gRPC ภายนอกเซิร์ฟเวอร์ REST API นี้ปรับใช้บน Cloud Run ด้วย

CHANNEL = grpc.insecure_channel('<CLOUD_RUN_DOMAIN_WITHOUT_HTTP>:443')
STUB = calculator_pb2_grpc.CalculatorStub(CHANNEL)

@app.get("/add") # passing value via query string
def index_dest(first : float, second : float):
    try:
        res = STUB.Calculate(calculator_pb2.BinaryOperation(first_operand=first,second_operand=second,operation="ADD")) 
        return {"message" : res}
    except Exception as e:
        logging.error(e)

คำตอบ

DustinIngram Aug 25 2020 at 11:04

จาก https://cloud.google.com/run/docs/issues#grpc_websocket:

ขณะนี้ Cloud Run (มีการจัดการโดยสมบูรณ์) ไม่รองรับการสตรีม HTTP ดังนั้นจึงไม่รองรับคำขอขาเข้าที่มีโปรโตคอลเช่น WebSockets และสตรีมมิ่ง gRPC