Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry logic #227

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ linters-settings:
dupl:
threshold: 150
funlen:
Lines: 135
Statements: 50
Lines: 150
Statements: 150
goconst:
min-len: 2
min-occurrences: 2
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Config struct {
Name string `default:"cmd-nsc-init" desc:"Name of the client" split_words:"true"`
DialTimeout time.Duration `default:"5s" desc:"timeout to dial NSMgr" split_words:"true"`
RequestTimeout time.Duration `default:"15s" desc:"timeout to request NSE" split_words:"true"`
RetryTimeout time.Duration `default:"0s" desc:"retry timeout" split_words:"true"`
RetryInterval time.Duration `default:"100ms" desc:"retry interval" split_words:"true"`
ConnectTo url.URL `default:"unix:///var/lib/networkservicemesh/nsm.io.sock" desc:"url to connect to" split_words:"true"`
MaxTokenLifetime time.Duration `default:"10m" desc:"maximum lifetime of tokens" split_words:"true"`
NetworkServices []url.URL `default:"" desc:"A list of Network Service Requests" split_words:"true"`
Expand Down
3 changes: 2 additions & 1 deletion internal/imports/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@

package imports

//go:generate bash -c "cd $(mktemp -d) && GO111MODULE=on go get github.com/edwarnicke/[email protected]"
//go:generate bash -c "rm -rf imports*.go"
//go:generate bash -c "cd $(mktemp -d) && GO111MODULE=on go get github.com/edwarnicke/[email protected]"
//go:generate bash -c "GOOS=linux ${GOPATH}/bin/imports-gen"
2 changes: 1 addition & 1 deletion internal/imports/imports_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 26 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"os/signal"
"syscall"
"time"

nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/edwarnicke/grpcfd"
Expand Down Expand Up @@ -130,7 +131,6 @@ func main() {
grpcfd.WithChainStreamInterceptor(),
grpcfd.WithChainUnaryInterceptor(),
grpc.WithDefaultCallOptions(
grpc.WaitForReady(true),
grpc.PerRPCCredentials(token.NewPerRPCCredentials(spiffejwt.TokenGeneratorFunc(source, rootConf.MaxTokenLifetime))),
),
grpc.WithTransportCredentials(
Expand Down Expand Up @@ -184,16 +184,33 @@ func main() {
},
}

requestCtx, cancelRequest := context.WithTimeout(ctx, rootConf.RequestTimeout)
defer cancelRequest()

resp, err := nsmClient.Request(requestCtx, request)

if err != nil {
logger.Fatalf("failed connect to NSMgr: %v", err.Error())
retryCtx, retryCancel := context.WithCancel(ctx)
defer retryCancel()
if rootConf.RequestTimeout != 0 {
retryCtx, retryCancel = context.WithTimeout(ctx, rootConf.RetryTimeout)
defer retryCancel()
}

logger.Infof("successfully connected to %v. Response: %v", u.NetworkService(), resp)
for {
requestCtx, cancelRequest := context.WithTimeout(ctx, rootConf.RequestTimeout)
defer cancelRequest()

resp, err := nsmClient.Request(requestCtx, request)

if err != nil {
logger.Errorf("failed connect to NSMgr: %v", err.Error())
select {
case <-retryCtx.Done():
logger.Fatalf("failed to connect to %s after %d retries")
default:
time.Sleep(rootConf.RetryInterval)
}
continue
}

logger.Infof("successfully connected to %v. Response: %v", u.NetworkService(), resp)
break
}
}
}

Expand Down