go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/examples/appengine/quotabeta/main.go (about) 1 // Copyright 2022 The LUCI Authors. 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 main contains a GAE app demonstrating how to use the server/quotabeta 16 // module to implement rate limiting for requests. Navigate to /rpcexplorer 17 // to try out quota operations. 18 // 19 // Not intended to be run locally. A local demo can be found under 20 // server/quotabeta/example. 21 package main 22 23 import ( 24 "go.chromium.org/luci/config/server/cfgmodule" 25 "go.chromium.org/luci/server" 26 "go.chromium.org/luci/server/module" 27 quota "go.chromium.org/luci/server/quotabeta" 28 quotapb "go.chromium.org/luci/server/quotabeta/proto" 29 "go.chromium.org/luci/server/quotabeta/quotaconfig" 30 "go.chromium.org/luci/server/redisconn" 31 32 pb "go.chromium.org/luci/examples/appengine/quotabeta/proto" 33 "go.chromium.org/luci/examples/appengine/quotabeta/rpc" 34 ) 35 36 func main() { 37 modules := []module.Module{ 38 cfgmodule.NewModuleFromFlags(), 39 quota.NewModuleFromFlags(), 40 redisconn.NewModuleFromFlags(), 41 } 42 43 server.Main(nil, modules, func(srv *server.Server) error { 44 // Initialize a static, in-memory implementation of quotaconfig.Interface. 45 // See the quota/rpc package for how these quotas are used. 46 // TODO(crbug/1280055): Fetch from the config service. 47 m, err := quotaconfig.NewMemory(srv.Context, []*quotapb.Policy{ 48 { 49 Name: "global-rate-limit", 50 Resources: 60, 51 Replenishment: 1, 52 }, 53 { 54 Name: "per-user-rate-limit/${user}", 55 Resources: 60, 56 Replenishment: 1, 57 }, 58 }) 59 if err != nil { 60 panic(err) 61 } 62 63 // Register the quotaconfig.Interface. 64 srv.Context = quota.Use(srv.Context, m) 65 66 // Register the demo RPC service. 67 pb.RegisterDemoServer(srv, rpc.New()) 68 return nil 69 }) 70 }