github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/quota/noop.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package quota 16 17 import ( 18 "context" 19 "fmt" 20 ) 21 22 type noopManager struct{} 23 24 const ( 25 noopUser = "noopQuotaUser" // arbitrary string 26 ) 27 28 // Noop returns a noop implementation of Manager. It allows all requests without restriction. 29 func Noop() Manager { 30 return &noopManager{} 31 } 32 33 func (n noopManager) GetUser(ctx context.Context, req interface{}) string { 34 return noopUser 35 } 36 37 func (n noopManager) GetTokens(ctx context.Context, numTokens int, specs []Spec) error { 38 if err := validateNumTokens(numTokens); err != nil { 39 return err 40 } 41 return validateSpecs(specs) 42 } 43 44 func (n noopManager) PeekTokens(ctx context.Context, specs []Spec) (map[Spec]int, error) { 45 if err := validateSpecs(specs); err != nil { 46 return nil, err 47 } 48 tokens := make(map[Spec]int) 49 for _, spec := range specs { 50 tokens[spec] = MaxTokens 51 } 52 return tokens, nil 53 } 54 55 func (n noopManager) PutTokens(ctx context.Context, numTokens int, specs []Spec) error { 56 if err := validateNumTokens(numTokens); err != nil { 57 return err 58 } 59 return validateSpecs(specs) 60 } 61 62 func (n noopManager) ResetQuota(ctx context.Context, specs []Spec) error { 63 return validateSpecs(specs) 64 } 65 66 func (n noopManager) SetupInitialQuota(ctx context.Context, treeID int64) error { 67 return nil 68 } 69 70 func validateNumTokens(numTokens int) error { 71 if numTokens <= 0 { 72 return fmt.Errorf("invalid numTokens: %v (>0 required)", numTokens) 73 } 74 return nil 75 } 76 77 func validateSpecs(specs []Spec) error { 78 for _, spec := range specs { 79 switch { 80 case spec.Group == User && spec.User != noopUser: 81 return fmt.Errorf("invalid quota user: %v (expected %v)", spec.User, noopUser) 82 case spec.Group == Tree && spec.TreeID <= 0: 83 return fmt.Errorf("invalid tree ID: %v (expected >=0)", spec.TreeID) 84 } 85 } 86 return nil 87 }