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 my authorship #68

Closed
wants to merge 1 commit into from
Closed
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
51 changes: 51 additions & 0 deletions conn/inner_timeout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package conn

import (
"context"
"errors"
"fmt"
"time"

"google.golang.org/grpc"
)

type TimeoutError struct {
wrapped error
}

func (t *TimeoutError) Error() string {
return fmt.Sprintf("gosdk request timeout: %s", t.wrapped.Error())
}

func (t *TimeoutError) Unwrap() error {
return t.wrapped
}

// UnaryClientTimeoutInterceptor returns a new unary client interceptor that sets a timeout on the request context.
func UnaryClientTimeoutInterceptor(timeout time.Duration) grpc.UnaryClientInterceptor {
return func(
ctx context.Context,
method string,
req, reply any,
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
timedCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
err := invoker(timedCtx, method, req, reply, cc, opts...)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
select {
case <-ctx.Done():
return err
default:
return &TimeoutError{wrapped: err}
}
}
// Otherwise, return the original error
return err
}
return nil
}
}
5 changes: 5 additions & 0 deletions operations/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/nebius/gosdk/conn"
common "github.com/nebius/gosdk/proto/nebius/common/v1"
)

Expand Down Expand Up @@ -186,6 +187,10 @@ func (o *opWrapper) Wait(ctx context.Context, opts ...grpc.CallOption) (Operatio
case <-time.After(interval):
_, err := o.Poll(ctx, opts...)
if err != nil {
tErr := &conn.TimeoutError{}
if errors.As(err, &tErr) {
continue
}
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/timeout"
"google.golang.org/grpc"
grpc_creds "google.golang.org/grpc/credentials"

Expand Down Expand Up @@ -160,7 +159,7 @@ func New(ctx context.Context, opts ...Option) (*SDK, error) { //nolint:funlen
}

// apply timeout after retry and auth interceptors for each request
dialOpts = append(dialOpts, grpc.WithChainUnaryInterceptor(timeout.UnaryClientInterceptor(1*time.Minute)))
dialOpts = append(dialOpts, grpc.WithChainUnaryInterceptor(conn.UnaryClientTimeoutInterceptor(1*time.Minute)))

dialer := conn.NewDialer(logger, dialOpts...)
closes = append(closes, dialer.Close)
Expand Down
Loading