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

feature: Adding option to use custom Id in request #277

Closed
ivankhm opened this issue Apr 6, 2021 · 4 comments · Fixed by #287
Closed

feature: Adding option to use custom Id in request #277

ivankhm opened this issue Apr 6, 2021 · 4 comments · Fixed by #287
Labels

Comments

@ivankhm
Copy link

ivankhm commented Apr 6, 2021

Hello!
I think there should be a way to use a custom Id for the request. Besides, the current way can be unsafe, for example, if 2 clients simultaneously make the first request - they will have the same id, which can be confusing for a server.

I suggest passing a function into the constructor, that returns the next id, and using it in this method:

const internalID = (++this.lastId).toString();

like this:

const internalID = this.getNextId();

And initiate this.getNextId in the constructor via parameter or default value.

@BelfordZ
Copy link
Member

Thanks @ivankhm for the issue!

This is 100% something that we need to address. If you want to take a stab at a PR, feel free to take a stab. I've created another issue to track the 'custom id' function, as well as a flag to switch from string to number for the id's type, if you want to solve both, that would be sweet!

@zcstarr
Copy link
Member

zcstarr commented Apr 16, 2021

Yeah I can see this being problematic, instantiate two clients to the same server, you have no way to manage the id's for those request, they're both listening for the same server responses. There's no way to coordinate between them.

I could imagine this being as simple as abstracting id generation Logic as you suggest to some interface around

interface RequestIDGenerator {
  nextID(): int | string
}

function requestGenerator() {
   let idCount = 0
   return () => {
      idCount = idCount + 1
      return idCount;
   }
}

Where a simple closure can encapsulate some stateful data.

I think you want the following at the end of the day.

new GeneratedClient( options: {requestOptions: {nextId: ()=>number| string})
new Client(new RequestManager([transports],requestOptions))

Then we can have the behavior remain the same for people that don't care about that management. Additionally people using multiple clients can just lean on the same generator and won't hit that collision point around instantiating multiple clients.

Let me know what you think or if this makes sense to y'all @BelfordZ @ivankhm

@BelfordZ
Copy link
Member

BelfordZ commented Apr 16, 2021

@zcstarr yeah something like that. Though the generatedClient part will be done elsewhere, and the options should reference the classes by name, so more like
new GeneratedClient( options: {requestManager?: RequestManagerOptions )

For this specific part, we would just be adding, as you said, constructor options for RequestManager (along with exported RequestManagerOptions interface). The first constructor option being nextId function, and as highlighted in #279 , an option to use a number instead of a string if using the default nextId functionality

zcstarr added a commit that referenced this issue May 28, 2021
This feature is to support generating requestID in either a string or
numeric format. Additionally this will support multiple clients
operating against the same context.

The goal here is to allow systems like client generator to
pass through this option to other downstream operators

fixes #277 , fixes #279
zcstarr added a commit that referenced this issue May 28, 2021
This feature is to support generating requestID in either a string or
numeric format. Additionally this will support multiple clients
operating against the same context.

The goal here is to allow systems like client generator to
pass through this option to other downstream operators

fixes #277 , fixes #279
zcstarr added a commit that referenced this issue May 28, 2021
This feature is to support generating requestID in either a string or
numeric format. Additionally this will support multiple clients
operating against the same context.

The goal here is to allow systems like client generator to
pass through this option to other downstream operators

fixes #277 , fixes #279
zcstarr added a commit that referenced this issue May 28, 2021
This feature is to support generating requestID in either a string or
numeric format. Additionally this will support multiple clients
operating against the same context.

The goal here is to allow systems like client generator to
pass through this option to other downstream operators

fixes #277 , fixes #279
openrpc-bastion added a commit that referenced this issue May 28, 2021
# [1.7.0](1.6.3...1.7.0) (2021-05-28)

### Features

* support custom requestID generation ([e512bd5](e512bd5)), closes [#277](#277) [#279](#279)
@openrpc-bastion
Copy link
Member

🎉 This issue has been resolved in version 1.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants