Skip to content

Commit

Permalink
Remove event webhook cache
Browse files Browse the repository at this point in the history
  • Loading branch information
window9u committed Feb 20, 2025
1 parent d4f87cf commit da1fda0
Show file tree
Hide file tree
Showing 14 changed files with 10 additions and 102 deletions.
13 changes: 0 additions & 13 deletions cmd/yorkie/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func newServerCmd() *cobra.Command {
conf.Backend.EventWebhookMaxWaitInterval = eventWebhookMaxWaitInterval.String()
conf.Backend.EventWebhookMinWaitInterval = eventWebhookMinWaitInterval.String()
conf.Backend.EventWebhookRequestTimeout = eventWebhookRequestTimeout.String()
conf.Backend.EventWebhookCacheTTL = eventWebhookCacheTTL.String()

conf.Backend.ProjectCacheTTL = projectCacheTTL.String()

Expand Down Expand Up @@ -384,18 +383,6 @@ func init() {
server.DefaultEventWebhookMaxWaitInterval,
"Maximum wait interval between retries(exponential backoff).",
)
cmd.Flags().IntVar(
&conf.Backend.EventWebhookCacheSize,
"event-webhook-cache-size",
server.DefaultEventWebhookCacheSize,
"The cache size of the event webhook.",
)
cmd.Flags().DurationVar(
&eventWebhookCacheTTL,
"event-webhook-cache-ttl",
server.DefaultEventWebhookCacheTTL,
"TTL value to set when caching event webhook response.",
)
cmd.Flags().IntVar(
&conf.Backend.ProjectCacheSize,
"project-info-cache-size",
Expand Down
3 changes: 2 additions & 1 deletion pkg/webhook/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ type Client[Req any, Res any] struct {
options Options
}

// NewClient creates a new instance of Client.
// NewClient creates a new instance of Client. If you only want to get the status code,
// then set Res to int.
func NewClient[Req any, Res any](
options Options,
) *Client[Req, Res] {
Expand Down
6 changes: 0 additions & 6 deletions server/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ type Backend struct {
// AuthWebhookClient is used to send auth webhook.
AuthWebhookClient *webhook.Client[types.AuthWebhookRequest, types.AuthWebhookResponse]

// EventWebhookCache is used to cache the response of the event webhook.
EventWebhookCache *cache.LRUExpireCache[string, int]
// EventWebhookClient is used to send event webhook
EventWebhookClient *webhook.Client[types.EventWebhookRequest, int]

Expand Down Expand Up @@ -108,9 +106,6 @@ func New(
},
)

eventWebhookCache := cache.NewLRUExpireCache[string, int](
conf.EventWebhookCacheSize,
)
eventWebhookClient := webhook.NewClient[types.EventWebhookRequest, int](
webhook.Options{
MaxRetries: conf.EventWebhookMaxRetries,
Expand Down Expand Up @@ -186,7 +181,6 @@ func New(
AuthWebhookClient: authWebhookClient,

EventWebhookClient: eventWebhookClient,
EventWebhookCache: eventWebhookCache,

Locker: locker,
PubSub: pubsub,
Expand Down
25 changes: 0 additions & 25 deletions server/backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ type Config struct {
// EventWebhookRequestTimeout is the max waiting time per event webhook request.
EventWebhookRequestTimeout string `yaml:"EventWebhookRequestTimeout"`

// EventWebhookCacheSize is the cache size of the event webhook.
EventWebhookCacheSize int `yaml:"EventWebhookCacheSize"`

// EventWebhookCacheTTL is the TTL value to set when caching the event webhook result.
EventWebhookCacheTTL string `yaml:"EventWebhookCacheTTL"`

// ProjectCacheSize is the cache size of the project metadata.
ProjectCacheSize int `yaml:"ProjectCacheSize"`

Expand Down Expand Up @@ -172,14 +166,6 @@ func (c *Config) Validate() error {
err,
)
}

if _, err := time.ParseDuration(c.EventWebhookCacheTTL); err != nil {
return fmt.Errorf(
`invalid argument "%s" for "--event-webhook-cache-ttl" flag: %w`,
c.EventWebhookCacheTTL,
err,
)
}
if _, err := time.ParseDuration(c.ProjectCacheTTL); err != nil {
return fmt.Errorf(
`invalid argument "%s" for "--project-info-cache-ttl" flag: %w`,
Expand Down Expand Up @@ -279,17 +265,6 @@ func (c *Config) ParseEventWebhookRequestTimeout() time.Duration {
return result
}

// ParseEventWebhookCacheTTL returns TTL for event webhook cache.
func (c *Config) ParseEventWebhookCacheTTL() time.Duration {
result, err := time.ParseDuration(c.EventWebhookCacheTTL)
if err != nil {
fmt.Fprintln(os.Stderr, "parse event webhook cache ttl: %w", err)
os.Exit(1)
}

return result
}

// ParseProjectCacheTTL returns TTL for project metadata cache.
func (c *Config) ParseProjectCacheTTL() time.Duration {
result, err := time.ParseDuration(c.ProjectCacheTTL)
Expand Down
5 changes: 0 additions & 5 deletions server/backend/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func TestConfig(t *testing.T) {
EventWebhookMaxWaitInterval: "0ms",
EventWebhookMinWaitInterval: "0ms",
EventWebhookRequestTimeout: "0ms",
EventWebhookCacheTTL: "10s",
}
assert.NoError(t, validConf.Validate())

Expand Down Expand Up @@ -75,9 +74,5 @@ func TestConfig(t *testing.T) {
conf9 := validConf
conf9.EventWebhookRequestTimeout = "1"
assert.Error(t, conf9.Validate())

conf10 := validConf
conf10.EventWebhookCacheTTL = "s"
assert.Error(t, conf10.Validate())
})
}
10 changes: 0 additions & 10 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ const (
DefaultEventWebhookMaxRetries = 10
DefaultEventWebhookMaxWaitInterval = 3000 * time.Millisecond
DefaultEventWebhookMinWaitInterval = 100 * time.Millisecond
DefaultEventWebhookCacheSize = 5000
DefaultEventWebhookCacheTTL = 10 * time.Second

DefaultProjectCacheSize = 256
DefaultProjectCacheTTL = 10 * time.Minute
Expand Down Expand Up @@ -218,10 +216,6 @@ func (c *Config) ensureDefaultValue() {
c.Backend.AuthWebhookCacheTTL = DefaultAuthWebhookCacheTTL.String()
}

if c.Backend.EventWebhookCacheSize == 0 {
c.Backend.EventWebhookCacheSize = DefaultEventWebhookCacheSize
}

if c.Backend.EventWebhookMaxRetries == 0 {
c.Backend.EventWebhookMaxRetries = DefaultEventWebhookMaxRetries
}
Expand All @@ -238,10 +232,6 @@ func (c *Config) ensureDefaultValue() {
c.Backend.EventWebhookRequestTimeout = DefaultEventWebhookRequestTimeout.String()
}

if c.Backend.EventWebhookCacheTTL == "" {
c.Backend.EventWebhookCacheTTL = DefaultEventWebhookCacheTTL.String()
}

if c.Backend.ProjectCacheSize == 0 {
c.Backend.ProjectCacheSize = DefaultProjectCacheSize
}
Expand Down
3 changes: 0 additions & 3 deletions server/config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ Backend:
# EventWebhookMaxWaitInterval is the maximum wait interval between retries(exponential backoff).
EventWebhookMaxWaitInterval: "3s"

# EventWebhookCacheTTL is the TTL value to set when caching the event result.
EventWebhookCacheTTL: "10s"

# ProjectCacheSize is the size of the project metadata cache.
ProjectCacheSize: 256

Expand Down
4 changes: 0 additions & 4 deletions server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ func TestNewConfigFromFile(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, eventWebhookRequestTimeout, server.DefaultEventWebhookRequestTimeout)

eventWebhookCacheTTL, err := time.ParseDuration(conf.Backend.EventWebhookCacheTTL)
assert.NoError(t, err)
assert.Equal(t, eventWebhookCacheTTL, server.DefaultEventWebhookCacheTTL)

projectCacheTTL, err := time.ParseDuration(conf.Backend.ProjectCacheTTL)
assert.NoError(t, err)
assert.Equal(t, projectCacheTTL, server.DefaultProjectCacheTTL)
Expand Down
2 changes: 0 additions & 2 deletions server/packs/packs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ func TestMain(m *testing.M) {
AuthWebhookMaxWaitInterval: helper.AuthWebhookMaxWaitInterval.String(),
AuthWebhookMinWaitInterval: helper.AuthWebhookMinWaitInterval.String(),
AuthWebhookRequestTimeout: helper.AuthWebhookRequestTimeout.String(),
EventWebhookCacheSize: helper.EventWebhookSize,
EventWebhookCacheTTL: helper.EventWebhookCacheTTL.String(),
EventWebhookMaxWaitInterval: helper.EventWebhookMaxWaitInterval.String(),
EventWebhookMinWaitInterval: helper.EventWebhookMinWaitInterval.String(),
EventWebhookRequestTimeout: helper.EventWebhookRequestTimeout.String(),
Expand Down
2 changes: 0 additions & 2 deletions server/rpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ func TestMain(m *testing.M) {
AuthWebhookMaxWaitInterval: helper.AuthWebhookMaxWaitInterval.String(),
AuthWebhookMinWaitInterval: helper.AuthWebhookMinWaitInterval.String(),
AuthWebhookRequestTimeout: helper.AuthWebhookRequestTimeout.String(),
EventWebhookCacheSize: helper.EventWebhookSize,
EventWebhookCacheTTL: helper.EventWebhookCacheTTL.String(),
EventWebhookMaxWaitInterval: helper.EventWebhookMaxWaitInterval.String(),
EventWebhookMinWaitInterval: helper.EventWebhookMinWaitInterval.String(),
EventWebhookRequestTimeout: helper.EventWebhookRequestTimeout.String(),
Expand Down
22 changes: 3 additions & 19 deletions server/webhook/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,16 @@ func SendEvent(
return fmt.Errorf("marshal event webhook request: %w", err)
}

cacheKey := generateEventCacheKey(prj.PublicKey, docKey, string(webhookType))
if _, found := be.EventWebhookCache.Get(cacheKey); found {
return nil
}

_, status, err := be.EventWebhookClient.Send(
// TODO(window9u): we should handle this returned status code properly.
if _, _, err := be.EventWebhookClient.Send(
ctx,
prj.EventWebhookURL,
prj.SecretKey,
body,
)
if err != nil {
); err != nil {
return fmt.Errorf("send event webhook: %w", err)
}

be.EventWebhookCache.Add(
cacheKey,
status,
be.Config.ParseEventWebhookCacheTTL(),
)

return nil
}

Expand All @@ -91,8 +80,3 @@ func buildEventWebhookBody(docKey string, webhookType types.EventWebhookType) ([

return body, nil
}

// generateEventCacheKey creates a unique cache key for an event webhook.
func generateEventCacheKey(publicKey, docKey, webhookType string) string {
return fmt.Sprintf("%s:event:%s:%s", publicKey, docKey, webhookType)
}
2 changes: 0 additions & 2 deletions test/complex/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ func TestMain(m *testing.M) {
EventWebhookMaxWaitInterval: helper.EventWebhookMaxWaitInterval.String(),
EventWebhookMinWaitInterval: helper.EventWebhookMinWaitInterval.String(),
EventWebhookRequestTimeout: helper.EventWebhookRequestTimeout.String(),
EventWebhookCacheSize: helper.EventWebhookSize,
EventWebhookCacheTTL: helper.EventWebhookCacheTTL.String(),
ProjectCacheSize: helper.ProjectCacheSize,
ProjectCacheTTL: helper.ProjectCacheTTL.String(),
AdminTokenDuration: helper.AdminTokenDuration,
Expand Down
2 changes: 0 additions & 2 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,6 @@ func TestConfig() *server.Config {
EventWebhookMaxWaitInterval: EventWebhookMaxWaitInterval.String(),
EventWebhookMinWaitInterval: EventWebhookMinWaitInterval.String(),
EventWebhookRequestTimeout: EventWebhookRequestTimeout.String(),
EventWebhookCacheSize: EventWebhookSize,
EventWebhookCacheTTL: EventWebhookCacheTTL.String(),
ProjectCacheSize: ProjectCacheSize,
ProjectCacheTTL: ProjectCacheTTL.String(),
GatewayAddr: fmt.Sprintf("localhost:%d", RPCPort+portOffset),
Expand Down
13 changes: 5 additions & 8 deletions test/integration/event_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,8 @@ func newWebhookServer(t *testing.T, secretKey, docKey string) (*httptest.Server,
}

// newYorkieServer initializes the Yorkie server and admin client.
func newYorkieServer(t *testing.T, webhookCacheTTL, projectCacheTTL string) *server.Yorkie {
func newYorkieServer(t *testing.T, projectCacheTTL string) *server.Yorkie {
conf := helper.TestConfig()
if webhookCacheTTL != "default" {
conf.Backend.EventWebhookCacheTTL = webhookCacheTTL
}
if projectCacheTTL != "default" {
conf.Backend.ProjectCacheTTL = projectCacheTTL
}
Expand Down Expand Up @@ -115,7 +112,7 @@ func TestRegisterEventWebhook(t *testing.T) {

// Set up yorkie server
projectCacheTTL := 1 * time.Millisecond
svr := newYorkieServer(t, "0ms", projectCacheTTL.String())
svr := newYorkieServer(t, projectCacheTTL.String())

// Set up project
adminCli := helper.CreateAdminCli(t, svr.RPCAddr())
Expand Down Expand Up @@ -189,7 +186,7 @@ func TestRegisterEventWebhook(t *testing.T) {
func TestDocRootChangedEventWebhook(t *testing.T) {
ctx := context.Background()

svr := newYorkieServer(t, "0ms", "default")
svr := newYorkieServer(t, "default")
adminCli := helper.CreateAdminCli(t, svr.RPCAddr())

project, err := adminCli.CreateProject(ctx, "doc-root-changed-event-webhook")
Expand Down Expand Up @@ -251,7 +248,7 @@ func TestEventWebhookCache(t *testing.T) {
ctx := context.Background()

webhookCacheTTL := 10 * time.Millisecond
svr := newYorkieServer(t, webhookCacheTTL.String(), "default")
svr := newYorkieServer(t, "default")
adminCli := helper.CreateAdminCli(t, svr.RPCAddr())

project, err := adminCli.CreateProject(ctx, "event-webhook-cache-webhook")
Expand All @@ -272,7 +269,7 @@ func TestEventWebhookCache(t *testing.T) {

waitWebhookReceived := 20 * time.Millisecond
t.Run("throttling event test", func(t *testing.T) {
t.Skip("remove this after implement advanced event cache control")
t.Skip("remove this after implement advanced event timing control")

expectedUpdates := 5
testDuration := webhookCacheTTL * time.Duration(expectedUpdates)
Expand Down

0 comments on commit da1fda0

Please sign in to comment.