-
Notifications
You must be signed in to change notification settings - Fork 9
v1.0.4 #17
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
+3,494
−424
Merged
v1.0.4 #17
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7abd87f
v1.0.4
CSCITech 5c9f603
v1.0.4
CSCITech 5291db2
v1.0.4
CSCITech 444a05e
v1.0.4
CSCITech bab6a4c
v1.0.4
CSCITech 9f4a745
v1.0.4
CSCITech f058a64
v1.0.4
CSCITech 1528dc9
v1.0.4
CSCITech b688fb5
v1.0.4-1
CSCITech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,21 +1,131 @@ | ||
| package common | ||
|
|
||
| import "math" | ||
| import ( | ||
| "fmt" | ||
| "math" | ||
|
|
||
| // QuotaFromFloat converts a computed quota value to int with saturation. | ||
| // Quota products can include user-controlled multipliers such as image count, | ||
| // video seconds, or resolution ratios; oversized products must never wrap into | ||
| // a negative charge. The bound is int32 because quota columns are int fields | ||
| // used as 32-bit database integers in supported deployments. | ||
| func QuotaFromFloat(value float64) int { | ||
| if math.IsNaN(value) { | ||
| return 0 | ||
| "github.com/shopspring/decimal" | ||
| ) | ||
|
|
||
| // Quota conversions are centralized here so every billing path shares one | ||
| // saturation + logging policy. Quota columns are 32-bit integers in supported | ||
| // deployments, so an oversized product must clamp to the int32 range instead | ||
| // of wrapping around and turning a charge into a credit. | ||
| const ( | ||
| MaxQuota = math.MaxInt32 | ||
| MinQuota = math.MinInt32 | ||
| ) | ||
|
|
||
| // QuotaClampKind identifies why a quota conversion had to be saturated. | ||
| type QuotaClampKind string | ||
|
|
||
| const ( | ||
| QuotaClampOverflow QuotaClampKind = "overflow" | ||
| QuotaClampUnderflow QuotaClampKind = "underflow" | ||
| QuotaClampNaN QuotaClampKind = "nan" | ||
| ) | ||
|
|
||
| // QuotaClamp describes a single saturation event and is stored under | ||
| // admin_info.quota_saturation for audit when a billing path has to clamp. | ||
| type QuotaClamp struct { | ||
| Op string `json:"op"` | ||
| Kind QuotaClampKind `json:"kind"` | ||
| Original float64 `json:"original"` | ||
| Clamped int `json:"clamped"` | ||
| } | ||
|
|
||
| func (c *QuotaClamp) Error() string { | ||
| if c == nil { | ||
| return "" | ||
| } | ||
| if value >= math.MaxInt32 { | ||
| return math.MaxInt32 | ||
| return fmt.Sprintf("quota conversion (%s) %s: original=%g, clamped=%d", c.Op, c.Kind, c.Original, c.Clamped) | ||
| } | ||
|
|
||
| func (c *QuotaClamp) AuditMap() map[string]interface{} { | ||
| if c == nil { | ||
| return nil | ||
| } | ||
| if value <= math.MinInt32 { | ||
| return math.MinInt32 | ||
| original := interface{}(c.Original) | ||
| switch { | ||
| case math.IsNaN(c.Original): | ||
| original = "NaN" | ||
| case math.IsInf(c.Original, 1): | ||
| original = "+Inf" | ||
| case math.IsInf(c.Original, -1): | ||
| original = "-Inf" | ||
| } | ||
| return int(value) | ||
| return map[string]interface{}{ | ||
| "op": c.Op, | ||
| "kind": c.Kind, | ||
| "original": original, | ||
| "clamped": c.Clamped, | ||
| } | ||
| } | ||
|
|
||
| func saturateQuota(value float64, op string) (int, *QuotaClamp) { | ||
| var clamp *QuotaClamp | ||
| switch { | ||
| case math.IsNaN(value): | ||
| clamp = &QuotaClamp{Op: op, Kind: QuotaClampNaN, Original: value, Clamped: 0} | ||
| case value > MaxQuota: | ||
| clamp = &QuotaClamp{Op: op, Kind: QuotaClampOverflow, Original: value, Clamped: MaxQuota} | ||
| case value < MinQuota: | ||
| clamp = &QuotaClamp{Op: op, Kind: QuotaClampUnderflow, Original: value, Clamped: MinQuota} | ||
| default: | ||
| return int(value), nil | ||
| } | ||
| SysError(clamp.Error()) | ||
| return clamp.Clamped, clamp | ||
| } | ||
|
|
||
| func strictQuota(quota int, clamp *QuotaClamp) (int, error) { | ||
| if clamp != nil { | ||
| return 0, clamp | ||
| } | ||
| return quota, nil | ||
| } | ||
|
|
||
| // QuotaFromFloat converts a computed quota value to int, truncating toward | ||
| // zero, with int32 saturation. | ||
| func QuotaFromFloat(value float64) int { | ||
| quota, _ := QuotaFromFloatChecked(value) | ||
| return quota | ||
| } | ||
|
|
||
| // QuotaFromFloatChecked is QuotaFromFloat plus a non-nil clamp descriptor | ||
| // when saturation or NaN fallback happened. | ||
| func QuotaFromFloatChecked(value float64) (int, *QuotaClamp) { | ||
| return saturateQuota(value, "QuotaFromFloat") | ||
| } | ||
|
|
||
| // QuotaFromFloatStrict rejects unrepresentable billing estimates instead of | ||
| // silently saturating them. | ||
| func QuotaFromFloatStrict(value float64) (int, error) { | ||
| return strictQuota(QuotaFromFloatChecked(value)) | ||
| } | ||
|
|
||
| // QuotaRound converts a float64 quota value to int using half-away-from-zero | ||
| // rounding, with the same saturation policy. | ||
| func QuotaRound(value float64) int { | ||
| quota, _ := QuotaRoundChecked(value) | ||
| return quota | ||
| } | ||
|
|
||
| func QuotaRoundChecked(value float64) (int, *QuotaClamp) { | ||
| return saturateQuota(math.Round(value), "QuotaRound") | ||
| } | ||
|
|
||
| func QuotaRoundStrict(value float64) (int, error) { | ||
| return strictQuota(QuotaRoundChecked(value)) | ||
| } | ||
|
|
||
| // QuotaFromDecimal rounds a computed quota decimal before conversion. | ||
| func QuotaFromDecimal(d decimal.Decimal) int { | ||
| quota, _ := QuotaFromDecimalChecked(d) | ||
| return quota | ||
| } | ||
|
|
||
| func QuotaFromDecimalChecked(d decimal.Decimal) (int, *QuotaClamp) { | ||
| f, _ := d.Round(0).Float64() | ||
| return saturateQuota(f, "QuotaFromDecimal") | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.