Skip to content

Commit 6aeeaec

Browse files
Cache access tokens
1 parent 266c7bc commit 6aeeaec

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

pg_lake_iceberg/src/rest_catalog/rest_catalog.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ char *RestCatalogHost = "http://localhost:8181";
4949
char *RestCatalogClientId = NULL;
5050
char *RestCatalogClientSecret = NULL;
5151

52+
53+
/*
54+
* Should always be accessed via GetRestCatalogAccessToken()
55+
*/
5256
char *RestCatalogAccessToken = NULL;
5357
TimestampTz RestCatalogAccessTokenExpiry = 0;
5458

@@ -454,7 +458,7 @@ PostHeadersWithAuth(void)
454458
List *
455459
DeleteHeadersWithAuth(void)
456460
{
457-
return list_make1(psprintf("Authorization: Bearer %s", RestCatalogFetchAccessToken()));
461+
return list_make1(psprintf("Authorization: Bearer %s", GetRestCatalogAccessToken()));
458462
}
459463

460464

@@ -515,10 +519,13 @@ GetRestCatalogAccessToken(void)
515519
{
516520
/*
517521
* Calling initial time or token will expire in 1 minute, fetch a new
518-
* token
522+
* token.
519523
*/
524+
TimestampTz now = GetCurrentTimestamp();
525+
const int MINUTE_IN_MSECS = 60 * 1000;
526+
520527
if (RestCatalogAccessTokenExpiry == 0 ||
521-
GetCurrentTimestamp() >= RestCatalogAccessTokenExpiry - 60 * 1000000)
528+
!TimestampDifferenceExceeds(now, RestCatalogAccessTokenExpiry, MINUTE_IN_MSECS))
522529
{
523530
if (RestCatalogAccessToken)
524531
pfree(RestCatalogAccessToken);
@@ -528,9 +535,12 @@ GetRestCatalogAccessToken(void)
528535

529536
FetchRestCatalogAccessToken(&accessToken, &expiresIn);
530537

538+
/* Polaris default is 3600 seconds */
539+
Assert(expiresIn > 0);
540+
531541
RestCatalogAccessToken = MemoryContextStrdup(TopMemoryContext, accessToken);
532-
RestCatalogAccessTokenExpiry = GetCurrentTimestamp() + expiresIn * 1000000; /* expiresIn is in
533-
* seconds */
542+
RestCatalogAccessTokenExpiry = now + (int64_t) expiresIn * 1000000; /* expiresIn is in
543+
* seconds */
534544
}
535545

536546
Assert(RestCatalogAccessToken != NULL);
@@ -634,11 +644,27 @@ JsonbGetStringByPath(const char *jsonb_text, int nkeys,...)
634644
}
635645
else
636646
{
637-
if (val->type != jbvString)
638-
ereport(ERROR, (errmsg("leaf \"%s\" is not a string", key)));
647+
if (!(val->type == jbvString || val->type == jbvNumeric))
648+
ereport(ERROR, (errmsg("leaf \"%s\" is not a string or numeric", key)));
639649

640650
va_end(variableArgList);
641-
return pnstrdup(val->val.string.val, val->val.string.len);
651+
652+
if (val->type == jbvString)
653+
return pnstrdup(val->val.string.val, val->val.string.len);
654+
else
655+
{
656+
bool haveError = false;
657+
658+
int valInt = numeric_int4_opt_error(val->val.numeric,
659+
&haveError);
660+
661+
if (haveError)
662+
{
663+
ereport(ERROR, (errmsg("integer out of range")));
664+
}
665+
666+
return psprintf("%d", valInt);
667+
}
642668
}
643669
}
644670

0 commit comments

Comments
 (0)