Giter Site home page Giter Site logo

Comments (2)

frankdejonge avatar frankdejonge commented on July 19, 2024

Hi @jmserra,

I've not run into this specific issue, but I have run into similar ones. My default strategy for these things would be to address this at the boundary level with Flysystem, of course this means such a boundary must be in place for this to have a good effect. Personally I think such cases illustrate nicely how software boundaries help you to deal with accidental complexity. But I'll make it a little more concrete.

For example, if our application was responsible for generating reports of some sort, there'd be a ReportingProcess, which has a Reporter and a ReportsStorage. The ReportStorage would be our boundary to Flysystem. It could look something like:

<?php

class ReportStorage
{
	public function __construct(FilesystemInterface $filesystem)
	{
		$this->filesystem = $filesystem;
	}

	public function storeReport(User $reportIssuer, DateTime $reportedAt, string $report): bool
	{
		$path = "{$reportIssuer->uuid()}-{$reportedAt->format('Y-m-d').txt";
		
		return $this->filesystem->write($path, $report);
	}
}

If there'd be to be some retry mechanism with the $client->authenticate() call, we could easily do that here. We could even do it pre-emptively.

For example, if we'd have a re-authenticator like this:

class ReAuthenticator
{
	public function __construct($client, int $throttle)
	{
		$this->client = $client;
		$this->throttle = $throttle;
		$this->lastCall = time();
	}

	public function reAuthenticate()
	{
		if (time() > ($this->lastCall + $this->throttle)) {
			$this->client->authenticate();
			$this->lastCall = time();
		}
	}
}

and then our boundary could use that:

<?php

class ReportStorage
{
	public function __construct(FilesystemInterface $filesystem, ReAuthenticator $reAuth)
	{
		$this->filesystem = $filesystem;
		$this->reAuth = $reAuth;
	}

	public function storeReport(User $reportIssuer, DateTime $reportedAt, string $report): bool
	{
		$path = "{$reportIssuer->uuid()}-{$reportedAt->format('Y-m-d').txt";
		$this->reAuth->reAuthenticate();
		
		return $this->filesystem->write($path, $report);
	}
}

In this case every time the throttle amount has passed we re-authenticate the client before trying to write with Flysystem.

Hope this help!

from flysystem-rackspace.

jmserra avatar jmserra commented on July 19, 2024

Hi @frankdejonge thanks for your extensive reply, it makes total sense, although doing this kind of implementation implies losing the beautiful abstraction Laravel is doing over Flysystem and its different providers through configuration.

So, looking again into the codebase and object structure i'm thinking that for simple scenarios would be easier accessing the client through the Disk object, what about this:

$client = $disk->getDriver()->getAdapter()->getContainer()->getService()->getClient();

if($client->hasExpired())
     $client->authenticate();

Yeah, not the cleanest thing in the world, and it can easily get broken as soon as some of those layers change a little bit.

This would be more elaborated: (still not ideal)

    function reAuthIfNeeded( FilesystemAdapter $disk )
    {
        $driver = $disk->getDriver();
        if(!is_a($driver, \League\Flysystem\Filesystem::class))
            return;

        $adapter = $driver->getAdapter();
        if(!is_a($adapter, \League\Flysystem\Rackspace\RackspaceAdapter::class))
            return;

        $client = $adapter->getContainer()->getService()->getClient();

        if($client->hasExpired())
        {
            $client->authenticate();
        }
    }

At this point im wondering if that should be done transparently by the driver, as if the token is currently expired, shouldn't be its responsibility to update it without the caller even knowing ?

So, when any method of RackspaceAdapter is called, shouldn't first check if the token got expired ?

Thanks again!

from flysystem-rackspace.

Related Issues (19)

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.