-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: share UnitToDuration logic (#4510)
* chore: share UnitToDuration logic Signed-off-by: zirain <zirain2009@gmail.com> * lint Signed-off-by: zirain <zirain2009@gmail.com> --------- Signed-off-by: zirain <zirain2009@gmail.com>
- Loading branch information
Showing
3 changed files
with
42 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright Envoy Gateway Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// The full text of the Apache license is available in the LICENSE file at | ||
// the root of the repo. | ||
|
||
package ratelimit | ||
|
||
import ( | ||
"google.golang.org/protobuf/types/known/durationpb" | ||
|
||
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" | ||
"github.com/envoyproxy/gateway/internal/ir" | ||
) | ||
|
||
func UnitToSeconds(unit egv1a1.RateLimitUnit) int64 { | ||
var seconds int64 | ||
|
||
switch unit { | ||
case egv1a1.RateLimitUnitSecond: | ||
seconds = 1 | ||
case egv1a1.RateLimitUnitMinute: | ||
seconds = 60 | ||
case egv1a1.RateLimitUnitHour: | ||
seconds = 60 * 60 | ||
case egv1a1.RateLimitUnitDay: | ||
seconds = 60 * 60 * 24 | ||
} | ||
return seconds | ||
} | ||
|
||
func UnitToDuration(unit ir.RateLimitUnit) *durationpb.Duration { | ||
seconds := UnitToSeconds(egv1a1.RateLimitUnit(unit)) | ||
return &durationpb.Duration{ | ||
Seconds: seconds, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters