Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS - Add Time command #1114

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Node: Added ZREMRANGEBYRANK command ([#924](https://github.com/aws/glide-for-redis/pull/924))
* Node: Added Xadd, Xtrim commands. ([#1057](https://github.com/aws/glide-for-redis/pull/1057))
* Python: Added json module and JSON.SET JSON.GET commands ([#1056](https://github.com/aws/glide-for-redis/pull/1056))
* Node: Added Time command. ([#1114](https://github.com/aws/glide-for-redis/pull/1114))

#### Features

Expand Down
1 change: 1 addition & 0 deletions glide-core/src/protobuf/redis_request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ enum RequestType {
JsonSet = 88;
JsonGet = 89;
ZRemRangeByScore = 90;
Time = 91;
}

message Command {
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/socket_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::JsonSet => Some(cmd("JSON.SET")),
RequestType::JsonGet => Some(cmd("JSON.GET")),
RequestType::ZRemRangeByScore => Some(cmd("ZREMRANGEBYSCORE")),
RequestType::Time => Some(cmd("TIME")),
}
}

Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,3 +1041,10 @@ export function createXtrim(
addTrimOptions(options, args);
return createCommand(RequestType.XTrim, args);
}

/**
* @internal
*/
export function createTime(): redis_request.Command {
return createCommand(RequestType.Time, []);
}
12 changes: 12 additions & 0 deletions node/src/RedisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
createInfo,
createPing,
createSelect,
createTime,
} from "./Commands";
import { connection_request } from "./ProtobufMessage";
import { Transaction } from "./Transaction";
Expand Down Expand Up @@ -213,4 +214,15 @@ export class RedisClient extends BaseClient {
public configSet(parameters: Record<string, string>): Promise<"OK"> {
return this.createWritePromise(createConfigSet(parameters));
}

/** Returns the server time
* See https://redis.io/commands/time/ for details.
*
* @returns - The current server time as a two items `array`:
* A Unix timestamp and the amount of microseconds already elapsed in the current second.
* The returned `array` is in a [Unix timestamp, Microseconds already elapsed] format.
*/
public time(): Promise<[string, string]> {
return this.createWritePromise(createTime());
}
}
15 changes: 15 additions & 0 deletions node/src/RedisClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
createCustomCommand,
createInfo,
createPing,
createTime,
} from "./Commands";
import { RequestError } from "./Errors";
import { connection_request, redis_request } from "./ProtobufMessage";
Expand Down Expand Up @@ -386,4 +387,18 @@ export class RedisClusterClient extends BaseClient {
toProtobufRoute(route),
);
}

/** Returns the server time.
* See https://redis.io/commands/time/ for details.
*
* @param route - The command will be routed to a random node, unless `route` is provided, in which
* case the client will route the command to the nodes defined by `route`.
*
* @returns - The current server time as a two items `array`:
* A Unix timestamp and the amount of microseconds already elapsed in the current second.
* The returned `array` is in a [Unix timestamp, Microseconds already elapsed] format.
*/
public time(route?: Routes): Promise<[string, string]> {
return this.createWritePromise(createTime(), toProtobufRoute(route));
}
}
12 changes: 12 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
createSismember,
createStrlen,
createTTL,
createTime,
createType,
createUnlink,
createXadd,
Expand Down Expand Up @@ -1081,6 +1082,17 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
public xtrim(key: string, options: StreamTrimOptions): T {
return this.addAndReturn(createXtrim(key, options));
}

/** Returns the server time.
* See https://redis.io/commands/time/ for details.
*
* @returns - The current server time as a two items `array`:
* A Unix timestamp and the amount of microseconds already elapsed in the current second.
* The returned `array` is in a [Unix timestamp, Microseconds already elapsed] format.
*/
public time(): T {
return this.addAndReturn(createTime());
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,23 @@ export function runBaseTests<Context>(config: {
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"time test_%p",
async (protocol) => {
await runTest(async (client: BaseClient) => {
// Take the time now, convert to 10 digits and subtract 1 second
const now = Math.floor(new Date().getTime() / 1000 - 1);
const result = await client.time();
expect(result?.length).toEqual(2);
expect(Number(result?.at(0))).toBeGreaterThan(now);
// Test its not more than 1 second
expect(Number(result?.at(1))).toBeLessThan(1000000);
client.close();
}, protocol);
},
config.timeout,
);
}

export function runCommonTests<Context>(config: {
Expand Down
Loading