在两个不同的Go协程中监视Pods。这两个协程会收到相似的事件吗?

huangapple go评论68阅读模式
英文:

Watching pods in two different go routines. Will both routines get similar events?

问题

我目前正在做类似这样的事情:

watch, err := s.clientset.CoreV1().Pods("").Watch(context.TODO(), metav1.ListOptions{
    FieldSelector: fmt.Sprintf("spec.myfoo=%s", s.foo),
})

for event := range watch.ResultChan() {
    .......
}

我想知道如果我在两个不同的Go协程中有类似的代码,这两个watch会收到相同的事件,还是可能会收到不同的事件,取决于哪个协程先收到?

英文:

I currently am doing something like this

watch, err := s.clientset.CoreV1().Pods("").Watch(context.TODO(), metav1.ListOptions{
    FieldSelector: fmt.Sprintf("spec.myfoo=%s", s.foo),
})

for event := range watch.ResultChan() {
    .......
}

I am curious if I have something similar in two different go routines will both of the watches get the same events or if both routines might get different events. Based on who got it first?

答案1

得分: 3

Watch在内部与API服务器建立了一个长轮询连接。建立连接后,API服务器将发送一批初始事件和任何后续更改。一旦超时发生,连接将被断开。

由于您的场景涉及两个go例程,我们无法保证两者将同时开始执行并且两个长轮询连接将同时建立。此外,连接可能会在某个时刻断开。

在一个大型集群中,Pods不断被杀死和创建。因此,两个go例程接收到不同的事件是完全可能的。

英文:

Watch internally establishes a long poll connection with the API server. After establishing a connection, the API server will send a batch of initial events and any subsequent changes. Once a timeout has occurred, the connection will be dropped.

Since your scenario involves two go routines, we cannot guarantee that both will start executing simultaneously and that both long poll connections will be established simultaneously. Furthermore, the connection may drop at some point.

In a large cluster, pods are constantly being killed and created. Thus, it is certainly possible for two go routines to receive different events.

huangapple
  • 本文由 发表于 2022年11月5日 04:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/74322897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定