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

Go: Implement connection logic in lib.rs #119

Closed

Conversation

jonathanl-bq
Copy link

No description provided.

go/.gitignore Show resolved Hide resolved
C.command(glideRedisClient.coreClient, C.uintptr_t(resultChannelPtr), requestType, C.uintptr_t(1), &cArgs[0])
resultPayload := <-resultChannel

return resultPayload.value, nil

Choose a reason for hiding this comment

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

Suggested change
return resultPayload.value, nil
return resultPayload.value, resultPayload.errMessage

Copy link
Author

Choose a reason for hiding this comment

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

Will deal with this later when we add command logic

go/glide/lib.h Show resolved Hide resolved
go/glide/lib.h Outdated Show resolved Hide resolved
go/glide/lib.h Show resolved Hide resolved
go/glide/lib.h Outdated Show resolved Hide resolved
go/go.sum Outdated Show resolved Hide resolved
go/glide/lib.h Outdated Show resolved Hide resolved
go/glide/lib.h Outdated Show resolved Hide resolved
go/src/lib.rs Outdated Show resolved Hide resolved
Copy link

@acarbonetto acarbonetto left a comment

Choose a reason for hiding this comment

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

Let's merge this PR with Aaron's glide.go PR.
Add IT tests for connection.
Remove the set/get/ping/info content for a follow-up PR.

go/examples/main.go Show resolved Hide resolved
request.Addresses,
&protobuf.NodeAddress{Host: "localhost", Port: uint32(6379)},
)
err := client.ConnectToRedis(request)

Choose a reason for hiding this comment

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

what happens if a user calls client.set() before they call ConnectToRedis?

go/examples/main.go Show resolved Hide resolved
go/src/lib.rs Outdated Show resolved Hide resolved
go/src/lib.rs Outdated Show resolved Hide resolved
go/src/lib.rs Outdated Show resolved Hide resolved
go/glide/lib.h Outdated
/**
* Failure callback that is called when a Redis command fails.
*/
typedef void (*FailureCallback)(uintptr_t channel_address, const struct RedisErrorFFI *error);
Copy link

@Yury-Fridlyand Yury-Fridlyand Mar 6, 2024

Choose a reason for hiding this comment

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

To ease marshalling it is better to have

Suggested change
typedef void (*FailureCallback)(uintptr_t channel_address, const struct RedisErrorFFI *error);
typedef void (*FailureCallback)(uintptr_t channel_address, ErrorType error_type, const char *message);

Otherwise we have to deal with memory allocations/deallocations for that struct pointer.

Copy link
Author

Choose a reason for hiding this comment

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

I've already implemented the proper functions for freeing this struct and the string within. We can do this, but it goes against what's in the design doc and we'll have to make sure Aaron's work is aligned with this API change as well. I think we should discuss this first with the rest of the team before changing it.

Copy link

@Yury-Fridlyand Yury-Fridlyand Mar 6, 2024

Choose a reason for hiding this comment

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

It is ok to adjust the design doc now

Copy link
Author

Choose a reason for hiding this comment

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

Is it? @aaron-congo are you aware of these changes?

Choose a reason for hiding this comment

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

I wasn't aware. We need the RedisErrorFFI and a mechanism to free it for the connection request, since we are not using a callback and C functions can only return a single result. If we need these things anyways, should we just use it in the failure callback as planned?

go/glide/lib.h Show resolved Hide resolved
go/glide/lib.h Show resolved Hide resolved
go/glide/glide.go Show resolved Hide resolved
go/glide/glide.go Outdated Show resolved Hide resolved

func (glideRedisClient *GlideRedisClient) CloseClient() error {
if glideRedisClient.coreClient == nil {
return fmt.Errorf("Cannot close glide client before it has been created.")

Choose a reason for hiding this comment

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

... or already closed

if glideRedisClient.coreClient == nil {
return fmt.Errorf("Cannot close glide client before it has been created.")
}
C.close_client(glideRedisClient.coreClient)

Choose a reason for hiding this comment

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

Suggested change
C.close_client(glideRedisClient.coreClient)
C.close_client(glideRedisClient.coreClient)
glideRedisClient.coreClient = nil

go/glide/glide.go Show resolved Hide resolved
go/src/lib.rs Outdated
}

/// FFI compatible version of the ErrorType enum defined in protobuf.
// TODO: Update when command errors are implemented

Choose a reason for hiding this comment

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

Add valkey-io#1077 reference for TODO
We will remove this enum after valkey-io#1077

Choose a reason for hiding this comment

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

1077 merged, you can update your code and remove TODO

go/src/lib.rs Outdated Show resolved Hide resolved
go/src/lib.rs Outdated
Comment on lines 67 to 71
let msg = CString::new(err.to_string()).unwrap();
RedisErrorFFI {
message: msg.into_raw(),
error_type: ErrorType::ConnectionError,
}

Choose a reason for hiding this comment

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

Do you want to make a function for this? You repeat it 3 times

go/src/lib.rs Show resolved Hide resolved
go/src/lib.rs Outdated Show resolved Hide resolved
go/examples/main.go Show resolved Hide resolved
go/glide/lib.h Show resolved Hide resolved
go/glide/lib.h Outdated
typedef void (*FailureCallback)(uintptr_t channel_address, const struct RedisErrorFFI *error);

/**
* Creates a new client to the given address. The success callback needs to copy the given string synchronously, since it will be dropped by Rust once the callback returns. All callbacks should be offloaded to separate threads in order not to exhaust the client's thread pool.

Choose a reason for hiding this comment

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

What is the given string you are referring to here? Would it make sense to move the success callback sentence here to be above the SuccessCallback doc instead of the create_client doc?

Copy link
Author

Choose a reason for hiding this comment

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

I'm trying to remember where I copied this doc comment from originally. I think it was the C# implementation? I can still move it if you like.

go/glide/lib.h Outdated Show resolved Hide resolved
@@ -10,6 +10,7 @@ require (

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect

Choose a reason for hiding this comment

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

Do you know why this is added? On my connect branch it is not needed since protobuf is already listed on line 7. If you do go mod tidy does this get removed?

go/src/lib.rs Outdated Show resolved Hide resolved
go/src/lib.rs Outdated Show resolved Hide resolved
go/src/lib.rs Show resolved Hide resolved
go/src/lib.rs Show resolved Hide resolved
Ok(client) => ConnectionResponse {
conn_ptr: Box::into_raw(Box::new(client)) as *const c_void,
error_message: std::ptr::null(),
error_type: RequestErrorType::Unspecified,

Choose a reason for hiding this comment

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

Should we ash Shachar to define NoError enum variant? (later)

Copy link
Author

Choose a reason for hiding this comment

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

I think it would be nice. We'll see what he thinks though.

@Yury-Fridlyand
Copy link

CI is red

go/src/lib.rs Outdated

/// Deallocates a `ConnectionResponse`.
///
/// This function does not free the contained error, which needs to be freed separately using `free_error` afterwards to avoid memory leaks.

Choose a reason for hiding this comment

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

Looks like this doc will have to get updated since it now frees the error

* * `error_msg_ptr` must be able to be safely casted to a valid `CString` via `CString::from_raw`. See the safety documentation of [`std::ffi::CString::from_raw`](https://doc.rust-lang.org/std/ffi/struct.CString.html#method.from_raw).
* * `error_msg_ptr` must not be null.
*/
void free_error(const char *error_msg_ptr);

Choose a reason for hiding this comment

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

Why re-expose?

Copy link
Author

Choose a reason for hiding this comment

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

I assumed that the failure callback would use it for the error it gets for commands. Not relevant for connection logic though, so I guess it can be reverted. Up to you, since you have all the changes in your branch now.

@jonathanl-bq
Copy link
Author

Closing as all relevant changes are integrated into other branches now.

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.

4 participants