Skip to content

Commit

Permalink
Merge branch 'main' into java/yuryf-modules-ci
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand authored Oct 7, 2024
2 parents cce10e4 + 6d0cffe commit 36fa567
Show file tree
Hide file tree
Showing 47 changed files with 2,176 additions and 532 deletions.
3 changes: 2 additions & 1 deletion csharp/lib/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public enum Level
Warn = 1,
Info = 2,
Debug = 3,
Trace = 4
Trace = 4,
Off = 5,
}

/*
Expand Down
3 changes: 3 additions & 0 deletions csharp/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum Level {
Info = 2,
Debug = 3,
Trace = 4,
Off = 5,
}

pub struct Client {
Expand Down Expand Up @@ -146,6 +147,7 @@ impl From<logger_core::Level> for Level {
logger_core::Level::Info => Level::Info,
logger_core::Level::Debug => Level::Debug,
logger_core::Level::Trace => Level::Trace,
logger_core::Level::Off => Level::Off,
}
}
}
Expand All @@ -158,6 +160,7 @@ impl From<Level> for logger_core::Level {
Level::Info => logger_core::Level::Info,
Level::Debug => logger_core::Level::Debug,
Level::Trace => logger_core::Level::Trace,
Level::Off => logger_core::Level::Off,
}
}
}
Expand Down
146 changes: 146 additions & 0 deletions examples/node/cluster_example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
*/

import {
ClosingError,
ConnectionError,
GlideClusterClient,
InfoOptions,
Logger,
RequestError,
TimeoutError,
} from "@valkey/valkey-glide";

/**
* Creates and returns a GlideClusterClient instance.
* This function initializes a GlideClusterClient with the provided list of nodes.
* The nodesList may contain the address of one or more cluster nodes, and the
* client will automatically discover all nodes in the cluster.
* @param nodesList A list of tuples where each tuple contains a host (str) and port (int). Defaults to [("localhost", 6379)].
* @returns An instance of GlideClusterClient connected to the discovered nodes.
*/
async function createClient(nodesList = [{ host: "localhost", port: 6379 }]) {
const addresses = nodesList.map((node) => ({
host: node.host,
port: node.port,
}));

// Check `GlideClusterClientConfiguration` for additional options.
return await GlideClusterClient.createClient({
addresses: addresses,
// if the server uses TLS, you'll need to enable it. Otherwise the connection attempt will time out silently.
// useTLS: true,
});
}

/**
* Executes the main logic of the application, performing basic operations
* such as SET, GET, PING, and INFO REPLICATION using the provided GlideClusterClient.
* @param client An instance of GlideClusterClient.
*/
async function appLogic(client: GlideClusterClient) {
// Send SET and GET
const setResponse = await client.set("foo", "bar");
Logger.log("info", "app", `Set response is: ${setResponse}`);

const getResponse = await client.get("foo");
Logger.log("info", "app", `Get response is: ${getResponse?.toString()}`);

// Send PING to all primaries (according to Redis's PING request_policy)
const pong = await client.ping();
Logger.log("info", "app", `PING response: ${pong}`);

// Send INFO REPLICATION to all nodes
const infoReplResps = await client.info({
sections: [InfoOptions.Replication],
});
const infoReplicationValues = Object.values(infoReplResps);

Logger.log(
"info",
"app",
`INFO REPLICATION responses from all nodes are:\n`,
);

infoReplicationValues.forEach((item) =>
Logger.log("info", "glide", item as string),
);
}

/**
* Executes the application logic with exception handling.
*/
async function execAppLogic() {
// Loop through with exception handling
while (true) {
let client;

try {
client = await createClient();
return await appLogic(client);
} catch (error) {
switch (true) {
case error instanceof ClosingError:
// If the error message contains "NOAUTH", raise the exception
// because it indicates a critical authentication issue.
if ((error as ClosingError).message.includes("NOAUTH")) {
Logger.log(
"error",
"glide",
`Authentication error encountered: ${error}`,
);
} else {
Logger.log(
"warn",
"glide",
`Client has closed and needs to be re-created: ${error}`,
);
}

throw error;
case error instanceof TimeoutError:
// A request timed out. You may choose to retry the execution based on your application's logic
Logger.log("error", "glide", `Timeout error: ${error}`);
throw error;
case error instanceof ConnectionError:
// The client wasn't able to reestablish the connection within the given retries
Logger.log("error", "glide", `Connection error: ${error}`);
throw error;
case error instanceof RequestError:
// Other error reported during a request, such as a server response error
Logger.log(
"error",
"glide",
`RequestError encountered: ${error}`,
);
throw error;
default:
Logger.log("error", "glide", `Unexpected error: ${error}`);
throw error;
}
} finally {
try {
if (client) {
await client.close();
}
} catch (error) {
Logger.log(
"warn",
"glide",
`Error encountered while closing the client: ${error}`,
);
}
}
}
}

function main() {
// In this example, we will utilize the client's logger for all log messages
Logger.setLoggerConfig("info");
// Optional - set the logger to write to a file
// Logger.setLoggerConfig("info", fileName);
execAppLogic();
}

main();
130 changes: 130 additions & 0 deletions examples/node/standalone_example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
*/

import {
ClosingError,
ConnectionError,
GlideClient,
Logger,
RequestError,
TimeoutError,
} from "@valkey/valkey-glide";

/**
* Creates and returns a GlideClient instance.
* This function initializes a GlideClient with the provided list of nodes.
* The nodes_list may contain either only primary node or a mix of primary
* and replica nodes. The GlideClient use these nodes to connect to
* the Standalone setup servers.
* @param nodesList A list of tuples where each tuple contains a host (str) and port (int). Defaults to [("localhost", 6379)].
* @returns An instance of GlideClient connected to the discovered nodes.
*/
async function createClient(nodesList = [{ host: "localhost", port: 6379 }]) {
const addresses = nodesList.map((node) => ({
host: node.host,
port: node.port,
}));

// Check `GlideClientConfiguration` for additional options.
return await GlideClient.createClient({
addresses: addresses,
// if the server uses TLS, you'll need to enable it. Otherwise the connection attempt will time out silently.
// useTLS: true,
});
}

/**
* Executes the main logic of the application, performing basic operations
* such as SET, GET, PING, and INFO REPLICATION using the provided GlideClient.
* @param client An instance of GlideClient.
*/
async function appLogic(client: GlideClient) {
// Send SET and GET
const setResponse = await client.set("foo", "bar");
Logger.log("info", "app", `Set response is: ${setResponse}`);

const getResponse = await client.get("foo");
Logger.log("info", "app", `Get response is: ${getResponse?.toString()}`);

// Send PING to primary
const pong = await client.ping();
Logger.log("info", "app", `PING response: ${pong}`);
}

/**
* Executes the application logic with exception handling.
*/
async function execAppLogic() {
// Loop through with exception handling
while (true) {
let client;

try {
client = await createClient();
return await appLogic(client);
} catch (error) {
switch (true) {
case error instanceof ClosingError:
// If the error message contains "NOAUTH", raise the exception
// because it indicates a critical authentication issue.
if ((error as ClosingError).message.includes("NOAUTH")) {
Logger.log(
"error",
"glide",
`Authentication error encountered: ${error}`,
);
} else {
Logger.log(
"warn",
"glide",
`Client has closed and needs to be re-created: ${error}`,
);
}

throw error;
case error instanceof TimeoutError:
// A request timed out. You may choose to retry the execution based on your application's logic
Logger.log("error", "glide", `Timeout error: ${error}`);
throw error;
case error instanceof ConnectionError:
// The client wasn't able to reestablish the connection within the given retries
Logger.log("error", "glide", `Connection error: ${error}`);
throw error;
case error instanceof RequestError:
// Other error reported during a request, such as a server response error
Logger.log(
"error",
"glide",
`RequestError encountered: ${error}`,
);
throw error;
default:
Logger.log("error", "glide", `Unexpected error: ${error}`);
throw error;
}
} finally {
try {
if (client) {
await client.close();
}
} catch (error) {
Logger.log(
"warn",
"glide",
`Error encountered while closing the client: ${error}`,
);
}
}
}
}

function main() {
// In this example, we will utilize the client's logger for all log messages
Logger.setLoggerConfig("info");
// Optional - set the logger to write to a file
// Logger.setLoggerConfig("info", fileName);
execAppLogic();
}

main();
13 changes: 7 additions & 6 deletions examples/python/cluster_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ async def exec_app_logic():
"glide",
f"Authentication error encountered: {e}",
)
raise e
Logger.log(
LogLevel.WARN,
"glide",
f"Client has closed and needs to be re-created: {e}",
)
else:
Logger.log(
LogLevel.WARN,
"glide",
f"Client has closed and needs to be re-created: {e}",
)
raise e
except TimeoutError as e:
# A request timed out. You may choose to retry the execution based on your application's logic
Logger.log(LogLevel.ERROR, "glide", f"TimeoutError encountered: {e}")
Expand Down
12 changes: 6 additions & 6 deletions glide-core/THIRD_PARTY_LICENSES_RUST
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ the following restrictions:

----

Package: async-trait:0.1.82
Package: async-trait:0.1.83

The following copyrights and licenses were found in the source code of this package:

Expand Down Expand Up @@ -6376,7 +6376,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

----

Package: flate2:1.0.33
Package: flate2:1.0.34

The following copyrights and licenses were found in the source code of this package:

Expand Down Expand Up @@ -11928,7 +11928,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

----

Package: libc:0.2.158
Package: libc:0.2.159

The following copyrights and licenses were found in the source code of this package:

Expand Down Expand Up @@ -18826,7 +18826,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----

Package: redox_syscall:0.5.4
Package: redox_syscall:0.5.6

The following copyrights and licenses were found in the source code of this package:

Expand Down Expand Up @@ -19834,7 +19834,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

----

Package: rustls-pki-types:1.8.0
Package: rustls-pki-types:1.9.0

The following copyrights and licenses were found in the source code of this package:

Expand Down Expand Up @@ -22588,7 +22588,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

----

Package: syn:2.0.77
Package: syn:2.0.79

The following copyrights and licenses were found in the source code of this package:

Expand Down
Loading

0 comments on commit 36fa567

Please sign in to comment.