Skip to content

Commit

Permalink
added types handeling
Browse files Browse the repository at this point in the history
  • Loading branch information
avifenesh committed Jul 12, 2023
1 parent 86aaa7f commit 23d4f5a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 12 deletions.
1 change: 1 addition & 0 deletions babushka-core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() {
.input("src/protobuf/redis_request.proto")
.input("src/protobuf/response.proto")
.input("src/protobuf/connection_request.proto")
.input("src/protobuf/routes.proto")
.customize(customization_options)
.run_from_script();
}
4 changes: 2 additions & 2 deletions babushka-core/src/protobuf/redis_request.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
syntax = "proto3";
package redis_request;

import "protobuf/routes.proto";
enum RequestType {
/// Invalid request type
InvalidRequest = 0;
Expand All @@ -18,12 +18,12 @@ message Command {
message ArgsArray {
repeated string args = 1;
}

RequestType request_type = 1;
oneof args {
ArgsArray args_array = 2;
uint64 args_vec_pointer = 3;
}
routes.Routes route = 4;
}

message Transaction {
Expand Down
36 changes: 36 additions & 0 deletions babushka-core/src/protobuf/routes.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
syntax = "proto3";
package routes;

enum SimpleRoutes {
AllNodes=0;
AllMasters=1;
Random=2;
}

enum SlotIdTypes {
MasterSlotId=0;
ReplicaSlotId=1;
}

message SlotIdRoute {
SlotIdTypes slotIdTypes = 1;
int32 slot_id = 2;
}

enum SlotKeyTypes {
MasterSlotKey=0;
ReplicaSlotKey=1;
}

message SlotKeyRoute {
SlotKeyTypes slotKeyType = 1;
string slot_key = 2;
}

message Routes {
oneof routes {
SimpleRoutes simmpleRoutes = 1;
SlotKeyRoute slotKeyRoute = 2;
SlotIdRoute slotIdRoute = 3;
}
}
1 change: 1 addition & 0 deletions babushka-core/src/socket_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::CustomCommand => Some(Cmd::new()),
RequestType::GetString => Some(cmd("GET")),
RequestType::SetString => Some(cmd("SET")),
RequestType::Ping => Some(cmd("PING")),
_ => None,
}
}
Expand Down
10 changes: 2 additions & 8 deletions node/src/ClusterSocketConnection.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import * as net from "net";
import { connection_request } from "./ProtobufMessage";
import { ConnectionOptions, SocketConnection } from "./SocketConnection";
import { Routes, createPing } from "./Commands";

export type Routes =
| "all_primaries"
| "all_nodes"
| "multi_shard"
| "random"
| "master_slot"
| "replica_slot";
export class ClusterSocketConnection extends SocketConnection {
protected createConnectionRequest(
options: ConnectionOptions
Expand All @@ -35,6 +29,6 @@ export class ClusterSocketConnection extends SocketConnection {
str?: string,
route: Routes = "all_primaries"
): Promise<string> {
return super.ping(str);
return this.createWritePromise(createPing(str, route));
}
}
23 changes: 22 additions & 1 deletion node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ import Long from "long";
import { redis_request } from "./ProtobufMessage";
import RequestType = redis_request.RequestType;

type slotIdTypes = {
type: "master_slot_id" | "replica_slot_id";
id: number;
};

type slotKeyTypes = {
type: "master_slot_key" | "replica_slot_key";
key: string;
};

export type Routes =
| "all_primaries"
| "all_nodes"
| "multi_shard"
| "random"
| slotIdTypes
| slotKeyTypes;

function isLargeCommand(args: string[]) {
let lenSum = 0;
for (const arg of args) {
Expand Down Expand Up @@ -42,7 +60,10 @@ export function createGet(key: string): redis_request.Command {
return createCommand(RequestType.GetString, [key]);
}

export function createPing(str?: string): redis_request.Command {
export function createPing(
str?: string,
route?: Routes
): redis_request.Command {
if (str !== undefined) return createCommand(RequestType.Ping, [str]);
return createCommand(RequestType.Ping, []);
}
Expand Down
2 changes: 1 addition & 1 deletion node/src/SocketConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class SocketConnection {
});
}

private createWritePromise<T>(
protected createWritePromise<T>(
command: redis_request.Command | redis_request.Command[]
): Promise<T> {
return new Promise((resolve, reject) => {
Expand Down

0 comments on commit 23d4f5a

Please sign in to comment.