Skip to content

Commit 7082c9c

Browse files
committed
Handle all gRPC error codes
1 parent 8726359 commit 7082c9c

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

internal/auditlog/tail.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111

1212
logging "cloud.google.com/go/logging/apiv2"
1313
"cloud.google.com/go/logging/apiv2/loggingpb"
14+
"golang.org/x/time/rate"
1415
"google.golang.org/genproto/googleapis/cloud/audit"
15-
"google.golang.org/grpc/codes"
1616
"google.golang.org/grpc/status"
1717
)
1818

@@ -23,20 +23,23 @@ func Tail(ctx context.Context, projectID, clusterName string, cb func(*audit.Aud
2323
}
2424
defer client.Close()
2525

26+
// Limiter used to throttle log tailing restarts
27+
limiter := rate.NewLimiter(rate.Every(time.Second*15), 3)
28+
2629
for {
2730
select {
2831
case <-ctx.Done():
2932
return ctx.Err()
3033
default:
3134
}
3235

36+
if err = limiter.Wait(ctx); err != nil {
37+
return fmt.Errorf("limit wait failed: %w", err)
38+
}
39+
3340
if err = tailLogs(ctx, client, projectID, clusterName, cb); err != nil {
34-
if grpcErr, ok := status.FromError(err); ok && grpcErr.Code() == codes.OutOfRange {
35-
// Expected error case:
36-
// "rpc error: code = OutOfRange desc = Session has run for the maximum allowed duration of 1h. To
37-
// continue, start a new session with the same request"
38-
slog.Warn("session expired, restarting", slog.Any("error", err))
39-
time.Sleep(time.Second * 5)
41+
if _, ok := status.FromError(err); ok {
42+
slog.Warn("gRPC request terminated, restarting", slog.Any("error", err))
4043
continue
4144
}
4245
return fmt.Errorf("log tailing failed: %w", err)

0 commit comments

Comments
 (0)