-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: adding ping * add testing * respect client timeout for pings * correct error message
- Loading branch information
1 parent
650a65e
commit 2121789
Showing
7 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package grpcmanagers | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
|
||
"github.com/momentohq/client-sdk-go/internal/interceptor" | ||
"github.com/momentohq/client-sdk-go/internal/models" | ||
"github.com/momentohq/client-sdk-go/internal/momentoerrors" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
) | ||
|
||
type PingGrpcManager struct { | ||
Conn *grpc.ClientConn | ||
} | ||
|
||
const PingPort = ":443" | ||
|
||
func NewPingGrpcManager(request *models.PingGrpcManagerRequest) (*PingGrpcManager, momentoerrors.MomentoSvcErr) { | ||
config := &tls.Config{ | ||
InsecureSkipVerify: false, | ||
} | ||
endpoint := fmt.Sprint(request.CredentialProvider.GetCacheEndpoint(), PingPort) | ||
authToken := request.CredentialProvider.GetAuthToken() | ||
conn, err := grpc.Dial( | ||
endpoint, | ||
grpc.WithTransportCredentials(credentials.NewTLS(config)), | ||
grpc.WithUnaryInterceptor(interceptor.AddHeadersInterceptor(authToken)), | ||
) | ||
if err != nil { | ||
return nil, momentoerrors.ConvertSvcErr(err) | ||
} | ||
return &PingGrpcManager{Conn: conn}, nil | ||
} | ||
|
||
func (pingManager *PingGrpcManager) Close() momentoerrors.MomentoSvcErr { | ||
return momentoerrors.ConvertSvcErr(pingManager.Conn.Close()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/momentohq/client-sdk-go/internal/grpcmanagers" | ||
"github.com/momentohq/client-sdk-go/internal/models" | ||
"github.com/momentohq/client-sdk-go/internal/momentoerrors" | ||
pb "github.com/momentohq/client-sdk-go/internal/protos" | ||
) | ||
|
||
type ScsPingClient struct { | ||
requestTimeout time.Duration | ||
grpcManager *grpcmanagers.PingGrpcManager | ||
grpcClient pb.PingClient | ||
} | ||
|
||
func NewScsPingClient(request *models.PingClientRequest) (*ScsPingClient, momentoerrors.MomentoSvcErr) { | ||
pingManager, err := grpcmanagers.NewPingGrpcManager(&models.PingGrpcManagerRequest{ | ||
CredentialProvider: request.CredentialProvider, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ScsPingClient{ | ||
requestTimeout: request.Configuration.GetClientSideTimeout(), | ||
grpcManager: pingManager, | ||
grpcClient: pb.NewPingClient(pingManager.Conn), | ||
}, nil | ||
} | ||
|
||
func (client *ScsPingClient) Close() momentoerrors.MomentoSvcErr { | ||
return client.grpcManager.Close() | ||
} | ||
|
||
func (client *ScsPingClient) Ping(ctx context.Context) momentoerrors.MomentoSvcErr { | ||
ctx, cancel := context.WithTimeout(ctx, client.requestTimeout) | ||
defer cancel() | ||
_, err := client.grpcClient.Ping(ctx, &pb.XPingRequest{}) | ||
if err != nil { | ||
return momentoerrors.ConvertSvcErr(err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package momento_test | ||
|
||
import ( | ||
. "github.com/momentohq/client-sdk-go/momento/test_helpers" | ||
. "github.com/momentohq/client-sdk-go/responses" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("ping", func() { | ||
var sharedContext SharedContext | ||
|
||
BeforeEach(func() { | ||
sharedContext = NewSharedContext() | ||
sharedContext.CreateDefaultCache() | ||
DeferCleanup(func() { | ||
sharedContext.Close() | ||
}) | ||
}) | ||
|
||
It("receives successful ping responses", func() { | ||
for i := 0; i < 25; i++ { | ||
Expect( | ||
sharedContext.Client.Ping(sharedContext.Ctx), | ||
).To(BeAssignableToTypeOf(&PingSuccess{})) | ||
} | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package responses | ||
|
||
type PingResponse interface { | ||
isPingResponse() | ||
} | ||
|
||
type PingSuccess struct{} | ||
|
||
func (PingSuccess) isPingResponse() {} |