Skip to content

Commit

Permalink
feat: usability improvements and testing updates (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
eaddingtonwhite authored Feb 17, 2022
1 parent 3b37174 commit 8ecd287
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 245 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: vendor
vendor:
go mod tidy
go mod vendor

.PHONY: lint
lint:
@echo "golint $(LINTARGS)"
@for pkg in $(shell go list ./...) ; do \
golint $(LINTARGS) $$pkg ; \
done
111 changes: 70 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

:warning: Experimental SDK :warning:

Go SDK for Momento is experimental and under active development.
There could be non-backward compatible changes or removal in the future.
Please be aware that you may need to update your source code with the current version of the SDK when its version gets upgraded.
Go SDK for Momento is experimental and under active development. There could be non-backward compatible changes or
removal in the future. Please be aware that you may need to update your source code with the current version of the SDK
when its version gets upgraded.

---

Expand All @@ -14,7 +14,8 @@ Please be aware that you may need to update your source code with the current ve
<img src="images/gopher.png" alt="Logo" width="200" height="150">
</div>

Go SDK for Momento, a serverless cache that automatically scales without any of the operational overhead required by traditional caching solutions.
Go SDK for Momento, a serverless cache that automatically scales without any of the operational overhead required by
traditional caching solutions.

<br/>

Expand All @@ -23,7 +24,12 @@ Go SDK for Momento, a serverless cache that automatically scales without any of
## Requirements

- [Go version 1.17.\*](https://go.dev/dl/)
- A Momento Auth Token is required, you can generate one using the [Momento CLI](https://github.com/momentohq/momento-cli)
- A Momento Auth Token is required, you can generate one using
the [Momento CLI](https://github.com/momentohq/momento-cli)
- golang
- `brew install go`
- golint
- `go get -u golang.org/x/lint/golint`

<br/>

Expand All @@ -36,71 +42,94 @@ Check out our [Go SDK example repo](https://github.com/momentohq/client-sdk-exam
## Using Momento

```go
package main

import (
"fmt"
"log"
"os"

"github.com/google/uuid"
"github.com/momentohq/client-sdk-go/momento"
. "github.com/momentohq/client-sdk-go/momento"
)

func main() {
var AuthToken = os.Getenv("MOMENTO_AUTH_TOKEN")
authToken := os.Getenv("MOMENTO_AUTH_TOKEN")
const (
CacheName = "cache"
ItemDefaultTtlSeconds = 60
cacheName = "my-first-cache-😊"
itemDefaultTtlSeconds = 60
)

if AuthToken == "" {
if authToken == "" {
log.Fatal("Missing required environment variable MOMENTO_AUTH_TOKEN")
}

// Initializes Momento
client, err := momento.SimpleCacheClient(&momento.SimpleCacheClientRequest{
AuthToken: AuthToken,
DefaultTtlSeconds: ItemDefaultTtlSeconds,
client, err := NewSimpleCacheClient(authToken, itemDefaultTtlSeconds)
if err != nil {
log.Fatal(fmt.Sprintf("failed initilizing cache client with err %+v", err))
}

// Create Cache Ignore if Cache already exists
err = client.CreateCache(&CreateCacheRequest{
CacheName: cacheName,
})
if err != nil {
panic(err)
if momentoErr, ok := err.(MomentoError); ok {
if momentoErr.Code() != AlreadyExists {
log.Fatal(fmt.Sprintf(
"failed creating cache %s with err %+v",
cacheName, momentoErr,
))
}
}
}
// Create Cache and check if CacheName exists
err = client.CreateCache(&momento.CreateCacheRequest{
CacheName: CacheName,
log.Println(fmt.Sprintf("Cache %s is created", cacheName))

// Sets key with default TTL and custom ttl and then retrieves the items from cache
key1 := []byte(uuid.NewString())
key2 := []byte(uuid.NewString())
value1 := []byte(uuid.NewString())
value2 := []byte(uuid.NewString())

log.Println(fmt.Sprintf("Setting key: %s, value: %s", key1, value1))
_, err = client.Set(&CacheSetRequest{
CacheName: cacheName,
Key: key1,
Value: value2,
})
if err != nil && !strings.Contains(err.Error(), "AlreadyExists") {
panic(err)
if err != nil {
log.Fatal(fmt.Sprintf("failed setting key %s with err %+v", key1, err))
}
log.Printf("Cache named %s is created\n", CacheName)

// Sets key with default TTL and gets value with that key
key := []byte(uuid.NewString())
value := []byte(uuid.NewString())
log.Printf("Setting key: %s, value: %s\n", key, value)
_, err = client.Set(&momento.CacheSetRequest{
CacheName: CacheName,
Key: key,
Value: value,
log.Println(fmt.Sprintf("Setting key with custom ttl key: %s, value: %s", key2, value2))
_, err = client.Set(&CacheSetRequest{
CacheName: cacheName,
Key: key2,
Value: value2,
TtlSeconds: TTL(60),
})
if err != nil {
panic(err)
log.Fatal(fmt.Sprintf("failed setting key %s with err %+v", key2, err))
}
log.Printf("Getting key: %s\n", key)
resp, err := client.Get(&momento.CacheGetRequest{
CacheName: CacheName,
Key: key,
log.Println(fmt.Sprintf("Getting key: %s", key1))
resp, err := client.Get(&CacheGetRequest{
CacheName: cacheName,
Key: key1,
})
if err != nil {
panic(err)
log.Fatal(fmt.Sprintf("failed getting key %s with err %+v", key1, err))
}
log.Printf("Lookup resulted in a : %s\n", resp.Result())
log.Printf("Looked up value: %s\n", resp.StringValue())
log.Println(fmt.Sprintf(
"Get request succeded result: %s value: %s",
resp.Result(), resp.StringValue(),
))

// Permanently delete the cache
err = client.DeleteCache(&momento.DeleteCacheRequest{CacheName: CacheName})
// Delete the cache
err = client.DeleteCache(&DeleteCacheRequest{CacheName: cacheName})
if err != nil {
panic(err)
log.Fatal(fmt.Sprintf("failed deleting cache %s with err %+v", cacheName, err))
}
log.Printf("Cache named %s is deleted\n", CacheName)
log.Printf(fmt.Sprintf("Cache %s is deleted", cacheName))
}
```

Expand Down
7 changes: 0 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@ require (
google.golang.org/protobuf v1.27.1
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

require (
github.com/golang/protobuf v1.5.0 // indirect
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/text v0.3.7 // indirect
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XP
github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -48,13 +47,11 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -126,11 +123,9 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
17 changes: 12 additions & 5 deletions internal/models/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ type ControlClientRequest struct {
}

type DataClientRequest struct {
AuthToken string
Endpoint string
DefaultTtlSeconds uint32
DataCtxTimeout *uint32
AuthToken string
Endpoint string
DefaultTtlSeconds uint32
RequestTimeoutSeconds uint32
}

type ConvertEcacheResultRequest struct {
Expand All @@ -41,5 +41,12 @@ type ConvertEcacheResultRequest struct {
}

func ConvertEcacheResult(request ConvertEcacheResultRequest) momentoerrors.MomentoSvcErr {
return momentoerrors.NewMomentoSvcErr(momentoerrors.InternalServerError, fmt.Sprintf("CacheService returned an unexpected result: %v for operation: %s with message: %s", request.ECacheResult, request.OpName, request.Message), nil)
return momentoerrors.NewMomentoSvcErr(
momentoerrors.InternalServerError,
fmt.Sprintf(
"CacheService returned an unexpected result: %v for operation: %s with message: %s",
request.ECacheResult, request.OpName, request.Message,
),
nil,
)
}
25 changes: 17 additions & 8 deletions internal/resolver/end_point_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

const (
MOMENTO_CONTROL_ENDPOINT_PREFIX = "control."
MOMENTO_CACHE_ENDPOINT_PREFIX = "cache."
CONTROL_ENDPOINT_CLAIM_ID = "cp"
CACHE_ENDPOINT_CLAIM_ID = "c"
momentoControlEndpointPrefix = "control."
momentoCacheEndpointPrefix = "cache."
controlEndpointClaimId = "cp"
cacheEndpointClaimId = "c"
)

type Endpoints struct {
Expand All @@ -23,7 +23,10 @@ type Endpoints struct {

func Resolve(request *models.ResolveRequest) (*Endpoints, momentoerrors.MomentoSvcErr) {
if request.EndpointOverride != "" {
return &Endpoints{ControlEndpoint: MOMENTO_CONTROL_ENDPOINT_PREFIX + request.EndpointOverride, CacheEndpoint: MOMENTO_CACHE_ENDPOINT_PREFIX + request.EndpointOverride}, nil
return &Endpoints{
ControlEndpoint: momentoControlEndpointPrefix + request.EndpointOverride,
CacheEndpoint: momentoCacheEndpointPrefix + request.EndpointOverride,
}, nil
}
return getEndpointsFromToken(request.AuthToken)
}
Expand All @@ -34,8 +37,14 @@ func getEndpointsFromToken(authToken string) (*Endpoints, momentoerrors.MomentoS
return nil, momentoerrors.ConvertSvcErr(err)
}
if claims, ok := token.Claims.(jwt.MapClaims); ok {
return &Endpoints{ControlEndpoint: reflect.ValueOf(claims[CONTROL_ENDPOINT_CLAIM_ID]).String(), CacheEndpoint: reflect.ValueOf(claims[CACHE_ENDPOINT_CLAIM_ID]).String()}, nil
} else {
return nil, momentoerrors.NewMomentoSvcErr(momentoerrors.InvalidArgumentError, "Invalid Auth token.", nil)
return &Endpoints{
ControlEndpoint: reflect.ValueOf(claims[controlEndpointClaimId]).String(),
CacheEndpoint: reflect.ValueOf(claims[cacheEndpointClaimId]).String(),
}, nil
}
return nil, momentoerrors.NewMomentoSvcErr(
momentoerrors.InvalidArgumentError,
"Invalid Auth token.",
nil,
)
}
12 changes: 6 additions & 6 deletions internal/services/scs_control_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const ControlPort = ":443"
const ControlCtxTimeout = 60 * time.Second

type ScsControlClient struct {
grpcManager *grpcmanagers.ScsControlGrpcManager
controlClient pb.ScsControlClient
grpcManager *grpcmanagers.ScsControlGrpcManager
grpcClient pb.ScsControlClient
}

func NewScsControlClient(request *models.ControlClientRequest) (*ScsControlClient, momentoerrors.MomentoSvcErr) {
Expand All @@ -28,7 +28,7 @@ func NewScsControlClient(request *models.ControlClientRequest) (*ScsControlClien
if err != nil {
return nil, momentoerrors.ConvertSvcErr(err)
}
return &ScsControlClient{grpcManager: controlManager, controlClient: pb.NewScsControlClient(controlManager.Conn)}, nil
return &ScsControlClient{grpcManager: controlManager, grpcClient: pb.NewScsControlClient(controlManager.Conn)}, nil
}

func (client *ScsControlClient) Close() momentoerrors.MomentoSvcErr {
Expand All @@ -41,7 +41,7 @@ func (client *ScsControlClient) CreateCache(request *models.CreateCacheRequest)
}
ctx, cancel := context.WithTimeout(context.Background(), ControlCtxTimeout)
defer cancel()
_, err := client.controlClient.CreateCache(ctx, &pb.XCreateCacheRequest{CacheName: request.CacheName})
_, err := client.grpcClient.CreateCache(ctx, &pb.XCreateCacheRequest{CacheName: request.CacheName})
if err != nil {
return momentoerrors.ConvertSvcErr(err)
}
Expand All @@ -54,7 +54,7 @@ func (client *ScsControlClient) DeleteCache(request *models.DeleteCacheRequest)
}
ctx, cancel := context.WithTimeout(context.Background(), ControlCtxTimeout)
defer cancel()
_, err := client.controlClient.DeleteCache(ctx, &pb.XDeleteCacheRequest{CacheName: request.CacheName})
_, err := client.grpcClient.DeleteCache(ctx, &pb.XDeleteCacheRequest{CacheName: request.CacheName})
if err != nil {
return momentoerrors.ConvertSvcErr(err)
}
Expand All @@ -64,7 +64,7 @@ func (client *ScsControlClient) DeleteCache(request *models.DeleteCacheRequest)
func (client *ScsControlClient) ListCaches(request *models.ListCachesRequest) (*models.ListCachesResponse, momentoerrors.MomentoSvcErr) {
ctx, cancel := context.WithTimeout(context.Background(), ControlCtxTimeout)
defer cancel()
resp, err := client.controlClient.ListCaches(ctx, &pb.XListCachesRequest{NextToken: request.NextToken})
resp, err := client.grpcClient.ListCaches(ctx, &pb.XListCachesRequest{NextToken: request.NextToken})
if err != nil {
return nil, momentoerrors.ConvertSvcErr(err)
}
Expand Down
Loading

0 comments on commit 8ecd287

Please sign in to comment.