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

sample-apps/ec2-spot - does ec2Client need to be static? #360

Open
DmitryLukyanov opened this issue May 17, 2022 · 4 comments
Open

sample-apps/ec2-spot - does ec2Client need to be static? #360

DmitryLukyanov opened this issue May 17, 2022 · 4 comments

Comments

@DmitryLukyanov
Copy link

DmitryLukyanov commented May 17, 2022

I'm looking at this sample app and have a question why this sample uses a static modifier for ec2Client? As far as I understand, the intention is to use this client as singleton, but the same behavior will be reached if ec2Client will be non static, won't it? Like:

    private AmazonEC2Client ec2Client;

    public Function() {
      AWSSDKHandler.RegisterXRayForAllServices();
      ec2Client = new AmazonEC2Client();
    }
@jyemin
Copy link

jyemin commented Aug 4, 2022

I'd like to know the answer to this as well.

@orozcoadrian
Copy link
Contributor

not the author of that sample code and not affiliated with this repo, nor with aws, but here's something from the docs regarding static members (using java but I think it's also applicable to dotnet):

In the following example, the logger, serializer, and AWS SDK client are created when the function serves its first event. Subsequent events served by the same function instance are much faster because those resources already exist.

// Handler value: example.Handler
public class Handler implements RequestHandler<SQSEvent, String>{
  private static final Logger logger = LoggerFactory.getLogger(Handler.class);
  private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
  private static final LambdaAsyncClient lambdaClient = LambdaAsyncClient.create();
  ...
  @Override
  public String handleRequest(SQSEvent event, Context context)
  {
    String response = new String();
    // call Lambda API
    logger.info("Getting account settings");
    CompletableFuture<GetAccountSettingsResponse> accountSettings =
        lambdaClient.getAccountSettings(GetAccountSettingsRequest.builder().build());
    // log execution details
    logger.info("ENVIRONMENT VARIABLES: " + gson.toJson(System.getenv()));
    ...

https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html

So it seems like the real value of using a static member comes from initializing it outside of the constructor (so that it only happens once across multiple lambda invocations). That is not the case in the c# code in the sample app. To fully achieve the goal of performance optimization, the member should be static but also the assignment needs to happen outside the constructor.

Otoh, I don't see the same in the c# docs: https://docs.aws.amazon.com/lambda/latest/dg/csharp-handler.html Maybe that static member is a leftover from converting sample java code to c#? 🤷

@orozcoadrian
Copy link
Contributor

hang on, it's a static constructor: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors
Then it makes sense. The ec2Client member will be initialized only once. Performance optimization achieved.

@DmitryLukyanov
Copy link
Author

So it seems like the real value of using a static member comes from initializing it outside of the constructor (so that it only happens once across multiple lambda invocations). That is not the case in the c# code in the sample app.

As far as I can see, non static ctor is called only once too (at least in all tests I did)

@mwunderl mwunderl changed the title Dotnet example: is it required to have ec2Client static in ec2-spot sample app? sample-apps/ec2-spot - does ec2Client need to be static? Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants