mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-24 06:27:17 +02:00
grpc: wait for connect to be ready before making calls (#3253)
* grpc: wait for connect to be ready before making calls * make sure to stop the ticker
This commit is contained in:
parent
443f4a01f5
commit
761c17b8ac
5 changed files with 42 additions and 6 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/connectivity"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/log"
|
||||
"github.com/pomerium/pomerium/internal/telemetry"
|
||||
|
@ -54,7 +55,7 @@ func NewGRPCClientConn(ctx context.Context, opts *Options, other ...grpc.DialOpt
|
|||
grpc.WithInsecure(),
|
||||
}
|
||||
dialOptions = append(dialOptions, other...)
|
||||
log.Info(ctx).Str("address", opts.Address).Msg("dialing")
|
||||
log.Info(ctx).Str("address", opts.Address).Msg("grpc: dialing")
|
||||
return grpc.DialContext(ctx, opts.Address, dialOptions...)
|
||||
}
|
||||
|
||||
|
@ -124,3 +125,28 @@ func (cache *CachedOutboundGRPClientConn) Get(ctx context.Context, opts *Outboun
|
|||
cache.opts = opts
|
||||
return cache.current, nil
|
||||
}
|
||||
|
||||
// WaitForReady waits for the connection to be ready.
|
||||
func WaitForReady(ctx context.Context, cc *grpc.ClientConn, timeout time.Duration) error {
|
||||
if cc.GetState() == connectivity.Ready {
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx, clearTimeout := context.WithTimeout(ctx, timeout)
|
||||
defer clearTimeout()
|
||||
|
||||
cc.Connect()
|
||||
ticker := time.NewTicker(time.Millisecond * 50)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-ticker.C:
|
||||
}
|
||||
|
||||
if cc.GetState() == connectivity.Ready {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue