Skip to content

Commit

Permalink
feat(gate): redis-less mode (#528)
Browse files Browse the repository at this point in the history
### Describe your change

Fallback to `MemoryRegister` and `NoLimiter` if typegate is unable to
connect to Redis.

### Motivation and context

Enable Redis-Less mode.

### Migration notes

<!-- Explain HOW users should update their code when required -->

### Checklist

- [ ] The change come with new or modified tests
- [ ] Hard-to-understand functions have explanatory comments
- [ ] End-user documentation is updated to reflect the change
  • Loading branch information
michael-0acf4 authored Dec 21, 2023
1 parent 2cb977e commit 5233c8e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
5 changes: 5 additions & 0 deletions typegate/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ pub async fn launch_typegate_deno(
main_mod: deno_core::ModuleSpecifier,
import_map_url: Option<String>,
) -> Result<()> {
std::env::var("REDIS_URL")
.ok()
.ok_or_else(|| std::env::set_var("REDIS_URL", "none"))
.ok();

let permissions = deno_runtime::permissions::PermissionsOptions {
allow_run: Some(["hostname"].into_iter().map(str::to_owned).collect()),
allow_sys: Some(vec![]),
Expand Down
4 changes: 3 additions & 1 deletion typegate/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ const schema = {
hostname: z.string(),
redis_url: z
.string()
.url()
.transform((s: string) => {
if (s == "none") {
return new URL("redis://none");
}
const url = new URL(s);
if (url.password === "") {
url.password = Deno.env.get("REDIS_PASSWORD") ?? "";
Expand Down
38 changes: 25 additions & 13 deletions typegate/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import { deferred } from "std/async/deferred.ts";
import { init_native } from "native";

import { ReplicatedRegister } from "./typegate/register.ts";
import { Register, ReplicatedRegister } from "./typegate/register.ts";
import config, { redisConfig } from "./config.ts";
import { Typegate } from "./typegate/mod.ts";
import { RedisRateLimiter } from "./typegate/rate_limiter.ts";
import { RateLimiter, RedisRateLimiter } from "./typegate/rate_limiter.ts";
import { SystemTypegraph } from "./system_typegraphs.ts";
import * as Sentry from "sentry";
import { getLogger } from "./log.ts";
import { init_runtimes } from "./runtimes/mod.ts";
import { MemoryRegister } from "test-utils/memory_register.ts";
import { NoLimiter } from "test-utils/no_limiter.ts";

const logger = getLogger(import.meta);
logger.info(`typegate v${config.version} starting`);
Expand Down Expand Up @@ -46,19 +48,29 @@ init_native();
await init_runtimes();

const deferredTypegate = deferred<Typegate>();
const register = await ReplicatedRegister.init(
deferredTypegate,
redisConfig,
);
const limiter = await RedisRateLimiter.init(redisConfig);
const typegate = new Typegate(register, limiter);
let register: Register | undefined;
let limiter: RateLimiter | undefined;

if (redisConfig.hostname != "none") {
register = await ReplicatedRegister.init(deferredTypegate, redisConfig);
limiter = await RedisRateLimiter.init(redisConfig);
} else {
logger.warning("Entering Redis-less mode");
register = new MemoryRegister();
limiter = new NoLimiter();
}

const typegate = new Typegate(register!, limiter!);

deferredTypegate.resolve(typegate);

const lastSync = await register.historySync().catch((err) => {
logger.error(err);
throw new Error(`failed to load history at boot, aborting: {err.message}`);
});
register.startSync(lastSync);
if (register instanceof ReplicatedRegister) {
const lastSync = await register.historySync().catch((err) => {
logger.error(err);
throw new Error(`failed to load history at boot, aborting: ${err.message}`);
});
register.startSync(lastSync);
}

await SystemTypegraph.loadAll(typegate, !config.packaged);

Expand Down

0 comments on commit 5233c8e

Please sign in to comment.