Skip to content

Commit

Permalink
Publish gosdk from 553145881ec
Browse files Browse the repository at this point in the history
  • Loading branch information
complynx committed Jan 8, 2025
1 parent db2817d commit 80de43c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
1 change: 0 additions & 1 deletion .github/latest-deps/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
_ "github.com/google/uuid"
_ "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
_ "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry"
_ "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/timeout"
_ "github.com/stretchr/testify/assert"
_ "github.com/stretchr/testify/require"
_ "go.uber.org/mock/gomock"
Expand Down
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

0 comments on commit 80de43c

Please sign in to comment.