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

update call-profiles to invoke call-profile using lambda Invoke to reduce latency #226

Merged
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
19 changes: 7 additions & 12 deletions call-profiles/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package main

import (
"bytes"
"context"
"fmt"
"identity-service/layer/utils"
"log"
"net/http"
"os"
"sync"
"time"

Expand All @@ -20,18 +17,16 @@ import (
var wg sync.WaitGroup

func callProfile(userId string, sessionId string) {

defer wg.Done()

httpClient := &http.Client{}
jsonBody := []byte(fmt.Sprintf(`{"userId": "%s", "sessionId": "%s"}`, userId, sessionId))
bodyReader := bytes.NewReader(jsonBody)
payload := utils.ProfileLambdaCallPayload{
UserId: userId,
SessionID: sessionId,
}

requestURL := fmt.Sprintf("%s/profile", os.Getenv("baseURL"))
req, _ := http.NewRequest(http.MethodPost, requestURL, bodyReader)
_, err1 := httpClient.Do(req)
if err1 != nil {
fmt.Println("error getting profile data", err1)
err := utils.InvokeProfileLambda(payload)
if err != nil {
log.Println("error calling profile lambda", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion layer/utils/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func getParameter(parameter string) string {
Name: &parameterName,
})
if err != nil {
log.Fatalf(err.Error())
log.Print(err.Error())
}

return *results.Parameter.Value
Expand Down
60 changes: 60 additions & 0 deletions layer/utils/lambdacalls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package utils

import (
"encoding/json"
"fmt"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
)

// ProfileLambdaCallPayload represents the payload to send to the CallProfileFunction
type ProfileLambdaCallPayload struct {
UserId string `json:"userId"`
SessionID string `json:"sessionId"`
}

// APIGatewayProxyRequestWrapper wraps the ProfileLambdaCallPayload inside the Body field
type APIGatewayProxyRequestWrapper struct {
Body string `json:"body"`
}

func InvokeProfileLambda(payload ProfileLambdaCallPayload) error {
session := session.Must(session.NewSession())
client := lambda.New(session)

payloadBytes, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("error marshalling payload: %w", err)
}

// wrap the payload inside the body field
wrapper := APIGatewayProxyRequestWrapper{
Body: string(payloadBytes),
}

// marshal the wrapper back to json
wrapperBytes, err := json.Marshal(wrapper)
if err != nil {
return fmt.Errorf("error marshalling wrapper: %w", err)
}

functionName := os.Getenv("profileFunctionLambdaName")
if functionName == "" {
return fmt.Errorf("profileFunctionLambdaName is not set")
}

input := &lambda.InvokeInput{
FunctionName: aws.String(functionName),
Payload: wrapperBytes,
}

_, err = client.Invoke(input)
if err != nil {
return fmt.Errorf("error invoking lambda: %w", err)
}

return nil
}
15 changes: 8 additions & 7 deletions template.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AWSTemplateFormatVersion: "2010-09-09"
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
identity-service
Expand All @@ -17,9 +17,9 @@ Globals:
baseURL: YourBaseAPIURL
discordBotURL: DiscordBotURL
identityServicePrivateKey: YourIdentityServicePrivateKey
profileFunctionLambdaName: identity-service-CallProfileFunction

Resources:

UtilitiesLayer:
Type: AWS::Serverless::LayerVersion
Properties:
Expand Down Expand Up @@ -58,7 +58,7 @@ Resources:
Handler: bootstrap
Runtime: provided.al2023
Layers:
- !Ref UtilitiesLayer
- !Ref UtilitiesLayer
Architectures:
- x86_64
Tracing: Active
Expand All @@ -83,7 +83,7 @@ Resources:
Handler: bootstrap
Runtime: provided.al2023
Layers:
- !Ref UtilitiesLayer
- !Ref UtilitiesLayer
Architectures:
- x86_64
Tracing: Active
Expand All @@ -103,7 +103,7 @@ Resources:
Handler: bootstrap
Runtime: provided.al2023
Layers:
- !Ref UtilitiesLayer
- !Ref UtilitiesLayer
Architectures:
- x86_64
Tracing: Active
Expand All @@ -124,11 +124,12 @@ Resources:
Metadata:
BuildMethod: go1.x
Properties:
FunctionName: identity-service-CallProfileFunction
CodeUri: call-profile/
Handler: bootstrap
Runtime: provided.al2023
Layers:
- !Ref UtilitiesLayer
- !Ref UtilitiesLayer
Architectures:
- x86_64
Tracing: Active
Expand All @@ -137,4 +138,4 @@ Resources:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /profile
Method: POST
Method: POST
Loading