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

Add context to Signer interface #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kchiranjewee63
Copy link
Collaborator

This PR adds context.Context to the Sign method of the Signer interface to support timeout and cancellation for signing operations, useful for HSM or network-based signing implementations.

Copy link
Collaborator

@richardxgao richardxgao left a comment

Choose a reason for hiding this comment

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

While the context.Background() can be used, it's typically used for initialization since it doesn't have a deadline and is never cancelled. Instead, we should use context.WithDeadline() or context.WithTimeout() with (for-now) a hard-coded timeout which we can later refactor to be an env variable.

@kchiranjewee63
Copy link
Collaborator Author

While the context.Background() can be used, it's typically used for initialization since it doesn't have a deadline and is never cancelled. Instead, we should use context.WithDeadline() or context.WithTimeout() with (for-now) a hard-coded timeout which we can later refactor to be an env variable.

These are example codes demonstrating how to use the library. I'm not sure if adding a timeout here provides any additional value. Using context.Background() seems simpler and more straightforward.

@richardxgao
Copy link
Collaborator

While the context.Background() can be used, it's typically used for initialization since it doesn't have a deadline and is never cancelled. Instead, we should use context.WithDeadline() or context.WithTimeout() with (for-now) a hard-coded timeout which we can later refactor to be an env variable.

These are example codes demonstrating how to use the library. I'm not sure if adding a timeout here provides any additional value. Using context.Background() seems simpler and more straightforward.

I believe it does add value in the sense that we're demonstrating best practices in our examples. See the sgnl repo for examples on context.WithTimeout().

@kchiranjewee63
Copy link
Collaborator Author

While the context.Background() can be used, it's typically used for initialization since it doesn't have a deadline and is never cancelled. Instead, we should use context.WithDeadline() or context.WithTimeout() with (for-now) a hard-coded timeout which we can later refactor to be an env variable.

These are example codes demonstrating how to use the library. I'm not sure if adding a timeout here provides any additional value. Using context.Background() seems simpler and more straightforward.

I believe it does add value in the sense that we're demonstrating best practices in our examples. See the sgnl repo for examples on context.WithTimeout().

I agree. It would show good practice, but adding a specific timeout value in the documentation feels odd. What timeout value should I add? Timeout value depends on application-specific implementation and is application-specific decision. Examples here do not make any network call for signing the token. It just usages a dummy, in-memory key. So a really small timeout value would be appropriate here. But in real code, which most likely makes a network call, this timeout value would not be right.

Also, I kind of feel adding a specific timeout in the documentation distracts from showing the main functionality of the library.

Let me know your thoughts.

@richardxgao
Copy link
Collaborator

Timeout value depends on application-specific implementation and is application-specific decision

Yes, that's correct hence why it ultimately should be a configurable env variable and not a hard-coded timeout value. But for the sake of this example code, I think a hard-coded timeout value is good enough as it reminds the user to set a timeout and avoid using the context.Background().

I don't believe we should be concerned with the specific timeout value we choose to use in this example. We should be concerned with demonstrating/reminding that a timeout should be configured. In fact, let's add a comment above the timeout code reminding the user to set their own appropriate timeout.

Also, I kind of feel adding a specific timeout in the documentation distracts from showing the main functionality of the library.

I don't believe this should be case -- a context timeout is almost always seen in any networked/unbound calls in Go clients, so it really shouldn't add much mental overhead.

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.

2 participants