Skip to content

Commit

Permalink
Usage caps (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Wong committed Apr 1, 2022
1 parent 2b72a66 commit af07943
Show file tree
Hide file tree
Showing 22 changed files with 488 additions and 57 deletions.
4 changes: 4 additions & 0 deletions app/handler/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,10 @@ func (e *GoogleApp) DeleteData(ctx context.Context, request *adminpb.DeleteDataR
return nil
}

func (e *GoogleApp) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}

func (e *GoogleApp) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
log.Info("Received App.List request")

Expand Down
5 changes: 4 additions & 1 deletion cache/handler/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (c *Cache) Get(ctx context.Context, req *pb.GetRequest, rsp *pb.GetResponse
rsp.Ttl = 0
}


return nil
}

Expand Down Expand Up @@ -163,3 +162,7 @@ func (c *Cache) DeleteData(ctx context.Context, request *adminpb.DeleteDataReque
log.Infof("Deleted %d keys for %s", len(keys), request.TenantId)
return nil
}

func (c *Cache) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}
4 changes: 4 additions & 0 deletions comments/handler/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,7 @@ func (h *Comments) DeleteData(ctx context.Context, request *adminpb.DeleteDataRe
logger.Infof("Deleted %d keys for %s", len(keys), request.TenantId)
return nil
}

func (h *Comments) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}
4 changes: 4 additions & 0 deletions contact/handler/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,7 @@ func (c *contact) DeleteData(ctx context.Context, request *adminpb.DeleteDataReq
logger.Infof("Deleted %d keys for %s", len(keys), request.TenantId)
return nil
}

func (c *contact) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}
56 changes: 55 additions & 1 deletion db/handler/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const renameTableStmt = `ALTER TABLE "%v" RENAME TO "%v"`

var re = regexp.MustCompile("^[a-zA-Z0-9_]*$")
var c = cache.New(5*time.Minute, 10*time.Minute)
var usageCache = cache.New(30*time.Second, 10*time.Minute)

type Record struct {
ID string
Expand Down Expand Up @@ -91,7 +92,6 @@ func (e *Db) tableName(ctx context.Context, t string) (string, error) {
return tableName, nil
}

// Call is a single request handler called via client.Call or the generated client code
func (e *Db) Create(ctx context.Context, req *db.CreateRequest, rsp *db.CreateResponse) error {
if len(req.Record.AsMap()) == 0 {
return errors.BadRequest("db.create", "missing record")
Expand Down Expand Up @@ -484,3 +484,57 @@ func (e *Db) DeleteData(ctx context.Context, request *adminpb.DeleteDataRequest,
logger.Infof("Deleted %d tables for %s", dropCount, request.TenantId)
return nil
}

func (e *Db) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
method := "admin.Usage"
_, err := pauth.VerifyMicroAdmin(ctx, method)
if err != nil {
return err
}

if len(request.TenantId) < 10 { // deliberate length check so we don't grab all the things
return errors.BadRequest(method, "Missing tenant ID")
}

split := strings.Split(request.TenantId, "/")
tctx := tenant.NewContext(split[1], split[0], split[1])

tenantId := request.TenantId
tenantId = strings.Replace(strings.Replace(tenantId, "/", "_", -1), "-", "_", -1)

// Saving load on DB - do we have a cached response?
if ret, ok := usageCache.Get(tenantId); ok {
response.Usage = ret.(map[string]*adminpb.Usage)

return nil
}

db, err := e.GetDBConn(tctx)
if err != nil {
return err
}

var tables []string
if err := db.Table("information_schema.tables").Select("table_name").Where("table_schema = ?", "public").Find(&tables).Error; err != nil {
return err
}
var rowCount int64
for _, v := range tables {
if !strings.HasPrefix(v, tenantId) {
continue
}
var a int64
err = db.Table(v).Model(Record{}).Count(&a).Error
if err != nil {
return err
}
rowCount += a
}
response.Usage = map[string]*adminpb.Usage{
"Db.Create": &adminpb.Usage{Usage: rowCount, Units: "rows"},
// all other methods don't add rows so are not usage capped
}
usageCache.Set(tenantId, response.Usage, 0)
return nil

}
4 changes: 4 additions & 0 deletions file/handler/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,7 @@ func (e *File) DeleteData(ctx context.Context, request *adminpb.DeleteDataReques

return nil
}

func (e *File) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}
4 changes: 4 additions & 0 deletions function/handler/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,10 @@ func (e *GoogleFunction) DeleteData(ctx context.Context, request *adminpb.Delete
return nil
}

func (e *GoogleFunction) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}

var (
logsFuncMap = map[string]func(e *GoogleFunction, ctx context.Context, req *function.LogsRequest, rsp *function.LogsResponse) error{
"build": buildLogs, // TODO add runtime logs
Expand Down
4 changes: 4 additions & 0 deletions github/handler/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,7 @@ func (e *Github) DeleteData(ctx context.Context, request *adminpb.DeleteDataRequ

return nil
}

func (e *Github) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}
4 changes: 4 additions & 0 deletions image/handler/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,7 @@ func (e *Image) DeleteData(ctx context.Context, request *adminpb.DeleteDataReque

return nil
}

func (e *Image) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}
4 changes: 4 additions & 0 deletions lists/handler/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,7 @@ func (h *Lists) DeleteData(ctx context.Context, request *adminpb.DeleteDataReque
logger.Infof("Deleted %d keys for %s", len(keys), request.TenantId)
return nil
}

func (h *Lists) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}
4 changes: 4 additions & 0 deletions location/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ func (l *Location) DeleteData(ctx context.Context, request *adminpb.DeleteDataRe
logger.Infof("Deleted index for %s", request.TenantId)
return nil
}

func (l *Location) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}
4 changes: 4 additions & 0 deletions notes/handler/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,7 @@ func (h *Notes) DeleteData(ctx context.Context, request *adminpb.DeleteDataReque
logger.Infof("Deleted %d keys for %s", len(keys), request.TenantId)
return nil
}

func (h *Notes) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}
4 changes: 4 additions & 0 deletions otp/handler/otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,7 @@ func (e *Otp) DeleteData(ctx context.Context, request *adminpb.DeleteDataRequest
logger.Infof("Deleted %d keys for %s", len(keys), request.TenantId)
return nil
}

func (e *Otp) Usage(ctx context.Context, request *adminpb.UsageRequest, response *adminpb.UsageResponse) error {
return nil
}

0 comments on commit af07943

Please sign in to comment.