github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/server/etcd_quota_provider.go (about) 1 // Copyright 2018 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 server 16 17 import ( 18 "flag" 19 "fmt" 20 21 "github.com/golang/glog" 22 "github.com/google/trillian/quota" 23 "github.com/google/trillian/quota/cacheqm" 24 "github.com/google/trillian/quota/etcd/etcdqm" 25 "github.com/google/trillian/util/etcd" 26 ) 27 28 // QuotaEtcd represents the etcd quota implementation. 29 const QuotaEtcd = "etcd" 30 31 var ( 32 // EtcdServers is a flag containing the address(es) of etcd servers 33 EtcdServers = flag.String("etcd_servers", "", "A comma-separated list of etcd servers; no etcd registration if empty") 34 // TODO(Martin2112): suggested renaming these to etc_... to avoid clashes, but will it break existing deploys? 35 quotaMinBatchSize = flag.Int("quota_min_batch_size", cacheqm.DefaultMinBatchSize, "Minimum number of tokens to request from the quota system. "+ 36 "Zero or lower means batching is disabled. Applicable for etcd quotas.") 37 quotaMaxCacheEntries = flag.Int("quota_max_cache_entries", cacheqm.DefaultMaxCacheEntries, "Max number of quota specs in the quota cache. "+ 38 "Zero or lower means batching is disabled. Applicable for etcd quotas.") 39 ) 40 41 func init() { 42 if err := RegisterQuotaManager(QuotaEtcd, newEtcdQuotaManager); err != nil { 43 glog.Fatalf("Failed to register quota manager %v: %v", QuotaEtcd, err) 44 } 45 } 46 47 func newEtcdQuotaManager() (quota.Manager, error) { 48 if *EtcdServers == "" { 49 return nil, fmt.Errorf("Can't create etcd quotamanager - etcd_servers flag is unset") 50 } 51 client, err := etcd.NewClient(*EtcdServers) 52 if err != nil { 53 return nil, fmt.Errorf("Failed to connect to etcd at %v: %v", *EtcdServers, err) 54 } 55 56 qm := etcdqm.New(client) 57 if *quotaMinBatchSize > 0 && *quotaMaxCacheEntries > 0 { 58 cachedQM, err := cacheqm.NewCachedManager(qm, *quotaMinBatchSize, *quotaMaxCacheEntries) 59 if err != nil { 60 return nil, err 61 } 62 qm = cachedQM 63 } 64 glog.Info("Using Etcd QuotaManager") 65 return qm, nil 66 }