|
| 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 redisqm defines a Redis-based quota.Manager implementation. |
| 16 | +package redisqm |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/google/trillian/quota" |
| 23 | + "github.com/google/trillian/quota/redis/redistb" |
| 24 | +) |
| 25 | + |
| 26 | +// ParameterFunc is a function that should return a token bucket's parameters |
| 27 | +// for a given quota specification. |
| 28 | +type ParameterFunc func(spec quota.Spec) (capacity int, rate float64) |
| 29 | + |
| 30 | +// ManagerOptions holds the parameters for a Manager. |
| 31 | +type ManagerOptions struct { |
| 32 | + // Parameters should return the parameters for a given quota.Spec. This |
| 33 | + // value must not be nil. |
| 34 | + Parameters ParameterFunc |
| 35 | + |
| 36 | + // Prefix is a static prefix to apply to all Redis keys; this is useful |
| 37 | + // if running on a multi-tenant Redis cluster. |
| 38 | + Prefix string |
| 39 | +} |
| 40 | + |
| 41 | +// Manager implements the quota.Manager interface backed by a Redis-based token |
| 42 | +// bucket implementation. |
| 43 | +type Manager struct { |
| 44 | + tb *redistb.TokenBucket |
| 45 | + opts ManagerOptions |
| 46 | +} |
| 47 | + |
| 48 | +var _ quota.Manager = &Manager{} |
| 49 | + |
| 50 | +// RedisClient is an interface that encompasses the various methods used by |
| 51 | +// this quota.Manager, and allows selecting among different Redis client |
| 52 | +// implementations (e.g. regular Redis, Redis Cluster, sharded, etc.) |
| 53 | +type RedisClient interface { |
| 54 | + // Everything required by the redistb.RedisClient interface |
| 55 | + redistb.RedisClient |
| 56 | +} |
| 57 | + |
| 58 | +// New returns a new Redis-based quota.Manager. |
| 59 | +func New(client RedisClient, opts ManagerOptions) *Manager { |
| 60 | + tb := redistb.New(client) |
| 61 | + return &Manager{tb: tb, opts: opts} |
| 62 | +} |
| 63 | + |
| 64 | +// GetTokens implements the quota.Manager API. |
| 65 | +func (m *Manager) GetTokens(ctx context.Context, numTokens int, specs []quota.Spec) error { |
| 66 | + for _, spec := range specs { |
| 67 | + if err := m.getTokensSingle(ctx, numTokens, spec); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (m *Manager) getTokensSingle(ctx context.Context, numTokens int, spec quota.Spec) error { |
| 76 | + capacity, rate := m.opts.Parameters(spec) |
| 77 | + |
| 78 | + // If we get back `MaxTokens` from our parameters call, this indicates |
| 79 | + // that there's no actual limit. We don't need to do anything to "get" |
| 80 | + // them; just ignore. |
| 81 | + if capacity == quota.MaxTokens { |
| 82 | + return nil |
| 83 | + } |
| 84 | + |
| 85 | + name := specName(m.opts.Prefix, spec) |
| 86 | + allowed, remaining, err := m.tb.Call( |
| 87 | + ctx, |
| 88 | + name, |
| 89 | + int64(capacity), |
| 90 | + rate, |
| 91 | + numTokens, |
| 92 | + ) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + if !allowed { |
| 97 | + return fmt.Errorf("insufficient tokens on %v (%v vs %v)", name, remaining, numTokens) |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// PeekTokens implements the quota.Manager API. |
| 104 | +func (m *Manager) PeekTokens(ctx context.Context, specs []quota.Spec) (map[quota.Spec]int, error) { |
| 105 | + tokens := make(map[quota.Spec]int) |
| 106 | + for _, spec := range specs { |
| 107 | + // Calling the limiter with 0 tokens requested is equivalent to |
| 108 | + // "peeking", but it will also shrink the token bucket if it |
| 109 | + // has too many tokens. |
| 110 | + capacity, rate := m.opts.Parameters(spec) |
| 111 | + |
| 112 | + // If we get back `MaxTokens` from our parameters call, this |
| 113 | + // indicates that there's no actual limit. We don't need to do |
| 114 | + // anything to "get" them; just set that value in the returned |
| 115 | + // map as well. |
| 116 | + if capacity == quota.MaxTokens { |
| 117 | + tokens[spec] = quota.MaxTokens |
| 118 | + continue |
| 119 | + } |
| 120 | + |
| 121 | + _, remaining, err := m.tb.Call( |
| 122 | + ctx, |
| 123 | + specName(m.opts.Prefix, spec), |
| 124 | + int64(capacity), |
| 125 | + rate, |
| 126 | + 0, |
| 127 | + ) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + |
| 132 | + tokens[spec] = int(remaining) |
| 133 | + } |
| 134 | + |
| 135 | + return tokens, nil |
| 136 | +} |
| 137 | + |
| 138 | +// PutTokens implements the quota.Manager API. |
| 139 | +func (m *Manager) PutTokens(ctx context.Context, numTokens int, specs []quota.Spec) error { |
| 140 | + // Putting tokens into a time-based quota doesn't mean anything (since |
| 141 | + // tokens are replenished at the moment they're requested) and since |
| 142 | + // that's the only supported mechanism for this package currently, do |
| 143 | + // nothing. |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +// ResetQuota implements the quota.Manager API. |
| 148 | +// |
| 149 | +// This function will reset every quota and return the first error encountered, |
| 150 | +// if any, but will continue trying to reset every quota even if an error is |
| 151 | +// encountered. |
| 152 | +func (m *Manager) ResetQuota(ctx context.Context, specs []quota.Spec) error { |
| 153 | + var firstErr error |
| 154 | + |
| 155 | + for _, name := range specNames(m.opts.Prefix, specs) { |
| 156 | + if err := m.tb.Reset(ctx, name); err != nil { |
| 157 | + if firstErr == nil { |
| 158 | + firstErr = err |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return firstErr |
| 164 | +} |
| 165 | + |
| 166 | +// Load attempts to load Redis scripts used by the Manager into the Redis |
| 167 | +// cluster. |
| 168 | +// |
| 169 | +// A Manager will operate successfully if this method is not called or fails, |
| 170 | +// but a successful Load will reduce bandwidth to/from the Redis cluster |
| 171 | +// substantially. |
| 172 | +func (m *Manager) Load(ctx context.Context) error { |
| 173 | + return m.tb.Load(ctx) |
| 174 | +} |
| 175 | + |
| 176 | +func specNames(prefix string, specs []quota.Spec) []string { |
| 177 | + names := make([]string, 0, len(specs)) |
| 178 | + for _, spec := range specs { |
| 179 | + names = append(names, specName(prefix, spec)) |
| 180 | + } |
| 181 | + return names |
| 182 | +} |
| 183 | + |
| 184 | +func specName(prefix string, spec quota.Spec) string { |
| 185 | + return prefix + "trillian/" + spec.Name() |
| 186 | +} |
0 commit comments