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

Introduce project event webhook #1113

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

window9u
Copy link
Contributor

@window9u window9u commented Dec 26, 2024

What this PR does / why we need it:

This PR implements document creation and document removal events for a new project event webhook. Currently, users who uses Yorkie as a SaaS are not always aware of document status changes. By introducing project-level events—rather than relying solely on doc-level events like attach or push-pull—we can more clearly represent the lifecycle of documents and make those events available to external systems.

Which issue(s) this PR fixes: Fixes #1002

Special notes for your reviewer:

  1. Project event webhook
  • The original issue suggested a DocEvent, but instead, I'm introducing a Project Event. We already have DocEvent events (DocumentChanged DocumentWatched, DocumentUnwatched, broadcast), and using the same naming might be confusing.
  • Additionally, doc-level events(e.g., attach, document-changed, watched, unwatched) don't clearly convey CRUD semantics. For instances:
    • Create: A document is automatically created when a user attaches to a non-existent document. documentAttached alone doesn't explicitly indicate creation.
    • Read/Update: push-pull may imply either reading or updating a document, and it's not always obvious which. (A Change can contain both presence and operations; if it only has presence, it might not mean a real update of document's content.)
    • Delete: Yorkie's removeDocument function only marks a document as "removed" without fully deleting it. In contrast, the removeDocumentByAdmin function permanently deletes the document. When a removeDocument action occurs, Yorkie sends webhook events, allowing the customer's server to decide the next steps—such as moving the document to a recycle bin or directly deleting it using removeDocumentByAdmin.
  1. Add HMAC signature in request header
  • For the authentication webhook, we provide a token so the receiver can validate the request. However, for the project event webhook, the endpoint may not know which service is calling.
  • To address this, I introduced an HMAC signature in the X-Signature header, generated using the project's secret key. The receiving server can verify this signature to ensure the request is from Yorkie.
  • In another approach, we could set our public key in projectKey in our request attributes.
type WebhookAttribute struct {
        ProjectKey string `json:"projectKey"`
	DocumentKey string `json:"documentKey"`
	ClientKey   string `json:"clientKey"`
	IssuedAt    string `json:"issuedAt"`
}
  1. Handling timeouts and unreachable events

    • Asynchronous work: Authentication must be verified before proceeding, so the auth webhook is synchronous. In contrast, a project event occurs after some action is completed, so it can be handled asynchronously without impacting user response times.
    • Unreachable events and replay: If an endpoint is down or encounters an error, we need a way to track failed events. In this PR, we only log these failures; in the future, the Yorkie dashboard could handle or additional recovery steps.
    • More relaxed timeouts: Currently, the auth webhook uses exponential backoff with a base of 200ms and a maximum wait of 3 seconds. This PR applies the same settings for consistency, although we might later adopt a more extended retry schedule(e.g., Liveblocks' approach: [immediately, 5s, 5m, 30m, 2h, 5h, 10h], etc.).
  2. Add an HTTP client with timeout

  • Since http.POST does not allow specifying a request timeout directly, using an HTTP client with a configurable timeout is more predictable. Although the auth webhook still uses http.POST, we may consider migrating it to an HTTP client for consistency.

In this PR

This design proposal differs somewhat from the original issue’s requirements. If the idea seems sound, I’ll proceed with the remaining tasks:

  • Add config validation and tests
  • Add event_webhook tests
  • Update the admin server’s UpdateProjectRequest API spec
  • Integrate the auth webhook’s exponential backoff logic into pkg/webhook

In a subsequent PR

  • Introduce a document-changed event (with caching)

Does this PR introduce a user-facing change?


Additional documentation

There are four configuration options related to timeouts, which can be confusing. Below is a brief explanation:

  • EventWebhookRequestTimeout: How long to wait for a response before timing out
  • EventWebhookMaxRetries: Number of retry attempts
  • EventWebhookBaseWaitInterval: Base interval for exponential backoff
  • EventWebhookMaxWaitInterval: Maximum interval between retries

image

N/A

Checklist

  • Added relevant tests or not required
  • Addressed and resolved all CodeRabbit review comments
  • Ensured no breaking changes

Copy link

coderabbitai bot commented Dec 26, 2024

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@window9u
Copy link
Contributor Author

window9u commented Dec 28, 2024

I notice that the RemoveDocument feature has more context discussed in issue #484. In that PR, three tasks were outlined:

  1. Implement a basic document deletion API that sets the RemovedAt date in docInfo.
  2. Decide whether to include removed documents in the ListDocuments API, which is used by the Dashboard and CLI (Admin).
  3. Create a housekeeping background process to physically delete documents (is_removed = true) and their related data.

The first task has been completed, and the second task has been worked on #563. but third task is ongoing in PR #718.

I'm currently considering when we should publish the documentRemoved webhook. This depends on whether we plan to support document revival (i.e., setting is_removed = false before physical deletion).

Two possible approaches:

  1. Trigger the webhook during soft deletion

    • If we don't plan to support document revival, this makes sense as it allows for faster updates.
  2. Trigger the webhook during hard (physical) deletion

    • If we do plan to support document revival, this approach is more logical, as triggering the webhook during soft deletion could cause confusion.

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.

Provide DocEvent Webhook
1 participant