-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e4f80f
commit fbb8551
Showing
4 changed files
with
79 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/strideynet/bsky-furry-feed/bluesky" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type cachedBlueSkyClient struct { | ||
renewalThreshold time.Duration | ||
creds *bluesky.Credentials | ||
|
||
mu sync.Mutex | ||
|
||
fetchedAt time.Time | ||
cachedClient *bluesky.Client | ||
} | ||
|
||
func (cb *cachedBlueSkyClient) Get(ctx context.Context) (c *bluesky.Client, err error) { | ||
ctx, span := tracer.Start(ctx, "cachedBlueSkyClient.Get") | ||
defer func() { | ||
endSpan(span, err) | ||
}() | ||
cb.mu.Lock() | ||
defer cb.mu.Unlock() | ||
|
||
// If client was created within the last five minutes, return that client. | ||
if time.Since(cb.fetchedAt) < cb.renewalThreshold { | ||
if cb.cachedClient != nil { | ||
span.AddEvent("client created within last five minutes, returning this client.") | ||
return cb.cachedClient, nil | ||
} | ||
} | ||
span.AddEvent("no client created within last five minutes, will attempt to create new client.") | ||
|
||
// Otherwise return a new client. | ||
c, err = bluesky.ClientFromCredentials(ctx, cb.creds) | ||
if err != nil { | ||
return nil, fmt.Errorf("fetching token from credentials: %w", err) | ||
} | ||
cb.cachedClient = c | ||
cb.fetchedAt = time.Now() | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fbb8551
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
bsky-furry-feed-admin – ./web/admin
bsky-furry-feed-admin-strideynet.vercel.app
admin.furryli.st
bsky-furry-feed-admin.vercel.app
bsky-furry-feed-admin-git-main-strideynet.vercel.app