Skip to content

Commit

Permalink
Revise the codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Feb 6, 2025
1 parent 27bad00 commit 638932f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
24 changes: 12 additions & 12 deletions cmd/yorkie/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,29 +299,29 @@ func init() {
server.DefaultSnapshotDisableGC,
"Whether to disable garbage collection of snapshots.",
)
cmd.Flags().DurationVar(
&authWebhookRequestTimeout,
"auth-webhook-request-timeout",
server.DefaultAuthWebhookRequestTimeout,
"Timeout for each authorization webhook request.",
)
cmd.Flags().Uint64Var(
&conf.Backend.AuthWebhookMaxRetries,
"auth-webhook-max-retries",
server.DefaultAuthWebhookMaxRetries,
"Maximum number of retries for an authorization webhook.",
)
cmd.Flags().DurationVar(
&authWebhookMaxWaitInterval,
"auth-webhook-max-wait-interval",
server.DefaultAuthWebhookMaxWaitInterval,
"Maximum wait interval for authorization webhook.",
"Maximum number of retries for authorization webhook.",
)
cmd.Flags().DurationVar(
&authWebhookMinWaitInterval,
"auth-webhook-min-wait-interval",
server.DefaultAuthWebhookMinWaitInterval,
"Minimum wait interval for authorization webhook.",
"Minimum wait interval between retries(exponential backoff).",
)
cmd.Flags().DurationVar(
&authWebhookRequestTimeout,
"auth-webhook-request-timeout",
server.DefaultAuthWebhookRequestTimeout,
"Maximum wait time per authorization webhook request.",
&authWebhookMaxWaitInterval,
"auth-webhook-max-wait-interval",
server.DefaultAuthWebhookMaxWaitInterval,
"Maximum wait interval between retries(exponential backoff).",
)
cmd.Flags().IntVar(
&conf.Backend.AuthWebhookCacheSize,
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ var (

// Options are the options for the webhook httpClient.
type Options struct {
RequestTimeout time.Duration
MaxRetries uint64
MinWaitInterval time.Duration
MaxWaitInterval time.Duration
RequestTimeout time.Duration
}

// Client is a httpClient for the webhook.
Expand Down
12 changes: 6 additions & 6 deletions pkg/webhook/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestHMAC(t *testing.T) {
})
}

func TestRetryRequest(t *testing.T) {
func TestBackoff(t *testing.T) {
replyAfter := 4
reachableRetries := replyAfter - 1
unreachableRetries := replyAfter - 2
Expand All @@ -170,17 +170,17 @@ func TestRetryRequest(t *testing.T) {
defer server.Close()

reachableClient := webhook.NewClient[testRequest, testResponse](webhook.Options{
RequestTimeout: 10 * time.Millisecond,
MaxRetries: uint64(reachableRetries),
MinWaitInterval: 1 * time.Millisecond,
MaxWaitInterval: 5 * time.Millisecond,
RequestTimeout: 10 * time.Millisecond,
})

unreachableClient := webhook.NewClient[testRequest, testResponse](webhook.Options{
RequestTimeout: 10 * time.Millisecond,
MaxRetries: uint64(unreachableRetries),
MinWaitInterval: 1 * time.Millisecond,
MaxWaitInterval: 5 * time.Millisecond,
RequestTimeout: 10 * time.Millisecond,
})

t.Run("retry fail test", func(t *testing.T) {
Expand Down Expand Up @@ -215,17 +215,17 @@ func TestRequestTimeout(t *testing.T) {
defer server.Close()

reachableClient := webhook.NewClient[testRequest, testResponse](webhook.Options{
RequestTimeout: 15 * time.Millisecond,
MaxRetries: 0,
MinWaitInterval: 0,
MaxWaitInterval: 0,
RequestTimeout: 15 * time.Millisecond,
})

unreachableClient := webhook.NewClient[testRequest, testResponse](webhook.Options{
RequestTimeout: 5 * time.Millisecond,
MaxRetries: 0,
MinWaitInterval: 0,
MaxWaitInterval: 0,
RequestTimeout: 5 * time.Millisecond,
})

t.Run("request succeed after timeout", func(t *testing.T) {
Expand Down Expand Up @@ -258,10 +258,10 @@ func TestErrorHandling(t *testing.T) {
defer server.Close()

unreachableClient := webhook.NewClient[testRequest, testResponse](webhook.Options{
RequestTimeout: 50 * time.Millisecond,
MaxRetries: 0,
MinWaitInterval: 0,
MaxWaitInterval: 0,
RequestTimeout: 50 * time.Millisecond,
})

t.Run("request fails with context done test", func(t *testing.T) {
Expand Down
18 changes: 10 additions & 8 deletions server/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ func New(
conf.Hostname = hostname
}

// 02. Create auth webhook client and cache.
cache := cache.NewLRUExpireCache[string, pkgtypes.Pair[int, *types.AuthWebhookResponse]](
// 02. Create the webhook webhookCache and client.
webhookCache := cache.NewLRUExpireCache[string, pkgtypes.Pair[int, *types.AuthWebhookResponse]](
conf.AuthWebhookCacheSize,
)
auth := webhook.NewClient[types.AuthWebhookRequest, types.AuthWebhookResponse](
webhookClient := webhook.NewClient[types.AuthWebhookRequest, types.AuthWebhookResponse](
webhook.Options{
MaxRetries: conf.AuthWebhookMaxRetries,
MinWaitInterval: conf.ParseAuthWebhookMinWaitInterval(),
Expand Down Expand Up @@ -155,11 +155,13 @@ func New(
)

return &Backend{
Config: conf,
WebhookCache: cache,
WebhookClient: auth,
Locker: locker,
PubSub: pubsub,
Config: conf,

WebhookCache: webhookCache,
WebhookClient: webhookClient,

Locker: locker,
PubSub: pubsub,

Metrics: metrics,
DB: db,
Expand Down
2 changes: 1 addition & 1 deletion server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ const (
DefaultSnapshotWithPurgingChanges = false
DefaultSnapshotDisableGC = false

DefaultAuthWebhookRequestTimeout = 3 * time.Second
DefaultAuthWebhookMaxRetries = 10
DefaultAuthWebhookMaxWaitInterval = 3000 * time.Millisecond
DefaultAuthWebhookMinWaitInterval = 100 * time.Millisecond
DefaultAuthWebhookRequestTimeout = 10 * time.Second
DefaultAuthWebhookCacheSize = 5000
DefaultAuthWebhookCacheTTL = 10 * time.Second
DefaultProjectCacheSize = 256
Expand Down
14 changes: 7 additions & 7 deletions server/config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ Backend:
# AuthWebhookMethods is the list of methods to use for authorization.
AuthWebhookMethods: []

# AuthWebhookMaxRetries is the max count that retries the authorization webhook.
AuthWebhookMaxRetries: 10
# AuthWebhookRequestTimeout is the timeout for each authorization webhook request.
AuthWebhookRequestTimeout: "3s"

# AuthWebhookMaxWaitInterval is the max interval that waits before retrying the authorization webhook.
AuthWebhookMaxWaitInterval: "3s"
# AuthWebhookMaxRetries is the max number of retries for the authorization webhook.
AuthWebhookMaxRetries: 10

# AuthWebhookMinWaitInterval is the min interval that waits before retrying the authorization webhook.
# AuthWebhookMinWaitInterval is the minimum wait interval between retries(exponential backoff).
AuthWebhookMinWaitInterval: "100ms"

# AuthWebhookRequestTimeout is the max waiting time per authorization webhook request.
AuthWebhookRequestTimeout: "10s"
# AuthWebhookMaxWaitInterval is the maximum wait interval between retries(exponential backoff).
AuthWebhookMaxWaitInterval: "3s"

# AuthWebhookCacheTTL is the TTL value to set when caching the authorized result.
AuthWebhookCacheTTL: "10s"
Expand Down

0 comments on commit 638932f

Please sign in to comment.