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

chore: convert cache client constructor to accept args #209

Merged
merged 5 commits into from
Mar 1, 2023
Merged
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
2 changes: 1 addition & 1 deletion momento/dictionary_increment.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *DictionaryIncrementRequest) initGrpcRequest(client scsDataClient) error
}

var ttl uint64
if ttl, err = prepareTTL(r, client.defaultTtl); err != nil {
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion momento/dictionary_set_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *DictionarySetFieldsRequest) initGrpcRequest(client scsDataClient) error
}

var ttl uint64
if ttl, err = prepareTTL(r, client.defaultTtl); err != nil {
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
return err
}

Expand Down
14 changes: 7 additions & 7 deletions momento/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ var _ = Describe("Dictionary methods", func() {
}),
).To(BeAssignableToTypeOf(&DictionaryFetchHit{}))

time.Sleep(sharedContext.DefaultTTL)
time.Sleep(sharedContext.DefaultTtl)

Expect(
sharedContext.Client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{
Expand Down Expand Up @@ -675,7 +675,7 @@ var _ = Describe("Dictionary methods", func() {
When("collection TTL is empty", func() {

It("will have a false refreshTTL and fetch will miss after client default ttl", func() {
time.Sleep(sharedContext.DefaultTTL / 2)
time.Sleep(sharedContext.DefaultTtl / 2)
Expect(
sharedContext.Client.DictionarySetField(sharedContext.Ctx, &DictionarySetFieldRequest{
CacheName: sharedContext.CacheName,
Expand All @@ -686,7 +686,7 @@ var _ = Describe("Dictionary methods", func() {
}),
).To(BeAssignableToTypeOf(&DictionarySetFieldSuccess{}))

time.Sleep(sharedContext.DefaultTTL / 2)
time.Sleep(sharedContext.DefaultTtl / 2)

Expect(
sharedContext.Client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{
Expand All @@ -708,13 +708,13 @@ var _ = Describe("Dictionary methods", func() {
Field: String("myField3"),
Value: String("myValue3"),
Ttl: utils.CollectionTtl{
Ttl: sharedContext.DefaultTTL + time.Second*60,
Ttl: sharedContext.DefaultTtl + time.Second*60,
RefreshTtl: false,
},
}),
).To(BeAssignableToTypeOf(&DictionarySetFieldSuccess{}))

time.Sleep(sharedContext.DefaultTTL)
time.Sleep(sharedContext.DefaultTtl)

Expect(
sharedContext.Client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{
Expand All @@ -732,13 +732,13 @@ var _ = Describe("Dictionary methods", func() {
Field: String("myField3"),
Value: String("myValue3"),
Ttl: utils.CollectionTtl{
Ttl: sharedContext.DefaultTTL + time.Second*60,
Ttl: sharedContext.DefaultTtl + time.Second*60,
RefreshTtl: true,
},
}),
).To(BeAssignableToTypeOf(&DictionarySetFieldSuccess{}))

time.Sleep(sharedContext.DefaultTTL)
time.Sleep(sharedContext.DefaultTtl)

Expect(
sharedContext.Client.DictionaryFetch(sharedContext.Ctx, &DictionaryFetchRequest{
Expand Down
2 changes: 1 addition & 1 deletion momento/list_concatenate_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (r *ListConcatenateBackRequest) initGrpcRequest(client scsDataClient) error
}

var ttl uint64
if ttl, err = prepareTTL(r, client.defaultTtl); err != nil {
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion momento/list_concatenate_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (r *ListConcatenateFrontRequest) initGrpcRequest(client scsDataClient) erro
}

var ttl uint64
if ttl, err = prepareTTL(r, client.defaultTtl); err != nil {
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion momento/list_push_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *ListPushBackRequest) initGrpcRequest(client scsDataClient) error {
}

var ttl uint64
if ttl, err = prepareTTL(r, client.defaultTtl); err != nil {
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion momento/list_push_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *ListPushFrontRequest) initGrpcRequest(client scsDataClient) error {
}

var ttl uint64
if ttl, err = prepareTTL(r, client.defaultTtl); err != nil {
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions momento/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type hasElements interface {
elements() map[string]Value
}

type hasTTL interface {
type hasTtl interface {
ttl() time.Duration
}

Expand Down Expand Up @@ -181,7 +181,7 @@ func prepareElements(r hasElements) (map[string][]byte, error) {
return retMap, nil
}

func prepareTTL(r hasTTL, defaultTtl time.Duration) (uint64, error) {
func prepareTtl(r hasTtl, defaultTtl time.Duration) (uint64, error) {
ttl := r.ttl()
if r.ttl() == time.Duration(0) {
ttl = defaultTtl
Expand Down
12 changes: 6 additions & 6 deletions momento/scalar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ var _ = Describe("Scalar methods", func() {
}),
).To(BeAssignableToTypeOf(&SetSuccess{}))

time.Sleep(sharedContext.DefaultTTL / 2)
time.Sleep(sharedContext.DefaultTtl / 2)

Expect(
sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{
Expand All @@ -164,7 +164,7 @@ var _ = Describe("Scalar methods", func() {
}),
).To(BeAssignableToTypeOf(&GetHit{}))

time.Sleep(sharedContext.DefaultTTL)
time.Sleep(sharedContext.DefaultTtl)

Expect(
sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{
Expand All @@ -183,11 +183,11 @@ var _ = Describe("Scalar methods", func() {
CacheName: sharedContext.CacheName,
Key: key,
Value: value,
TTL: sharedContext.DefaultTTL * 2,
Ttl: sharedContext.DefaultTtl * 2,
}),
).To(BeAssignableToTypeOf(&SetSuccess{}))

time.Sleep(sharedContext.DefaultTTL / 2)
time.Sleep(sharedContext.DefaultTtl / 2)

Expect(
sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{
Expand All @@ -196,7 +196,7 @@ var _ = Describe("Scalar methods", func() {
}),
).To(BeAssignableToTypeOf(&GetHit{}))

time.Sleep(sharedContext.DefaultTTL)
time.Sleep(sharedContext.DefaultTtl)

Expect(
sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{
Expand All @@ -205,7 +205,7 @@ var _ = Describe("Scalar methods", func() {
}),
).To(BeAssignableToTypeOf(&GetHit{}))

time.Sleep(sharedContext.DefaultTTL)
time.Sleep(sharedContext.DefaultTtl)

Expect(
sharedContext.Client.Get(sharedContext.Ctx, &GetRequest{
Expand Down
6 changes: 3 additions & 3 deletions momento/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type SetRequest struct {
Value Value
// Optional Time to live in cache in seconds.
// If not provided, then default TTL for the cache client instance is used.
TTL time.Duration
Ttl time.Duration

grpcRequest *pb.XSetRequest
grpcResponse *pb.XSetResponse
Expand All @@ -41,7 +41,7 @@ func (r *SetRequest) key() Key { return r.Key }

func (r *SetRequest) value() Value { return r.Value }

func (r *SetRequest) ttl() time.Duration { return r.TTL }
func (r *SetRequest) ttl() time.Duration { return r.Ttl }

func (r *SetRequest) requestName() string { return "Set" }

Expand All @@ -59,7 +59,7 @@ func (r *SetRequest) initGrpcRequest(client scsDataClient) error {
}

var ttl uint64
if ttl, err = prepareTTL(r, client.defaultTtl); err != nil {
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion momento/set_add_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *SetAddElementsRequest) initGrpcRequest(client scsDataClient) error {
}

var ttl uint64
if ttl, err = prepareTTL(r, client.defaultTtl); err != nil {
if ttl, err = prepareTtl(r, client.defaultTtl); err != nil {
return err
}

Expand Down
12 changes: 6 additions & 6 deletions momento/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ var _ = Describe("Set methods", func() {
Expect(err).To(BeNil())
Expect(fetchResp).To(HaveSetLength(1))

time.Sleep(sharedContext.DefaultTTL)
time.Sleep(sharedContext.DefaultTtl)

fetchResp, err = sharedContext.Client.SetFetch(sharedContext.Ctx, &SetFetchRequest{
CacheName: sharedContext.CacheName,
Expand Down Expand Up @@ -365,7 +365,7 @@ var _ = Describe("Set methods", func() {
SetName: sharedContext.CollectionName,
Element: String("hello"),
Ttl: utils.CollectionTtl{
Ttl: sharedContext.DefaultTTL + time.Second*60,
Ttl: sharedContext.DefaultTtl + time.Second*60,
RefreshTtl: true,
},
}),
Expand All @@ -378,7 +378,7 @@ var _ = Describe("Set methods", func() {
Expect(err).To(BeNil())
Expect(fetchResp).To(HaveSetLength(2))

time.Sleep(sharedContext.DefaultTTL)
time.Sleep(sharedContext.DefaultTtl)

fetchResp, err = sharedContext.Client.SetFetch(sharedContext.Ctx, &SetFetchRequest{
CacheName: sharedContext.CacheName,
Expand All @@ -395,7 +395,7 @@ var _ = Describe("Set methods", func() {
SetName: sharedContext.CollectionName,
Element: String("hello"),
Ttl: utils.CollectionTtl{
Ttl: sharedContext.DefaultTTL + 1*time.Second,
Ttl: sharedContext.DefaultTtl + 1*time.Second,
RefreshTtl: false,
},
}),
Expand All @@ -408,7 +408,7 @@ var _ = Describe("Set methods", func() {
}),
).To(HaveSetLength(2))

time.Sleep(sharedContext.DefaultTTL + 500*time.Millisecond)
time.Sleep(sharedContext.DefaultTtl + 500*time.Millisecond)

Expect(
sharedContext.Client.SetFetch(sharedContext.Ctx, &SetFetchRequest{
Expand Down Expand Up @@ -438,7 +438,7 @@ var _ = Describe("Set methods", func() {
Expect(err).To(BeNil())
Expect(fetchResp).To(HaveSetLength(2))

time.Sleep(sharedContext.DefaultTTL / 2)
time.Sleep(sharedContext.DefaultTtl / 2)

fetchResp, err = sharedContext.Client.SetFetch(sharedContext.Ctx, &SetFetchRequest{
CacheName: sharedContext.CacheName,
Expand Down
15 changes: 10 additions & 5 deletions momento/simple_cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,16 @@ type defaultScsClient struct {
type CacheClientProps struct {
Configuration config.Configuration
CredentialProvider auth.CredentialProvider
DefaultTTL time.Duration
DefaultTtl time.Duration
}

// NewCacheClient returns a new CacheClient with provided authToken, DefaultTTLSeconds, and opts arguments.
func NewCacheClient(props *CacheClientProps) (CacheClient, error) {
// NewCacheClient returns a new CacheClient with provided configuration, credential provider, and default TTL seconds arguments.
func NewCacheClient(configuration config.Configuration, credentialProvider auth.CredentialProvider, defaultTtl time.Duration) (CacheClient, error) {
props := CacheClientProps{
Configuration: configuration,
CredentialProvider: credentialProvider,
DefaultTtl: defaultTtl,
}
if props.Configuration.GetClientSideTimeout() < 1 {
return nil, momentoerrors.NewMomentoSvcErr(momentoerrors.InvalidArgumentError, "request timeout must not be 0", nil)
}
Expand All @@ -101,7 +106,7 @@ func NewCacheClient(props *CacheClientProps) (CacheClient, error) {
return nil, convertMomentoSvcErrorToCustomerError(momentoerrors.ConvertSvcErr(err))
}

if props.DefaultTTL == 0 {
if props.DefaultTtl == 0 {
return nil, convertMomentoSvcErrorToCustomerError(
momentoerrors.NewMomentoSvcErr(
momentoerrors.InvalidArgumentError,
Expand All @@ -112,7 +117,7 @@ func NewCacheClient(props *CacheClientProps) (CacheClient, error) {
dataClient, err := newScsDataClient(&models.DataClientRequest{
CredentialProvider: props.CredentialProvider,
Configuration: props.Configuration,
DefaultTtl: props.DefaultTTL,
DefaultTtl: props.DefaultTtl,
})
if err != nil {
return nil, convertMomentoSvcErrorToCustomerError(momentoerrors.ConvertSvcErr(err))
Expand Down
8 changes: 4 additions & 4 deletions momento/simple_cache_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var _ = Describe("CacheClient", func() {
})

It(`errors on an invalid TTL`, func() {
sharedContext.ClientProps.DefaultTTL = 0 * time.Second
client, err := NewCacheClient(sharedContext.ClientProps)
sharedContext.DefaultTtl = 0 * time.Second
client, err := NewCacheClient(sharedContext.Configuration, sharedContext.CredentialProvider, sharedContext.DefaultTtl)

Expect(client).To(BeNil())
Expect(err).NotTo(BeNil())
Expand All @@ -33,9 +33,9 @@ var _ = Describe("CacheClient", func() {

It(`errors on invalid timeout`, func() {
badRequestTimeout := 0 * time.Second
sharedContext.ClientProps.Configuration = config.LatestLaptopConfig().WithClientTimeout(badRequestTimeout)
sharedContext.Configuration = config.LatestLaptopConfig().WithClientTimeout(badRequestTimeout)
Expect(
NewCacheClient(sharedContext.ClientProps),
NewCacheClient(sharedContext.Configuration, sharedContext.CredentialProvider, sharedContext.DefaultTtl),
).Error().To(HaveMomentoErrorCode(InvalidArgumentError))
})
})
2 changes: 1 addition & 1 deletion momento/sorted_set_increment_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (r *SortedSetIncrementScoreRequest) initGrpcRequest(client scsDataClient) e
}

var ttlMillis uint64
if ttlMillis, err = prepareTTL(r, client.defaultTtl); err != nil {
if ttlMillis, err = prepareTtl(r, client.defaultTtl); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion momento/sorted_set_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (r *SortedSetPutRequest) initGrpcRequest(client scsDataClient) error {
}

var ttlMills uint64
if ttlMills, err = prepareTTL(r, client.defaultTtl); err != nil {
if ttlMills, err = prepareTtl(r, client.defaultTtl); err != nil {
return err
}

Expand Down
Loading