github.com/m3db/m3@v1.5.0/src/dbnode/storage/limits/permits/permit.go (about) 1 // Copyright (c) 2021 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package permits 22 23 import ( 24 "go.uber.org/atomic" 25 "go.uber.org/zap" 26 27 "github.com/m3db/m3/src/x/instrument" 28 ) 29 30 // permit tracks the quota used by a caller and provides basic sanity checks that a caller 31 // correctly uses the permit. 32 type permit struct { 33 // immutable state 34 quota int64 35 iOpts instrument.Options 36 37 // mutable state 38 quotaUsed int64 39 // refCount is used to check if a caller incorrectly double releases/acquires a permit. the value should 40 // always be 0 (nobody holds the permit) or 1 (somebody holds the permit). 41 refCount atomic.Int32 42 } 43 44 // NewPermit constructs a new Permit with the provided quota. 45 func NewPermit(quota int64, iOpts instrument.Options) Permit { 46 return &permit{ 47 quota: quota, 48 iOpts: iOpts, 49 } 50 } 51 52 func (p *permit) PostRelease() { 53 if p.iOpts != nil && p.refCount.Dec() != 0 { 54 instrument.EmitAndLogInvariantViolation(p.iOpts, func(l *zap.Logger) { 55 l.Error("permit released more than once") 56 }) 57 } 58 } 59 60 func (p *permit) PreAcquire() { 61 if p.iOpts != nil && p.refCount.Inc() != 1 { 62 instrument.EmitAndLogInvariantViolation(p.iOpts, func(l *zap.Logger) { 63 l.Error("permit acquired more than once") 64 }) 65 } 66 p.quotaUsed = 0 67 } 68 69 // AllowedQuota is the amount of quota the caller can use with this Permit. 70 func (p *permit) AllowedQuota() int64 { 71 return p.quota 72 } 73 74 // QuotaRemaining is the amount of remaining quota for this Permit. Can be negative if the caller used more quota 75 // than they were allowed. 76 func (p *permit) QuotaRemaining() int64 { 77 return p.quota - p.quotaUsed 78 } 79 80 // Use adds the quota to the total used quota. 81 func (p *permit) Use(quota int64) { 82 p.quotaUsed += quota 83 }