Roblox / rs-consul

This crate provides access to a set of strongly typed apis to interact with consul (https://www.consul.io/)
MIT License
35 stars 23 forks source link

Introductory PR for lots of changes #31

Open rrichardson opened 1 year ago

rrichardson commented 1 year ago

What problem are we solving?

I have been building some systems against this library for the last couple of days and made a bunch of changes to my fork. These changes include additional functions, but also updates the rs-consul code to make it more "idiomatic Rust".

A couple of these changes do break backwards compatibility a bit, but not a lot. This would probably warrant a new minor version.

This is a lot of changes, so it might need to be broken up into a few separate PRs, which I'm willing to do. I'd just like to put this out there to get some feedback first.

Here is an incomplete list of changes:

Introduced a Base64Vec type

This is mostly used under the hood to simplify sending Vec<u8> to and from Consul. It transparently base64 encodes and decodes with a custom Serializer and Deserializer.
ReadKeyRequest is generic over T (see below) but defaults to Base64Vec.

Changed all KV related operations to be generic over type T

Previously one had to write Vec<u8> and read a String, which I found to be a little awkward. In all cases, T must implement Serialize or Deserialize + Debug + Default and in some cases (get_lock) it requres Clone as well.
If all you're doing is storing and retrieving String this interface should be a bit simpler, because you don't have to convert to and from Vec

Methods updated to be Generic over T:

Structs updated to be generic over T:

Added Transaction operations

You can now send batches of operations to be executed with (some?) isolation using Consuls txn API. (see https://developer.hashicorp.com/consul/api-docs/txn)

This includes new a new method: create_or_update_all which takes a Vec of TransactionOp. Note that I didn't add support for all request types into TransactionOp, only KV, which is all that I needed.

Note that TransactionOp is not generic over T, because A Vec<TransactionOp<T>> would allow only 1 type of value to be written. So it takes a Base64Vec as a value instead.

Tests are idempotent, for the most part

The tests assumed that the database was pristine, so some tests would fail on multiple runs. This is no longer the case. They now clean up before their operations.

Exposed create_session and made a get_lock_inner

For those that don't want to use a LockGuard with drop semantics, because they have their own system for managing locks, I've exposed the inner workings of get_lock

Made everything more self-consistent and idiomatic Rust

Checks

Please check these off before promoting the pull request to non-draft status.

kushudai commented 1 year ago

Hi @rrichardson Thank you for all this work! I see you maintain a pretty elaborate fork of this library.

We would love to integrate your changes but the scope of them is quite large and a bit risky to pull off all in one go (since we use this library internally).

If you could sign the CLA, I would be happy to try to pull these changes in piecemeal.

github-actions[bot] commented 1 year ago

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

cholcombe973 commented 1 year ago

I have read the CLA Document and I hereby sign the CLA

cholcombe973 commented 1 year ago

I also linked my email so that should be fixed as well

rrichardson commented 1 year ago

I have read the CLA Document and I hereby sign the CLA