Code Monkey home page Code Monkey logo

aiob2's People

Contributors

metadaddy avatar void-ux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

aiob2's Issues

Race condition with concurrent operations

If you create an aiob2.Client instance, then use it to fire off multiple concurrent operations, multiple coroutines can be in the section between testing and setting the underlying HTTPClient._authorization_token, resulting in multiple authentication calls.

Example code:

import os
import asyncio

from aiob2 import Client

keys = [
    "some-big-file",
    "some-small-file"
]


async def download_file(client, bucket, key):
    print(f"About to download {bucket}/{key}")
    f = await client.download_file_by_name(
        file_name=key,
        bucket_name=bucket,
    )
    print(f"Downloaded {bucket}/{key}: {f.content_length} bytes")
    return f.content_length


async def main():
    print(f"Downloading objects {keys}")
    async with Client(os.environ["B2_APPLICATION_KEY_ID"], os.environ["B2_APPLICATION_KEY"]) as client:
        sizes = await asyncio.gather(*(download_file(client, "my-bucket", key) for key in keys))
    print(f"Object sizes: {sizes}")


if __name__ == '__main__':
    asyncio.run(main())

Output:

Downloading objects ['some-big-file', 'some-small-file']
About to download my-bucket/some-big-file
2023-05-04 13:17:15 INFO     aiob2.http Authenticating using static application key
About to download my-bucket/some-small-file
2023-05-04 13:17:15 INFO     aiob2.http Authenticating using static application key
Downloaded my-bucket/some-small-file: 6 bytes
Downloaded my-bucket/some-big-file: 15729152 bytes
Object sizes: ['15729152', '6']

The second download doesn't "see" the token retrieved by the first download.

Assuming the goal is to have the HTTPClient authenticate just once, I'm not sure it's possible to do 'lazy' authentication without synchronizing on HTTPClient._authorization_token. It might be easier to just have HTTPClient authenticate in its constructor. In case of authentication failure, that's probably a better (more obvious to the developer) place to throw the error than the first time the client is used.

Expires and cache-control header handling appears to be broken

In DownloadedFile, it's unclear what this code is doing:

        if (expires := headers.get('cache-control')) is not None:
            expires = datetime.datetime.strptime(expires, '%a, %d %b %Y %H:%M:%S %Z')
        else:
            expires = None
        self.expires: Optional[datetime.datetime] = expires

The value of the cache-control header is assigned to expires, which is then parsed as a timestamp.

I think it should be:

        self.cache_control: Optional[str] = headers.get('cache-control')
        if (expires := headers.get('expires')) is not None:
            expires = datetime.datetime.strptime(expires, '%a, %d %b %Y %H:%M:%S %Z')
        else:
            expires = None
        self.expires: Optional[datetime.datetime] = expires

Let me know if my understanding is correct and I'll file the PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.