question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: Get a long-lived client object

See original GitHub issue

I have an a singleton object that encapsulates S3 uploads/download as well as marshal/unmarshal for a particular type of object & destination. I would like to have it encapsulate a long-lived client object. The best I can do so far is: (given it creates self.session in __init__.)

I call shutdown in global cleanup from signal handler.

  1. Is there anything wrong with using a client in this way? It seems that getting a client per request when there could be many requests could wasteful as it (may?) do a network operation on creation (or why do we wait for it?).

  2. If this is kosher, could there be a better interface for it?

    @property
    async def client(self) -> S3Client:
        """
        Get a client for S3.

        TODO: is there a better way to get a long-lived client?
        """
        if self._client is None:
            ctx = self.session.create_client(  # type: ignore
                service_name="s3",
                region_name=self.aws_region,
                aws_access_key_id=self.aws_access_key_id,
                aws_secret_access_key=self.aws_secret_access_key,
            )
            self._client = await ctx.__aenter__()
        return self._client

    async def shutdown(self) -> None:
        """
        Shutdown the client.
        """
        if self._client is not None:
            await self._client.close()
        self._client = None

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:26

github_iconTop GitHub Comments

1reaction
NicoAdriancommented, May 9, 2022

@thehesiod lol 😃 thanks for the fixes

1reaction
thehesiodcommented, May 6, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

gRPC Long-lived Streaming - Code The Cloud
Implementing gRPC long-lived streaming - a tool for cloud native applications. Use it to create watch APIs and notifications ...
Read more >
ASP.NET Core - how to create a long lived RabbitMQ ...
Basically I have a basic RMQ producer POC using both the standard .NET Core RMQ client as well as EasyNetQ. All is working...
Read more >
Creating Service Clients - AWS SDK for Java 1.x
Service clients in the SDK are thread-safe and, for best performance, you should treat them as long-lived objects. Each client has its own...
Read more >
Subscriptions - Apollo GraphQL Docs
Unlike queries, subscriptions are long-lasting operations that can change their result over time. They can maintain an active connection to your GraphQL ...
Read more >
Configurable token lifetimes - Microsoft Entra
A token lifetime policy is a type of policy object that contains token lifetime rules. This policy controls how long access, SAML, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found