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

feat: added request id #10

Merged
merged 4 commits into from
Sep 24, 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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ require (
require (
github.com/Aton-Kish/goptr v0.1.0
github.com/briandowns/spinner v1.23.0
github.com/google/uuid v1.3.1
github.com/hashicorp/golang-lru/v2 v2.0.6
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
go.uber.org/mock v0.2.0
golang.org/x/sync v0.3.0
)

require (
Expand All @@ -38,7 +41,7 @@ require (
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.3.3 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru/v2 v2.0.6 h1:3xi/Cafd1NaoEnS/yDssIiuVeDVywU0QdFGl3aQaQHM=
github.com/hashicorp/golang-lru/v2 v2.0.6/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down Expand Up @@ -76,12 +80,14 @@ go.uber.org/mock v0.2.0/go.mod h1:J0y0rp9L3xiff1+ZBfKxlC1fz2+aO16tw0tsDOixfuM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
44 changes: 44 additions & 0 deletions internal/syncup/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2023 Aton-Kish
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package syncup

import (
"context"
)

type contextKey int

const (
contextKeyRequestID contextKey = iota
)

func RequestID(ctx context.Context) string {
v, ok := ctx.Value(contextKeyRequestID).(string)
if !ok {
return ""
}

return v
}

func WithRequestID(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, contextKeyRequestID, id)
}
112 changes: 112 additions & 0 deletions internal/syncup/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) 2023 Aton-Kish
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package syncup

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRequestID(t *testing.T) {
type args struct {
ctx context.Context //nolint:containedctx
}

type expected struct {
res string
}

tests := []struct {
name string
args args
expected expected
}{
{
name: "happy path: request id was found",
args: args{
ctx: context.WithValue(context.Background(), contextKeyRequestID, "RequestID"),
},
expected: expected{
res: "RequestID",
},
},
{
name: "happy path: request id was not found",
args: args{
ctx: context.Background(),
},
expected: expected{
res: "",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Act
actual := RequestID(tt.args.ctx)

// Assert
assert.Equal(t, tt.expected.res, actual)
})
}
}

func TestWithRequestID(t *testing.T) {
type args struct {
id string
}

type expected struct {
res context.Context //nolint:containedctx
}

tests := []struct {
name string
args args
expected expected
}{
{
name: "happy path",
args: args{
id: "RequestID",
},
expected: expected{
res: context.WithValue(context.Background(), contextKeyRequestID, "RequestID"),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Arrange
ctx := context.Background()

// Act
actual := WithRequestID(ctx, tt.args.id)

// Assert
assert.Equal(t, tt.expected.res, actual)
})
}
}
8 changes: 8 additions & 0 deletions internal/syncup/interface/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (
"fmt"
"sync"

"github.com/Aton-Kish/syncup/internal/syncup"
"github.com/Aton-Kish/syncup/internal/syncup/domain/model"
"github.com/Aton-Kish/syncup/internal/syncup/domain/repository"
"github.com/google/uuid"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -80,6 +82,12 @@ func (c *rootCommand) command() *cobra.Command {
Use: "syncup",
Short: "Sync up with AWS AppSync",
Version: fmt.Sprintf("%s, build %s (%s/%s)", c.version.Version, c.version.GitCommit, c.version.OS, c.version.Arch),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
ctx = syncup.WithRequestID(ctx, uuid.NewString())
cmd.SetContext(ctx)
return nil
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer wrap(&err)

Expand Down
52 changes: 51 additions & 1 deletion internal/syncup/interface/infrastructure/function_appsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,31 @@ import (
"context"
"errors"
"fmt"
"time"

"github.com/Aton-Kish/syncup/internal/syncup"
"github.com/Aton-Kish/syncup/internal/syncup/domain/model"
"github.com/Aton-Kish/syncup/internal/syncup/domain/repository"
"github.com/Aton-Kish/syncup/internal/syncup/interface/infrastructure/mapper"
"github.com/aws/aws-sdk-go-v2/aws/retry"
"github.com/aws/aws-sdk-go-v2/service/appsync"
"github.com/aws/aws-sdk-go-v2/service/appsync/types"
"github.com/hashicorp/golang-lru/v2/expirable"
"golang.org/x/sync/singleflight"
)

const (
cacheSizeFunctionRepositoryForAppSync = 0 // unlimited size
cacheTTLFunctionRepositoryForAppSync = time.Duration(1) * time.Minute

cacheKeyPrefixFunctions = "Functions"
)

type functionRepositoryForAppSync struct {
appsyncClient appsyncClient

cache *expirable.LRU[string, []model.Function]
sfg *singleflight.Group
}

var (
Expand All @@ -44,7 +58,10 @@ var (
)

func NewFunctionRepositoryForAppSync() repository.FunctionRepository {
return &functionRepositoryForAppSync{}
return &functionRepositoryForAppSync{
cache: expirable.NewLRU[string, []model.Function](cacheSizeFunctionRepositoryForAppSync, nil, cacheTTLFunctionRepositoryForAppSync),
sfg: &singleflight.Group{},
}
}

func (r *functionRepositoryForAppSync) ActivateAWS(ctx context.Context, optFns ...func(o *model.AWSOptions)) (err error) {
Expand All @@ -63,6 +80,36 @@ func (r *functionRepositoryForAppSync) ActivateAWS(ctx context.Context, optFns .
func (r *functionRepositoryForAppSync) List(ctx context.Context, apiID string) (res []model.Function, err error) {
defer wrap(&err)

cacheKey := fmt.Sprintf("%s#%s", cacheKeyPrefixFunctions, syncup.RequestID(ctx))
v, err, _ := r.sfg.Do(cacheKey, func() (any, error) {
if fns, ok := r.cache.Get(cacheKey); ok {
return fns, nil
}

fns, err := r.list(ctx, apiID)
if err != nil {
return nil, err
}

r.cache.Add(cacheKey, fns)

return fns, nil
})
if err != nil {
return nil, err
}

fns, ok := v.([]model.Function)
if !ok {
return nil, fmt.Errorf("%w: expected type []model.Function but got %T", model.ErrInvalidValue, v)
}

return fns, nil
}

func (r *functionRepositoryForAppSync) list(ctx context.Context, apiID string) (res []model.Function, err error) {
defer wrap(&err)

fns := make([]model.Function, 0)

var token *string
Expand Down Expand Up @@ -150,6 +197,9 @@ func (r *functionRepositoryForAppSync) Save(ctx context.Context, apiID string, f
return nil, err
}

cacheKey := fmt.Sprintf("%s#%s", cacheKeyPrefixFunctions, syncup.RequestID(ctx))
r.cache.Remove(cacheKey)

return fn, nil
}

Expand Down
14 changes: 14 additions & 0 deletions internal/syncup/interface/infrastructure/function_appsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ import (
"github.com/aws/aws-sdk-go-v2/service/appsync/types"
smithymiddleware "github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
"github.com/hashicorp/golang-lru/v2/expirable"
"github.com/stretchr/testify/assert"
"golang.org/x/sync/singleflight"
)

func Test_functionRepositoryForAppSync_List(t *testing.T) {
Expand Down Expand Up @@ -244,6 +246,9 @@ func Test_functionRepositoryForAppSync_List(t *testing.T) {

r := &functionRepositoryForAppSync{
appsyncClient: mockAppSyncClient,

cache: expirable.NewLRU[string, []model.Function](cacheSizeFunctionRepositoryForAppSync, nil, cacheTTLFunctionRepositoryForAppSync),
sfg: &singleflight.Group{},
}

// Act
Expand Down Expand Up @@ -433,6 +438,9 @@ func Test_functionRepositoryForAppSync_Get(t *testing.T) {

r := &functionRepositoryForAppSync{
appsyncClient: mockAppSyncClient,

cache: expirable.NewLRU[string, []model.Function](cacheSizeFunctionRepositoryForAppSync, nil, cacheTTLFunctionRepositoryForAppSync),
sfg: &singleflight.Group{},
}

// Act
Expand Down Expand Up @@ -1021,6 +1029,9 @@ func Test_functionRepositoryForAppSync_Save(t *testing.T) {

r := &functionRepositoryForAppSync{
appsyncClient: mockAppSyncClient,

cache: expirable.NewLRU[string, []model.Function](cacheSizeFunctionRepositoryForAppSync, nil, cacheTTLFunctionRepositoryForAppSync),
sfg: &singleflight.Group{},
}

// Act
Expand Down Expand Up @@ -1387,6 +1398,9 @@ func Test_functionRepositoryForAppSync_Delete(t *testing.T) {

r := &functionRepositoryForAppSync{
appsyncClient: mockAppSyncClient,

cache: expirable.NewLRU[string, []model.Function](cacheSizeFunctionRepositoryForAppSync, nil, cacheTTLFunctionRepositoryForAppSync),
sfg: &singleflight.Group{},
}

// Act
Expand Down
Loading