Skip to content

Commit

Permalink
fix: remove verbose variables; fix: pass struct pointer to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
poppoerika committed Jan 28, 2022
1 parent 5dd3f28 commit 6274d0c
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 80 deletions.
2 changes: 1 addition & 1 deletion internal/grpcmanagers/control_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ControlGrpcManager struct {
Conn *grpc.ClientConn
}

func NewControlGrpcManager(cgmr internalRequests.ControlGrpcManagerRequest) (*ControlGrpcManager, error) {
func NewControlGrpcManager(cgmr *internalRequests.ControlGrpcManagerRequest) (*ControlGrpcManager, error) {
config := &tls.Config{
InsecureSkipVerify: false,
}
Expand Down
2 changes: 1 addition & 1 deletion internal/grpcmanagers/data_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type DataGrpcManager struct {
Conn *grpc.ClientConn
}

func NewDataGrpcManager(dgmr internalRequests.DataGrpcManagerRequest) (*DataGrpcManager, error) {
func NewDataGrpcManager(dgmr *internalRequests.DataGrpcManagerRequest) (*DataGrpcManager, error) {
config := &tls.Config{
InsecureSkipVerify: false,
}
Expand Down
3 changes: 1 addition & 2 deletions internal/interceptor/authorization_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

func AddHeadersInterceptor(authToken string) func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", authToken)
return invoker(ctx, method, req, reply, cc, opts...)
return invoker(metadata.AppendToOutgoingContext(ctx, "authorization", authToken), method, req, reply, cc, opts...)
}
}
6 changes: 2 additions & 4 deletions internal/resolver/end_point_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Endpoints struct {
CacheEndpoint string
}

func Resolve(rr requests.ResolveRequest) (*Endpoints, error) {
func Resolve(rr *requests.ResolveRequest) (*Endpoints, error) {
if rr.EndpointOverride != "" {
return &Endpoints{ControlEndpoint: MOMENTO_CONTROL_ENDPOINT_PREFIX + rr.EndpointOverride, CacheEndpoint: MOMENTO_CACHE_ENDPOINT_PREFIX + rr.EndpointOverride}, nil
}
Expand All @@ -33,9 +33,7 @@ func getEndpointsFromToken(authToken string) (*Endpoints, error) {
return nil, err
}
if claims, ok := token.Claims.(jwt.MapClaims); ok {
ctEndpoint := reflect.ValueOf(claims[CONTROL_ENDPOINT_CLAIM_ID]).String()
cEndpoint := reflect.ValueOf(claims[CACHE_ENDPOINT_CLAIM_ID]).String()
return &Endpoints{ControlEndpoint: ctEndpoint, CacheEndpoint: cEndpoint}, nil
return &Endpoints{ControlEndpoint: reflect.ValueOf(claims[CONTROL_ENDPOINT_CLAIM_ID]).String(), CacheEndpoint: reflect.ValueOf(claims[CACHE_ENDPOINT_CLAIM_ID]).String()}, nil
} else {
return nil, err
}
Expand Down
60 changes: 27 additions & 33 deletions internal/scsmanagers/scs_control_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,51 @@ type ScsControlClient struct {
client pb.ScsControlClient
}

func NewScsControlClient(ccr internalRequests.ControlClientRequest) (*ScsControlClient, error) {
newEndpoint := fmt.Sprint(ccr.Endpoint, ControlPort)
controlGrpcManagerRequest := internalRequests.ControlGrpcManagerRequest{
func NewScsControlClient(ccr *internalRequests.ControlClientRequest) (*ScsControlClient, error) {
cm, err := grpcmanagers.NewControlGrpcManager(&internalRequests.ControlGrpcManagerRequest{
AuthToken: ccr.AuthToken,
Endpoint: newEndpoint,
}
cm, err := grpcmanagers.NewControlGrpcManager(controlGrpcManagerRequest)
Endpoint: fmt.Sprint(ccr.Endpoint, ControlPort),
})
if err != nil {
return nil, err
}
client := pb.NewScsControlClient(cm.Conn)
return &ScsControlClient{grpcManager: cm, client: client}, nil
return &ScsControlClient{grpcManager: cm, client: pb.NewScsControlClient(cm.Conn)}, nil
}

func (cc *ScsControlClient) Close() error {
return cc.grpcManager.Close()
}

func (cc *ScsControlClient) CreateCache(ccr requests.CreateCacheRequest) error {
if utility.IsCacheNameValid(ccr.CacheName) {
request := pb.CreateCacheRequest{CacheName: ccr.CacheName}
ctx, cancel := context.WithTimeout(context.Background(), ControlCtxTimeout)
defer cancel()
_, err := cc.client.CreateCache(ctx, &request)
if err != nil {
return scserrors.GrpcErrorConverter(err)
}
return nil
func (cc *ScsControlClient) CreateCache(ccr *requests.CreateCacheRequest) error {
if !utility.IsCacheNameValid(ccr.CacheName) {
return fmt.Errorf("cache name cannot be empty")
}
ctx, cancel := context.WithTimeout(context.Background(), ControlCtxTimeout)
defer cancel()
_, err := cc.client.CreateCache(ctx, &pb.CreateCacheRequest{CacheName: ccr.CacheName})
if err != nil {
return scserrors.GrpcErrorConverter(err)
}
return fmt.Errorf("cache name cannot be empty")
return nil
}

func (cc *ScsControlClient) DeleteCache(dcr requests.DeleteCacheRequest) error {
if utility.IsCacheNameValid(dcr.CacheName) {
request := pb.DeleteCacheRequest{CacheName: dcr.CacheName}
ctx, cancel := context.WithTimeout(context.Background(), ControlCtxTimeout)
defer cancel()
_, err := cc.client.DeleteCache(ctx, &request)
if err != nil {
return scserrors.GrpcErrorConverter(err)
}
return nil
func (cc *ScsControlClient) DeleteCache(dcr *requests.DeleteCacheRequest) error {
if !utility.IsCacheNameValid(dcr.CacheName) {
return fmt.Errorf("cache name cannot be empty")
}
ctx, cancel := context.WithTimeout(context.Background(), ControlCtxTimeout)
defer cancel()
_, err := cc.client.DeleteCache(ctx, &pb.DeleteCacheRequest{CacheName: dcr.CacheName})
if err != nil {
return scserrors.GrpcErrorConverter(err)
}
return fmt.Errorf("cache name cannot be empty")
return nil
}

func (cc *ScsControlClient) ListCaches(lcr requests.ListCachesRequest) (*responses.ListCachesResponse, error) {
request := pb.ListCachesRequest{NextToken: lcr.NextToken}
func (cc *ScsControlClient) ListCaches(lcr *requests.ListCachesRequest) (*responses.ListCachesResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), ControlCtxTimeout)
defer cancel()
resp, err := cc.client.ListCaches(ctx, &request)
resp, err := cc.client.ListCaches(ctx, &pb.ListCachesRequest{NextToken: lcr.NextToken})
if err != nil {
return nil, scserrors.GrpcErrorConverter(err)
}
Expand Down
28 changes: 10 additions & 18 deletions internal/scsmanagers/scs_data_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,22 @@ type ScsDataClient struct {
defaultTtlSeconds uint32
}

func NewScsDataClient(dcr internalRequests.DataClientRequest) (*ScsDataClient, error) {
newEndpoint := fmt.Sprint(dcr.Endpoint, CachePort)
dataGrpcManagerRequest := internalRequests.DataGrpcManagerRequest{
func NewScsDataClient(dcr *internalRequests.DataClientRequest) (*ScsDataClient, error) {
cm, err := grpcmanagers.NewDataGrpcManager(&internalRequests.DataGrpcManagerRequest{
AuthToken: dcr.AuthToken,
Endpoint: newEndpoint,
}
cm, err := grpcmanagers.NewDataGrpcManager(dataGrpcManagerRequest)
Endpoint: fmt.Sprint(dcr.Endpoint, CachePort),
})
if err != nil {
return nil, err
}
client := pb.NewScsClient(cm.Conn)
return &ScsDataClient{grpcManager: cm, client: client, defaultTtlSeconds: dcr.DefaultTtlSeconds}, nil
return &ScsDataClient{grpcManager: cm, client: pb.NewScsClient(cm.Conn), defaultTtlSeconds: dcr.DefaultTtlSeconds}, nil
}

func (dc *ScsDataClient) Close() error {
return dc.grpcManager.Close()
}

func (dc *ScsDataClient) Set(csr requests.CacheSetRequest) (*responses.SetCacheResponse, error) {
func (dc *ScsDataClient) Set(csr *requests.CacheSetRequest) (*responses.SetCacheResponse, error) {
if !utility.IsCacheNameValid(csr.CacheName) {
return nil, scserrors.InvalidInputError("cache name cannot be empty")
}
Expand All @@ -65,31 +62,26 @@ func (dc *ScsDataClient) Set(csr requests.CacheSetRequest) (*responses.SetCacheR
itemTtlMils = ttlMils

}
request := pb.SetRequest{CacheKey: byteKey, CacheBody: byteValue, TtlMilliseconds: itemTtlMils}
ctx, cancel := context.WithTimeout(context.Background(), CacheCtxTimeout)
defer cancel()
md := createNewMetadata(csr.CacheName)
resp, err := dc.client.Set(metadata.NewOutgoingContext(ctx, md), &request)
resp, err := dc.client.Set(metadata.NewOutgoingContext(ctx, createNewMetadata(csr.CacheName)), &pb.SetRequest{CacheKey: byteKey, CacheBody: byteValue, TtlMilliseconds: itemTtlMils})
if err != nil {
return nil, scserrors.GrpcErrorConverter(err)
}
newResp := responses.NewSetCacheResponse(resp, byteValue)
return newResp, nil
return responses.NewSetCacheResponse(resp, byteValue), nil
}

func (dc *ScsDataClient) Get(cgr requests.CacheGetRequest) (*responses.GetCacheResponse, error) {
func (dc *ScsDataClient) Get(cgr *requests.CacheGetRequest) (*responses.GetCacheResponse, error) {
if !utility.IsCacheNameValid(cgr.CacheName) {
return nil, scserrors.InvalidInputError("cache name cannot be empty")
}
byteKey, err := asBytes(cgr.Key, "Unsupported type for key: ")
if err != nil {
return nil, err
}
request := pb.GetRequest{CacheKey: byteKey}
ctx, cancel := context.WithTimeout(context.Background(), CacheCtxTimeout)
defer cancel()
md := createNewMetadata(cgr.CacheName)
resp, err := dc.client.Get(metadata.NewOutgoingContext(ctx, md), &request)
resp, err := dc.client.Get(metadata.NewOutgoingContext(ctx, createNewMetadata(cgr.CacheName)), &pb.GetRequest{CacheKey: byteKey})
if err != nil {
return nil, scserrors.GrpcErrorConverter(err)
}
Expand Down
5 changes: 2 additions & 3 deletions momento/responses/cache_operation_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ func NewGetCacheResponse(gr *pb.GetResponse) (*GetCacheResponse, error) {
} else if gr.Result == pb.ECacheResult_Miss {
result = MISS
} else {
resultRequest := internalRequests.ConvertEcacheResultRequest{
return nil, utility.ConvertEcacheResult(internalRequests.ConvertEcacheResultRequest{
ECacheResult: gr.Result,
Message: gr.Message,
OpName: "GET",
}
return nil, utility.ConvertEcacheResult(resultRequest)
})
}
return &GetCacheResponse{value: gr.CacheBody, result: result}, nil
}
Expand Down
31 changes: 13 additions & 18 deletions momento/simple_cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,47 @@ type ScsClient struct {
}

func SimpleCacheClient(ccr requests.SimpleCacheClientRequest) (*ScsClient, error) {
resolveRequest := internalRequests.ResolveRequest{
endpoints, err := resolver.Resolve(&internalRequests.ResolveRequest{
AuthToken: ccr.AuthToken,
}
endpoints, err := resolver.Resolve(resolveRequest)
})
if err != nil {
return nil, err
}
controlEndpoint := endpoints.ControlEndpoint
cacheEndpoint := endpoints.CacheEndpoint
controlClientRequest := internalRequests.ControlClientRequest{
controlClient, err := scsmanagers.NewScsControlClient(&internalRequests.ControlClientRequest{
AuthToken: ccr.AuthToken,
Endpoint: controlEndpoint,
}
controlClient, err := scsmanagers.NewScsControlClient(controlClientRequest)
Endpoint: endpoints.ControlEndpoint,
})
if err != nil {
return nil, err
}
dataClientRequest := internalRequests.DataClientRequest{
dataClient, err := scsmanagers.NewScsDataClient(&internalRequests.DataClientRequest{
AuthToken: ccr.AuthToken,
Endpoint: cacheEndpoint,
Endpoint: endpoints.CacheEndpoint,
DefaultTtlSeconds: ccr.DefaultTtlSeconds,
}
dataClient, err := scsmanagers.NewScsDataClient(dataClientRequest)
})
if err != nil {
return nil, err
}
return &ScsClient{authToken: ccr.AuthToken, defaultTtlSeconds: ccr.DefaultTtlSeconds, controlClient: controlClient, dataClient: dataClient}, nil
}

func (scc *ScsClient) CreateCache(ccr requests.CreateCacheRequest) error {
func (scc *ScsClient) CreateCache(ccr *requests.CreateCacheRequest) error {
return scc.controlClient.CreateCache(ccr)
}

func (scc *ScsClient) DeleteCache(dcr requests.DeleteCacheRequest) error {
func (scc *ScsClient) DeleteCache(dcr *requests.DeleteCacheRequest) error {
return scc.controlClient.DeleteCache(dcr)
}

func (scc *ScsClient) ListCaches(lcr requests.ListCachesRequest) (*responses.ListCachesResponse, error) {
func (scc *ScsClient) ListCaches(lcr *requests.ListCachesRequest) (*responses.ListCachesResponse, error) {
return scc.controlClient.ListCaches(lcr)
}

func (scc *ScsClient) Set(csr requests.CacheSetRequest) (*responses.SetCacheResponse, error) {
func (scc *ScsClient) Set(csr *requests.CacheSetRequest) (*responses.SetCacheResponse, error) {
return scc.dataClient.Set(csr)
}

func (scc *ScsClient) Get(cgr requests.CacheGetRequest) (*responses.GetCacheResponse, error) {
func (scc *ScsClient) Get(cgr *requests.CacheGetRequest) (*responses.GetCacheResponse, error) {
return scc.dataClient.Get(cgr)
}

Expand Down

0 comments on commit 6274d0c

Please sign in to comment.