Skip to content

Commit

Permalink
ui: cache sqlroles results
Browse files Browse the repository at this point in the history
Previously, the call to get sql roles was constantly
being requested. This commits adds a cache limit, so
it will only get request after the expiration time.

Epic: None
Release note: None
  • Loading branch information
maryliag committed Jan 25, 2023
1 parent 1a4bcbf commit 62d314d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
4 changes: 1 addition & 3 deletions pkg/ui/workspaces/cluster-ui/src/api/userApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export type UserSQLRolesRequestMessage =
export type UserSQLRolesResponseMessage =
cockroach.server.serverpb.UserSQLRolesResponse;

export function getUserSQLRoles(
req: UserSQLRolesRequestMessage,
): Promise<UserSQLRolesResponseMessage> {
export function getUserSQLRoles(): Promise<UserSQLRolesResponseMessage> {
return fetchData(
cockroach.server.serverpb.UserSQLRolesResponse,
`/_status/sqlroles`,
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/workspaces/cluster-ui/src/store/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ export function* sagas(cacheInvalidationPeriod?: number): SagaIterator {
fork(indexStatsSaga),
fork(clusterLocksSaga),
fork(schemaInsightsSaga),
fork(uiConfigSaga),
fork(uiConfigSaga, cacheInvalidationPeriod),
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { merge } from "lodash";
import { DOMAIN_NAME } from "../utils";
import { DOMAIN_NAME, noopReducer } from "../utils";
import { cockroach } from "@cockroachlabs/crdb-protobuf-client";
export type UserSQLRolesRequest = cockroach.server.serverpb.UserSQLRolesRequest;

Expand Down Expand Up @@ -57,15 +57,17 @@ const uiConfigSlice = createSlice({
update: (state, action: PayloadAction<Partial<UIConfigState>>) => {
merge(state, action.payload);
},
refreshUserSQLRoles: (
state,
action?: PayloadAction<UserSQLRolesRequest>,
) => {
receivedUserSQLRoles: (state, action: PayloadAction<string[]>) => {
if (action?.payload) {
const resp = action.payload.toJSON();
state.userSQLRoles = resp["roles"];
state.userSQLRoles = action.payload;
}
},
invalidatedUserSQLRoles: state => {
state.userSQLRoles = [];
},
// Define actions that don't change state
refreshUserSQLRoles: noopReducer,
requestUserSQLRoles: noopReducer,
},
});

Expand Down
46 changes: 36 additions & 10 deletions pkg/ui/workspaces/cluster-ui/src/store/uiConfig/uiConfig.sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,48 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

import { all, call, put, takeLatest } from "redux-saga/effects";
import { actions, UserSQLRolesRequest } from "./uiConfig.reducer";
import { PayloadAction } from "@reduxjs/toolkit";
import { all, call, delay, put, takeLatest } from "redux-saga/effects";
import { actions } from "./uiConfig.reducer";
import { getUserSQLRoles } from "../../api/userApi";
import { CACHE_INVALIDATION_PERIOD, throttleWithReset } from "../utils";
import { rootActions } from "../reducers";
import { cockroach } from "@cockroachlabs/crdb-protobuf-client";

export function* refreshUserSQLRoles(
action: PayloadAction<UserSQLRolesRequest>,
): any {
export function* refreshUserSQLRolesSaga(): any {
yield put(actions.requestUserSQLRoles());
}

export function* requestUserSQLRolesSaga(): any {
try {
const result = yield call(getUserSQLRoles, action?.payload);
yield put(actions.refreshUserSQLRoles(result));
const result: cockroach.server.serverpb.UserSQLRolesResponse = yield call(
getUserSQLRoles,
);
yield put(actions.receivedUserSQLRoles(result.roles));
} catch (e) {
console.warn(e.message);
}
}

export function* uiConfigSaga() {
yield all([takeLatest(actions.refreshUserSQLRoles, refreshUserSQLRoles)]);
export function* receivedUserSQLRolesSaga(delayMs: number): any {
yield delay(delayMs);
yield put(actions.invalidatedUserSQLRoles());
}

export function* uiConfigSaga(
cacheInvalidationPeriod: number = CACHE_INVALIDATION_PERIOD,
): any {
yield all([
throttleWithReset(
cacheInvalidationPeriod,
actions.refreshUserSQLRoles,
[actions.invalidatedUserSQLRoles, rootActions.resetState],
refreshUserSQLRolesSaga,
),
takeLatest(actions.requestUserSQLRoles, requestUserSQLRolesSaga),
takeLatest(
actions.receivedUserSQLRoles,
receivedUserSQLRolesSaga,
cacheInvalidationPeriod,
),
]);
}

0 comments on commit 62d314d

Please sign in to comment.