Skip to content

Commit 0a10c62

Browse files
committed
internal/gcpdial: throttle polling for instances
gcpdial polls for instances in case instances from the sandbox instance group change. Unfortunately, it is causing us to go over quota in our production project, which breaks other services (mainly deploying from Cloud Build). This change introduces a 10s ticker. Verified locally using internal/gcpdial/gcpdialtool. Updates golang/go#38315 Change-Id: I36009397233c6fc1663a7f2cee4f47caea1ca161 Reviewed-on: https://go-review.googlesource.com/c/playground/+/227648 Run-TryBot: Alexander Rakoczy <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 8e01013 commit 0a10c62

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

internal/gcpdial/gcpdial.go

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"sync"
1919
"time"
2020

21-
compute "google.golang.org/api/compute/v1"
21+
"google.golang.org/api/compute/v1"
2222
)
2323

2424
type Dialer struct {
@@ -188,46 +188,58 @@ func (d *Dialer) pickIP() (string, bool) {
188188
}
189189

190190
func (d *Dialer) poll() {
191+
// TODO(golang.org/issue/38315) - Plumb a context in here correctly
192+
ctx := context.TODO()
193+
t := time.NewTicker(10 * time.Second)
194+
defer t.Stop()
191195
for {
192-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
193-
res, err := d.lister.ListInstances(ctx)
194-
cancel()
195-
if err != nil {
196-
log.Printf("gcpdial: polling %v: %v", d.lister, err)
197-
time.Sleep(10 * time.Second)
198-
continue
196+
d.pollOnce(ctx)
197+
select {
198+
case <-ctx.Done():
199+
return
200+
case <-t.C:
199201
}
202+
}
203+
}
200204

201-
want := map[string]bool{} // the res []string turned into a set
202-
for _, instURL := range res {
203-
want[instURL] = true
204-
}
205+
func (d *Dialer) pollOnce(ctx context.Context) {
206+
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
207+
res, err := d.lister.ListInstances(ctx)
208+
cancel()
209+
if err != nil {
210+
log.Printf("gcpdial: polling %v: %v", d.lister, err)
211+
return
212+
}
213+
214+
want := map[string]bool{} // the res []string turned into a set
215+
for _, instURL := range res {
216+
want[instURL] = true
217+
}
205218

206-
d.mu.Lock()
207-
// Stop + remove any health check probers that no longer appear in the
208-
// instance group.
209-
for instURL, prober := range d.prober {
210-
if !want[instURL] {
211-
prober.cancel()
212-
delete(d.prober, instURL)
213-
}
219+
d.mu.Lock()
220+
defer d.mu.Unlock()
221+
// Stop + remove any health check probers that no longer appear in the
222+
// instance group.
223+
for instURL, prober := range d.prober {
224+
if !want[instURL] {
225+
prober.cancel()
226+
delete(d.prober, instURL)
227+
}
228+
}
229+
// And start any new health check probers that are newly added
230+
// (or newly known at least) to the instance group.
231+
for _, instURL := range res {
232+
if _, ok := d.prober[instURL]; ok {
233+
continue
214234
}
215-
// And start any new health check probers that are newly added
216-
// (or newly known at least) to the instance group.
217-
for _, instURL := range res {
218-
if _, ok := d.prober[instURL]; ok {
219-
continue
220-
}
221-
p := newProber(d, instURL)
222-
go p.probeLoop()
223-
if d.prober == nil {
224-
d.prober = map[string]*prober{}
225-
}
226-
d.prober[instURL] = p
235+
p := newProber(d, instURL)
236+
go p.probeLoop()
237+
if d.prober == nil {
238+
d.prober = map[string]*prober{}
227239
}
228-
d.lastInstances = res
229-
d.mu.Unlock()
240+
d.prober[instURL] = p
230241
}
242+
d.lastInstances = res
231243
}
232244

233245
// NewRegionInstanceGroupDialer returns a new dialer that dials named

0 commit comments

Comments
 (0)