Skip to content

Commit

Permalink
feat: retry on gRPC status error INTERNAL only if it fits `RST_STRE…
Browse files Browse the repository at this point in the history
…AM` regex

Signed-off-by: Daniel Akhterov <daniel@launchbadge.com>
  • Loading branch information
janaakhterov committed Jun 30, 2021
1 parent 9061b43 commit 85bd404
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 3 additions & 4 deletions executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package hedera

import (
"context"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"math"
"time"

"github.com/pkg/errors"

"github.com/hashgraph/hedera-sdk-go/v2/proto"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -114,7 +113,7 @@ func execute(

if err != nil {
errPersistent = err
if grpcErr, ok := status.FromError(err); ok && (grpcErr.Code() == codes.Unavailable || grpcErr.Code() == codes.ResourceExhausted) {
if defaultRetryHandler(err) {
node.increaseDelay()
continue
}
Expand Down
21 changes: 20 additions & 1 deletion topic_message_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"context"
"io"
"math"
"regexp"
"time"

"github.com/hashgraph/hedera-sdk-go/v2/proto/mirror"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var RST_STREAM *regexp.Regexp

type TopicMessageQuery struct {
pb *mirror.ConsensusTopicQuery
errorHandler func(stat status.Status)
Expand Down Expand Up @@ -219,8 +222,24 @@ func defaultRetryHandler(err error) bool {
code := status.Code(err)

switch code {
case codes.NotFound, codes.ResourceExhausted, codes.Internal, codes.Unavailable:
case codes.NotFound, codes.ResourceExhausted, codes.Unavailable:
return true
case codes.Internal:
if RST_STREAM == nil {
var err1 error
RST_STREAM, err1 = regexp.Compile(".*(rst.stream.*internal.error|internal.error.*rst.stream).*")
if err1 != nil {
panic(err1)
}
}

grpcErr, ok := status.FromError(err)

if !ok {
return false
}

return RST_STREAM.FindIndex([]byte(grpcErr.Message())) != nil
default:
return false
}
Expand Down

0 comments on commit 85bd404

Please sign in to comment.