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

[Client] Add support for zig client library #160

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

NishantJoshi00
Copy link
Member

Description

Adding support for zig client for superposition. The steps to work with the client are mentioned in the README.md file in the client.

@NishantJoshi00 NishantJoshi00 requested a review from a team as a code owner July 14, 2024 07:51
@Datron
Copy link
Collaborator

Datron commented Jul 15, 2024

@NishantJoshi00 can we remove the merge commit? It breaks semantic guidelines here. Please rebase if possible

@NishantJoshi00
Copy link
Member Author

@NishantJoshi00 can we remove the merge commit? It breaks semantic guidelines here. Please rebase if possible

I have done that 👍 .
But, I am curious. What merge policy do you use?

@Datron
Copy link
Collaborator

Datron commented Jul 15, 2024

rebase and merge/squash and merge, depending on the use-case

@NishantJoshi00
Copy link
Member Author

squash and merge

In this case, my commit messages shouldn't matter, only the PR title would be of significance, right?

@Datron
Copy link
Collaborator

Datron commented Jul 15, 2024

@NishantJoshi00 yup

@Datron
Copy link
Collaborator

Datron commented Jul 21, 2024

@juspay/sdk-backend can we start reviewing this?

//
// It is redundant to include "zig" in this name because it is already
// within the Zig package namespace.
.name = "pauli",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NishantJoshi00 , what is pauli here, the package/lib name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a generic name zig-cac-client? We don't want to get into naming clients for other languages too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I started of with this, will change that

Copy link
Collaborator

@Datron Datron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NishantJoshi00 few changes:

  • We have to avoid the clone of superposition into the client
  • link to the binaries in the parent folder
  • binaries should be compiled for the target platform
  • can you specify the min version of zig
  • can you add a zig and zls package in flake.nix
  • Avoid relative paths

Let us know if we need to make any changes to make this work.

test: test-cac test-expt

test-cac:
zig test -I../../headers -lc src/cac.zig ../../target/debug/libcac_client.so
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NishantJoshi00 few things here:

  • Can the linking binary be an absolute path
  • This should be different based on the OS compiled for

If any support is needed from Superposition, we can add this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer required, I was using this initially to test it. Will remove this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the libs and headers are figured out in build by build.zig


### Pre-requisites

- As this client leverages `build.zig.zon` file for managing dependencies. Your zig version should atleast satisfy the minimum version mentioned in the `build.zig.zon` file.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NishantJoshi00 can we include some usage examples too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part of the example/ folder. I will add a README in that folder too

const repository = b.addSystemCommand(&.{
"sh", "-c", std.fmt.comptimePrint(
\\if [ ! -d superposition ]; then
\\ git clone https://github.com/juspay/superposition.git
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we avoid this clone? Can you link to the libraries in target directory in the parent folder?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think when using this client as a dependency, we need some mechanism to be able to link the library, header files. By, doing this I don't need to worry about if you have the .so files present in any of your lib paths (same goes to header files). But, it does incur the cost of compilation.

I think one possible fix might be to only build the client crates, while not touching the others.

What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you can only build the clients rather than the whole project

/// This is a blocking call, make sure to run this in a separate thread. The polling frequency is set in the [`StartupConfig`] struct using `update_frequency`.
///
pub fn poll(self: *const Client) void {
expt_bindings.expt_start_polling_update(self.config.tenant.ptr);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NishantJoshi00 Can we provide an example in docs of spawning a thread?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here, I faced an issue. This FFI function didn't block. I wasn't able to debug it. But, even without using this I was still able to get the default configs

Comment on lines +188 to +189
// BUG: The current implementation of `cac_free_string` accepts `char *` but a significant other functions defined return `const char *`. This makes it impossible to free the memory allocated by the CAC client.
// cac_bindings.cac_free_string(s_ptr.ptr);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it impossible? Should we change something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's translate it to rust concepts and understand this.
most of the functions imagine are returning &String while this function is accepting &mut String. Will this work in rust?

I am looking at this with a similar lens

clients/zig/build.zig.zon Outdated Show resolved Hide resolved
clients/zig/example/src/main.zig Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants