func (l *dbLimiter) allowed(ctx context.Context, tx *sql.Tx, lr limiterRequest) (bool, error) {
	if err := l.checkTableSizes(ctx, lr); err != nil {
		return false, errors.Wrap(err, "check table sizes")
	}
	allowed, err := l.checkWriterRates(ctx, tx, lr)
	if err != nil {
		return false, errors.Wrap(err, "check writer rates")
	}
	return allowed, nil
}
allowed gates writes to ctldb. it ensures that the tables are not too large, and also that the
specified writer has enough of a quota to make the writes.  Note that if there is any table in
the limiter request that has exceeded its max size, the entire request will be rejected.  This is
to prevent partial mutations being performed over and over due to the client retrying a failed
request that includes some tables that are not over their limits.
package executive
import (
	"context"
	"database/sql"
	"sync"
	"time"
	"github.com/pkg/errors"
	"github.com/segmentio/ctlstore/pkg/errs"
	"github.com/segmentio/ctlstore/pkg/limits"
	"github.com/segmentio/ctlstore/pkg/schema"
	"github.com/segmentio/ctlstore/pkg/utils"
	"github.com/segmentio/events/v2"
	"github.com/segmentio/stats/v4"
)
const (
	defaultRefreshPeriod        = 1 * time.Minute  
	defaultDeleteUsagePeriod    = 1 * time.Hour    
	defaultDeleteUsageOlderThan = 24 * time.Hour   
)
type (
	dbLimiter struct {
		db                 *sql.DB
		tableSizer         *tableSizer
		mut                sync.Mutex  
		defaultWriterLimit limits.RateLimit
		perWriterLimits    map[string]int64  
		timeFunc           func() time.Time
	}
	limiterRequest struct {
		writerName string
		familyName string
		requests   []ExecutiveMutationRequest
	}
)
func newDBLimiter(db *sql.DB, dbType string, defaultTableLimit limits.SizeLimits, writerLimitPeriod time.Duration, writerLimit int64) *dbLimiter {
	return &dbLimiter{
		db:                 db,
		tableSizer:         newTableSizer(db, dbType, defaultTableLimit, time.Minute),
		defaultWriterLimit: limits.RateLimit{Amount: writerLimit, Period: writerLimitPeriod},
		perWriterLimits:    make(map[string]int64),
	}
}