Google Cloud Run和golang goroutines [英] Google Cloud Run and golang goroutines

查看:62
本文介绍了Google Cloud Run和golang goroutines的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑对Google Cloud Run执行一些我需要执行的类似cron的操作.它们将由HTTP调用触发.调用将返回(可能使用202),并继续通过golang goroutine在后台运行.

I'm considering Google Cloud Run for some cron-like operations I need to perform. They will get triggered by an HTTP invocation. The invocation will return (likely with a 202) and continue running in the background via a golang goroutine.

但是,我担心Google Cloud Run容器在不处理HTTP请求时会被破坏.我可能会在处理过程中半途而废.

But, I'm concerned that Google Cloud Run containers are destroyed when they're not handling HTTP requests. I could be part-way through my processing and get reaped.

有没有办法告诉GCR保持容器存活直到完成?

Is there a way to tell GCR to keep the container alive until I'm finished?

推荐答案

Cloud Run在不处理任何请求时会将您的CPU缩减至几乎为零,因为您只在处理请求时才付款.(在此处中记录.)

Cloud Run will scale your CPU down to nearly zero when it's not handling any requests, because you’re only paying when a request is being processed. (It's documented here).

因此,在后台启动goroutine的应用程序不适用于Cloud Run .如果这样做,您的goroutines很可能会饿死CPU时间分配,并且您的程序可能开始表现得很奇怪(因为它将在非常慢的CPU上运行,如果有的话).

Therefore, applications starting goroutines in the background are not suitable for Cloud Run. If you do this, your goroutines will most likely starve for CPU time shares and your program may start behaving very weirdly (as it would be running on a very very slow CPU, if anything at all).

不活动的Cloud Run应用程序获得的微不足道的数量可能仅对垃圾收集有利,而运行时将为您做这些事情.

The miniscule amount of an inactive Cloud Run application gets is probably only good for garbage collection, which go runtime will be doing for you.

如果您想在请求的上下文中等待goroutine完成,则应使用诸如 chan sync.WaitGroup#Done().

If you want to wait for your goroutine to finish during the context of the request, you should block the request from returning, by using something like a blocking-receive from a chan, or sync.WaitGroup#Done().

这篇关于Google Cloud Run和golang goroutines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆