工作中遇到一个问题,上游服务通过grpc调用下游服务,但是由于下游服务负载太高导致上游服务的调用会随机出现超时的情况,但是有一点不太明确:超时之后,下游服务还会继续进行计算么?

于是自己写了一个damon试了一下:

client:

  1. # Copyright 2015 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """The Python implementation of the GRPC helloworld.Greeter client."""
  15.  
  16. from __future__ import print_function
  17. import logging
  18.  
  19. import grpc
  20.  
  21. import helloworld_pb2
  22. import helloworld_pb2_grpc
  23.  
  24. def run():
  25. # NOTE(gRPC Python Team): .close() is possible on a channel and should be
  26. # used in circumstances in which the with statement does not fit the needs
  27. # of the code.
  28. with grpc.insecure_channel('localhost:50051') as channel:
  29. stub = helloworld_pb2_grpc.GreeterStub(channel)
  30. response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), timeout=30)
  31. print("Greeter client received: " + response.message)
  32.  
  33. if __name__ == '__main__':
  34. logging.basicConfig()
  35. run()

server:

  1. # Copyright 2015 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """The Python implementation of the GRPC helloworld.Greeter server."""
  15.  
  16. from concurrent import futures
  17. import time
  18. import logging
  19.  
  20. import grpc
  21.  
  22. import helloworld_pb2
  23. import helloworld_pb2_grpc
  24.  
  25. _ONE_DAY_IN_SECONDS = 60 * 60 * 24
  26.  
  27. class Greeter(helloworld_pb2_grpc.GreeterServicer):
  28.  
  29. def SayHello(self, request, context):
  30. count = 0
  31. while count < 10:
  32. print('time:%s' % (time.time()))
  33. time.sleep(5)
  34. return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
  35.  
  36. def serve():
  37. server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
  38. helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
  39. server.add_insecure_port('[::]:50051')
  40. server.start()
  41. try:
  42. while True:
  43. time.sleep(_ONE_DAY_IN_SECONDS)
  44. except KeyboardInterrupt:
  45. server.stop(0)
  46.  
  47. if __name__ == '__main__':
  48. logging.basicConfig()
  49. serve()

这两个例子就是在grpc官方提供的python例子上做了一下小的改动,得到的结果是:当client超时报错退出之后,server还是会继续进行计算,直到结束,那如果是这样的话,超时的机制对于server来说是没有作用的,即使client已经不再等待这个结果了,但是server还是会继续计算,浪费server的资源。

grpc:超时机制的更多相关文章

  1. 您还有心跳吗?超时机制分析(java)

    注:本人是原作者,首发于并发编程网(您还有心跳吗?超时机制分析),此文结合那里的留言作了一些修改. 问题描述 在C/S模式中,有时我们会长时间保持一个连接,以避免频繁地建立连接,但同时,一般会有一个超 ...

  2. 从报错“无效操作,连接被关闭”探究Transaction的Timeout超时机制

    1.报错如下:Invalid Operation the connection is closed,无效操作,连接被关闭.这个错误是并不是每次都报,只有在复杂操作.大事务的情况下才偶然报出来. sta ...

  3. C# Socket连接请求超时机制

    作者:RazanPaul 译者:Todd Wei 原文:http://www.codeproject.com/KB/IP/TimeOutSocket.aspx 介绍 您可能注意到了,.Net的Syst ...

  4. TCP/IP的三次握手和四次分手以及超时机制

    使用INADDR_ANY的时候,往往针对多网卡情况,采用tcp连接方式,需要选择使用哪一个网卡发送,自己猜想应该是使用三次握手机制,如何判断目标地址不可达,应该使用的是超时机制,即握手超时则不可到达. ...

  5. mysql超时机制

    mysql每次建立一个socket连接(connect)时,这个socket都会占用一定内存.即使你关闭(close)连接时,并不是真正的关闭,而是处于睡眠(sleep)状态. 当你下次再进行连接时, ...

  6. Java并发框架——AQS超时机制

    AQS框架提供的另外一个优秀机制是锁获取超时的支持,当大量线程对某一锁竞争时可能导致某些线程在很长一段时间都获取不了锁,在某些场景下可能希望如果线程在一段时间内不能成功获取锁就取消对该锁的等待以提高性 ...

  7. 从零开始学spring cloud(九) -------- 超时机制,断路器模式介绍

    目前存在的问题: 现在我们假设一下,服务提供者响应非常缓慢,那么消费者对提供者的请求就会被强制等待,直到服务返回.在高负载场景下,如果不做任何处理,这种问题很可能造成所有处理用户请求的线程都被耗竭,而 ...

  8. Go 初体验 - channel.2 - 超时机制

    channel 虽然很好用,但是我们也要考虑异常情况,比如:超时 go 语言怎么解决这个超时问题呢? 可以利用 select 语句: select 的用法与 switch 语言非常类似,由 selec ...

  9. Netty 超时机制及心跳程序实现

    Netty 超时机制的介绍 Netty 的超时类型 IdleState 主要分为: ALL_IDLE : 一段时间内没有数据接收或者发送 READER_IDLE : 一段时间内没有数据接收 WRITE ...

  10. go语言之进阶篇 select实现的超时机制

    1.select实现的超时机制 示例: package main import ( "fmt" "time" ) func main() { ch := mak ...

随机推荐

  1. 【java异常】org.springframework.web.util.NestedServletException: Handler processing failed;Can't connect to X11 window server using 'localhost:10.0' as the value of th

    tomcat工程中创建二维码失败.抛出异常Can't connect to X11 window server using 'localhost:10.0' as the value of th 因为 ...

  2. maker使用说明书

    1.以自带的示例数据为例 dpp_contig.fasta dpp_est.fasta dpp_protein.fasta te_proteins.fasta 2.生成控制文件 控制文件是特定于运行的 ...

  3. ESA2GJK1DH1K升级篇: STM32远程乒乓升级,基于(GPRS模块AT指令TCP透传方式),定时访问升级(兼容Air202,SIM800)

    实现功能概要 单片机定时使用http访问云端的程序版本, 如果版本不一致,然后通过http下载最新的升级文件,实现远程升级STM32. 兼容Air202,SIM800 测试准备工作(默认访问我的服务器 ...

  4. vue中使用过的全局API

    Vue.directive()---------全局自定义指令 Vue.filter()----------全局过滤器 Vue.$set()----------为响应式对象添加一个响应式属性 Vue. ...

  5. 以py脚本形式ORM操作 及 django终端打印sql语句的设置

    1. 在Django项目的settings.py文件中,在最后复制粘贴如下代码: LOGGING = { 'version': 1, 'disable_existing_loggers': False ...

  6. Eclipse对spring-boot,spring-boot-mybatis的搭建

    1.准备工作 1.1.如果没有sts(spring tool suite)插件,  则需要下载. 1.1.1.eclipse下载的话,一定要注意版本,因为eclipse会直接下载最新版本,如果ts版本 ...

  7. Java实现AES加密(window机器和linux机器) 注意window机器 和linux机器算法稍有不同

    一)什么是AES? 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),是一种区块加密标准.这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用. ...

  8. WebUploader 被 FormData 函数坑了,用了他的页面千万别定FormData变量

    WebUploader 被  FormData 函数坑了,用了他的页面千万别定FormData变量 被这个坑了,页面中变量var FormData=null;后又为他赋了值,所有一直上传不了,也没有错 ...

  9. 整合zuul启动时报错Correct the classpath of your application so that it contains a single, compatible version of XXX

    今天集成zuul与consul的时候,出现如下错误 ***************************APPLICATION FAILED TO START******************** ...

  10. 笔记:Map(就是用来Ctrl+C,V的)

    JDK1.8:List -> Map: 1,Map<String, String> maps = userList.stream().collect(Collectors.toMap ...