feat(rpm): RPM 限流模块优化

P0:
- rpm_override 嵌入 Auth Cache Snapshot,消除每请求 DB 查询 (snapshot v6→v7)
- 429 RPM 响应返回 Retry-After 头(当前分钟剩余秒数)

P1:
- ClearAll 按钮直连 DELETE API,带 loading 防重复
- 新增 GET /admin/users/:id/rpm-status 管理员 RPM 用量查询端点

优化:
- checkRPM 从级联互斥改为并行取最严,user.rpm_limit 作为全局硬上限始终生效
- Override/Group 变更后自动失效 auth cache
- fail-open 语义不变,Redis 故障不阻塞业务
This commit is contained in:
james-6-23
2026-04-23 03:33:52 +08:00
parent ef967d8f8a
commit dc5d42addc
79 changed files with 2831 additions and 140 deletions

View File

@@ -79,6 +79,8 @@ type Group struct {
DefaultMappedModel string `json:"default_mapped_model,omitempty"`
// OpenAI Messages 调度模型配置:按 Claude 系列/精确模型映射到目标 GPT 模型
MessagesDispatchModelConfig domain.OpenAIMessagesDispatchModelConfig `json:"messages_dispatch_model_config,omitempty"`
// 分组 RPM 上限0 表示不限制;设置后接管该分组用户的限流
RpmLimit int `json:"rpm_limit,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the GroupQuery when eager-loading is set.
Edges GroupEdges `json:"edges"`
@@ -191,7 +193,7 @@ func (*Group) scanValues(columns []string) ([]any, error) {
values[i] = new(sql.NullBool)
case group.FieldRateMultiplier, group.FieldDailyLimitUsd, group.FieldWeeklyLimitUsd, group.FieldMonthlyLimitUsd, group.FieldImagePrice1k, group.FieldImagePrice2k, group.FieldImagePrice4k:
values[i] = new(sql.NullFloat64)
case group.FieldID, group.FieldDefaultValidityDays, group.FieldFallbackGroupID, group.FieldFallbackGroupIDOnInvalidRequest, group.FieldSortOrder:
case group.FieldID, group.FieldDefaultValidityDays, group.FieldFallbackGroupID, group.FieldFallbackGroupIDOnInvalidRequest, group.FieldSortOrder, group.FieldRpmLimit:
values[i] = new(sql.NullInt64)
case group.FieldName, group.FieldDescription, group.FieldStatus, group.FieldPlatform, group.FieldSubscriptionType, group.FieldDefaultMappedModel:
values[i] = new(sql.NullString)
@@ -414,6 +416,12 @@ func (_m *Group) assignValues(columns []string, values []any) error {
return fmt.Errorf("unmarshal field messages_dispatch_model_config: %w", err)
}
}
case group.FieldRpmLimit:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field rpm_limit", values[i])
} else if value.Valid {
_m.RpmLimit = int(value.Int64)
}
default:
_m.selectValues.Set(columns[i], values[i])
}
@@ -599,6 +607,9 @@ func (_m *Group) String() string {
builder.WriteString(", ")
builder.WriteString("messages_dispatch_model_config=")
builder.WriteString(fmt.Sprintf("%v", _m.MessagesDispatchModelConfig))
builder.WriteString(", ")
builder.WriteString("rpm_limit=")
builder.WriteString(fmt.Sprintf("%v", _m.RpmLimit))
builder.WriteByte(')')
return builder.String()
}

View File

@@ -76,6 +76,8 @@ const (
FieldDefaultMappedModel = "default_mapped_model"
// FieldMessagesDispatchModelConfig holds the string denoting the messages_dispatch_model_config field in the database.
FieldMessagesDispatchModelConfig = "messages_dispatch_model_config"
// FieldRpmLimit holds the string denoting the rpm_limit field in the database.
FieldRpmLimit = "rpm_limit"
// EdgeAPIKeys holds the string denoting the api_keys edge name in mutations.
EdgeAPIKeys = "api_keys"
// EdgeRedeemCodes holds the string denoting the redeem_codes edge name in mutations.
@@ -181,6 +183,7 @@ var Columns = []string{
FieldRequirePrivacySet,
FieldDefaultMappedModel,
FieldMessagesDispatchModelConfig,
FieldRpmLimit,
}
var (
@@ -258,6 +261,8 @@ var (
DefaultMappedModelValidator func(string) error
// DefaultMessagesDispatchModelConfig holds the default value on creation for the "messages_dispatch_model_config" field.
DefaultMessagesDispatchModelConfig domain.OpenAIMessagesDispatchModelConfig
// DefaultRpmLimit holds the default value on creation for the "rpm_limit" field.
DefaultRpmLimit int
)
// OrderOption defines the ordering options for the Group queries.
@@ -403,6 +408,11 @@ func ByDefaultMappedModel(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldDefaultMappedModel, opts...).ToFunc()
}
// ByRpmLimit orders the results by the rpm_limit field.
func ByRpmLimit(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldRpmLimit, opts...).ToFunc()
}
// ByAPIKeysCount orders the results by api_keys count.
func ByAPIKeysCount(opts ...sql.OrderTermOption) OrderOption {
return func(s *sql.Selector) {

View File

@@ -190,6 +190,11 @@ func DefaultMappedModel(v string) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldDefaultMappedModel, v))
}
// RpmLimit applies equality check predicate on the "rpm_limit" field. It's identical to RpmLimitEQ.
func RpmLimit(v int) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldRpmLimit, v))
}
// CreatedAtEQ applies the EQ predicate on the "created_at" field.
func CreatedAtEQ(v time.Time) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldCreatedAt, v))
@@ -1320,6 +1325,46 @@ func DefaultMappedModelContainsFold(v string) predicate.Group {
return predicate.Group(sql.FieldContainsFold(FieldDefaultMappedModel, v))
}
// RpmLimitEQ applies the EQ predicate on the "rpm_limit" field.
func RpmLimitEQ(v int) predicate.Group {
return predicate.Group(sql.FieldEQ(FieldRpmLimit, v))
}
// RpmLimitNEQ applies the NEQ predicate on the "rpm_limit" field.
func RpmLimitNEQ(v int) predicate.Group {
return predicate.Group(sql.FieldNEQ(FieldRpmLimit, v))
}
// RpmLimitIn applies the In predicate on the "rpm_limit" field.
func RpmLimitIn(vs ...int) predicate.Group {
return predicate.Group(sql.FieldIn(FieldRpmLimit, vs...))
}
// RpmLimitNotIn applies the NotIn predicate on the "rpm_limit" field.
func RpmLimitNotIn(vs ...int) predicate.Group {
return predicate.Group(sql.FieldNotIn(FieldRpmLimit, vs...))
}
// RpmLimitGT applies the GT predicate on the "rpm_limit" field.
func RpmLimitGT(v int) predicate.Group {
return predicate.Group(sql.FieldGT(FieldRpmLimit, v))
}
// RpmLimitGTE applies the GTE predicate on the "rpm_limit" field.
func RpmLimitGTE(v int) predicate.Group {
return predicate.Group(sql.FieldGTE(FieldRpmLimit, v))
}
// RpmLimitLT applies the LT predicate on the "rpm_limit" field.
func RpmLimitLT(v int) predicate.Group {
return predicate.Group(sql.FieldLT(FieldRpmLimit, v))
}
// RpmLimitLTE applies the LTE predicate on the "rpm_limit" field.
func RpmLimitLTE(v int) predicate.Group {
return predicate.Group(sql.FieldLTE(FieldRpmLimit, v))
}
// HasAPIKeys applies the HasEdge predicate on the "api_keys" edge.
func HasAPIKeys() predicate.Group {
return predicate.Group(func(s *sql.Selector) {

View File

@@ -425,6 +425,20 @@ func (_c *GroupCreate) SetNillableMessagesDispatchModelConfig(v *domain.OpenAIMe
return _c
}
// SetRpmLimit sets the "rpm_limit" field.
func (_c *GroupCreate) SetRpmLimit(v int) *GroupCreate {
_c.mutation.SetRpmLimit(v)
return _c
}
// SetNillableRpmLimit sets the "rpm_limit" field if the given value is not nil.
func (_c *GroupCreate) SetNillableRpmLimit(v *int) *GroupCreate {
if v != nil {
_c.SetRpmLimit(*v)
}
return _c
}
// AddAPIKeyIDs adds the "api_keys" edge to the APIKey entity by IDs.
func (_c *GroupCreate) AddAPIKeyIDs(ids ...int64) *GroupCreate {
_c.mutation.AddAPIKeyIDs(ids...)
@@ -630,6 +644,10 @@ func (_c *GroupCreate) defaults() error {
v := group.DefaultMessagesDispatchModelConfig
_c.mutation.SetMessagesDispatchModelConfig(v)
}
if _, ok := _c.mutation.RpmLimit(); !ok {
v := group.DefaultRpmLimit
_c.mutation.SetRpmLimit(v)
}
return nil
}
@@ -717,6 +735,9 @@ func (_c *GroupCreate) check() error {
if _, ok := _c.mutation.MessagesDispatchModelConfig(); !ok {
return &ValidationError{Name: "messages_dispatch_model_config", err: errors.New(`ent: missing required field "Group.messages_dispatch_model_config"`)}
}
if _, ok := _c.mutation.RpmLimit(); !ok {
return &ValidationError{Name: "rpm_limit", err: errors.New(`ent: missing required field "Group.rpm_limit"`)}
}
return nil
}
@@ -864,6 +885,10 @@ func (_c *GroupCreate) createSpec() (*Group, *sqlgraph.CreateSpec) {
_spec.SetField(group.FieldMessagesDispatchModelConfig, field.TypeJSON, value)
_node.MessagesDispatchModelConfig = value
}
if value, ok := _c.mutation.RpmLimit(); ok {
_spec.SetField(group.FieldRpmLimit, field.TypeInt, value)
_node.RpmLimit = value
}
if nodes := _c.mutation.APIKeysIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
@@ -1500,6 +1525,24 @@ func (u *GroupUpsert) UpdateMessagesDispatchModelConfig() *GroupUpsert {
return u
}
// SetRpmLimit sets the "rpm_limit" field.
func (u *GroupUpsert) SetRpmLimit(v int) *GroupUpsert {
u.Set(group.FieldRpmLimit, v)
return u
}
// UpdateRpmLimit sets the "rpm_limit" field to the value that was provided on create.
func (u *GroupUpsert) UpdateRpmLimit() *GroupUpsert {
u.SetExcluded(group.FieldRpmLimit)
return u
}
// AddRpmLimit adds v to the "rpm_limit" field.
func (u *GroupUpsert) AddRpmLimit(v int) *GroupUpsert {
u.Add(group.FieldRpmLimit, v)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
@@ -2105,6 +2148,27 @@ func (u *GroupUpsertOne) UpdateMessagesDispatchModelConfig() *GroupUpsertOne {
})
}
// SetRpmLimit sets the "rpm_limit" field.
func (u *GroupUpsertOne) SetRpmLimit(v int) *GroupUpsertOne {
return u.Update(func(s *GroupUpsert) {
s.SetRpmLimit(v)
})
}
// AddRpmLimit adds v to the "rpm_limit" field.
func (u *GroupUpsertOne) AddRpmLimit(v int) *GroupUpsertOne {
return u.Update(func(s *GroupUpsert) {
s.AddRpmLimit(v)
})
}
// UpdateRpmLimit sets the "rpm_limit" field to the value that was provided on create.
func (u *GroupUpsertOne) UpdateRpmLimit() *GroupUpsertOne {
return u.Update(func(s *GroupUpsert) {
s.UpdateRpmLimit()
})
}
// Exec executes the query.
func (u *GroupUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
@@ -2876,6 +2940,27 @@ func (u *GroupUpsertBulk) UpdateMessagesDispatchModelConfig() *GroupUpsertBulk {
})
}
// SetRpmLimit sets the "rpm_limit" field.
func (u *GroupUpsertBulk) SetRpmLimit(v int) *GroupUpsertBulk {
return u.Update(func(s *GroupUpsert) {
s.SetRpmLimit(v)
})
}
// AddRpmLimit adds v to the "rpm_limit" field.
func (u *GroupUpsertBulk) AddRpmLimit(v int) *GroupUpsertBulk {
return u.Update(func(s *GroupUpsert) {
s.AddRpmLimit(v)
})
}
// UpdateRpmLimit sets the "rpm_limit" field to the value that was provided on create.
func (u *GroupUpsertBulk) UpdateRpmLimit() *GroupUpsertBulk {
return u.Update(func(s *GroupUpsert) {
s.UpdateRpmLimit()
})
}
// Exec executes the query.
func (u *GroupUpsertBulk) Exec(ctx context.Context) error {
if u.create.err != nil {

View File

@@ -567,6 +567,27 @@ func (_u *GroupUpdate) SetNillableMessagesDispatchModelConfig(v *domain.OpenAIMe
return _u
}
// SetRpmLimit sets the "rpm_limit" field.
func (_u *GroupUpdate) SetRpmLimit(v int) *GroupUpdate {
_u.mutation.ResetRpmLimit()
_u.mutation.SetRpmLimit(v)
return _u
}
// SetNillableRpmLimit sets the "rpm_limit" field if the given value is not nil.
func (_u *GroupUpdate) SetNillableRpmLimit(v *int) *GroupUpdate {
if v != nil {
_u.SetRpmLimit(*v)
}
return _u
}
// AddRpmLimit adds value to the "rpm_limit" field.
func (_u *GroupUpdate) AddRpmLimit(v int) *GroupUpdate {
_u.mutation.AddRpmLimit(v)
return _u
}
// AddAPIKeyIDs adds the "api_keys" edge to the APIKey entity by IDs.
func (_u *GroupUpdate) AddAPIKeyIDs(ids ...int64) *GroupUpdate {
_u.mutation.AddAPIKeyIDs(ids...)
@@ -1030,6 +1051,12 @@ func (_u *GroupUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if value, ok := _u.mutation.MessagesDispatchModelConfig(); ok {
_spec.SetField(group.FieldMessagesDispatchModelConfig, field.TypeJSON, value)
}
if value, ok := _u.mutation.RpmLimit(); ok {
_spec.SetField(group.FieldRpmLimit, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedRpmLimit(); ok {
_spec.AddField(group.FieldRpmLimit, field.TypeInt, value)
}
if _u.mutation.APIKeysCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
@@ -1875,6 +1902,27 @@ func (_u *GroupUpdateOne) SetNillableMessagesDispatchModelConfig(v *domain.OpenA
return _u
}
// SetRpmLimit sets the "rpm_limit" field.
func (_u *GroupUpdateOne) SetRpmLimit(v int) *GroupUpdateOne {
_u.mutation.ResetRpmLimit()
_u.mutation.SetRpmLimit(v)
return _u
}
// SetNillableRpmLimit sets the "rpm_limit" field if the given value is not nil.
func (_u *GroupUpdateOne) SetNillableRpmLimit(v *int) *GroupUpdateOne {
if v != nil {
_u.SetRpmLimit(*v)
}
return _u
}
// AddRpmLimit adds value to the "rpm_limit" field.
func (_u *GroupUpdateOne) AddRpmLimit(v int) *GroupUpdateOne {
_u.mutation.AddRpmLimit(v)
return _u
}
// AddAPIKeyIDs adds the "api_keys" edge to the APIKey entity by IDs.
func (_u *GroupUpdateOne) AddAPIKeyIDs(ids ...int64) *GroupUpdateOne {
_u.mutation.AddAPIKeyIDs(ids...)
@@ -2368,6 +2416,12 @@ func (_u *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error)
if value, ok := _u.mutation.MessagesDispatchModelConfig(); ok {
_spec.SetField(group.FieldMessagesDispatchModelConfig, field.TypeJSON, value)
}
if value, ok := _u.mutation.RpmLimit(); ok {
_spec.SetField(group.FieldRpmLimit, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedRpmLimit(); ok {
_spec.AddField(group.FieldRpmLimit, field.TypeInt, value)
}
if _u.mutation.APIKeysCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,

View File

@@ -491,6 +491,7 @@ var (
{Name: "require_privacy_set", Type: field.TypeBool, Default: false},
{Name: "default_mapped_model", Type: field.TypeString, Size: 100, Default: ""},
{Name: "messages_dispatch_model_config", Type: field.TypeJSON, SchemaType: map[string]string{"postgres": "jsonb"}},
{Name: "rpm_limit", Type: field.TypeInt, Default: 0},
}
// GroupsTable holds the schema information for the "groups" table.
GroupsTable = &schema.Table{
@@ -1276,7 +1277,7 @@ var (
{Name: "totp_secret_encrypted", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "text"}},
{Name: "totp_enabled", Type: field.TypeBool, Default: false},
{Name: "totp_enabled_at", Type: field.TypeTime, Nullable: true},
{Name: "signup_source", Type: field.TypeString, Size: 20, Default: "email"},
{Name: "signup_source", Type: field.TypeString, Default: "email"},
{Name: "last_login_at", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "last_active_at", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"postgres": "timestamptz"}},
{Name: "balance_notify_enabled", Type: field.TypeBool, Default: true},
@@ -1284,6 +1285,7 @@ var (
{Name: "balance_notify_threshold", Type: field.TypeFloat64, Nullable: true, SchemaType: map[string]string{"postgres": "decimal(20,8)"}},
{Name: "balance_notify_extra_emails", Type: field.TypeString, Default: "[]", SchemaType: map[string]string{"postgres": "text"}},
{Name: "total_recharged", Type: field.TypeFloat64, Default: 0, SchemaType: map[string]string{"postgres": "decimal(20,8)"}},
{Name: "rpm_limit", Type: field.TypeInt, Default: 0},
}
// UsersTable holds the schema information for the "users" table.
UsersTable = &schema.Table{

View File

@@ -10102,6 +10102,8 @@ type GroupMutation struct {
require_privacy_set *bool
default_mapped_model *string
messages_dispatch_model_config *domain.OpenAIMessagesDispatchModelConfig
rpm_limit *int
addrpm_limit *int
clearedFields map[string]struct{}
api_keys map[int64]struct{}
removedapi_keys map[int64]struct{}
@@ -11690,6 +11692,62 @@ func (m *GroupMutation) ResetMessagesDispatchModelConfig() {
m.messages_dispatch_model_config = nil
}
// SetRpmLimit sets the "rpm_limit" field.
func (m *GroupMutation) SetRpmLimit(i int) {
m.rpm_limit = &i
m.addrpm_limit = nil
}
// RpmLimit returns the value of the "rpm_limit" field in the mutation.
func (m *GroupMutation) RpmLimit() (r int, exists bool) {
v := m.rpm_limit
if v == nil {
return
}
return *v, true
}
// OldRpmLimit returns the old "rpm_limit" field's value of the Group entity.
// If the Group object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *GroupMutation) OldRpmLimit(ctx context.Context) (v int, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldRpmLimit is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldRpmLimit requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldRpmLimit: %w", err)
}
return oldValue.RpmLimit, nil
}
// AddRpmLimit adds i to the "rpm_limit" field.
func (m *GroupMutation) AddRpmLimit(i int) {
if m.addrpm_limit != nil {
*m.addrpm_limit += i
} else {
m.addrpm_limit = &i
}
}
// AddedRpmLimit returns the value that was added to the "rpm_limit" field in this mutation.
func (m *GroupMutation) AddedRpmLimit() (r int, exists bool) {
v := m.addrpm_limit
if v == nil {
return
}
return *v, true
}
// ResetRpmLimit resets all changes to the "rpm_limit" field.
func (m *GroupMutation) ResetRpmLimit() {
m.rpm_limit = nil
m.addrpm_limit = nil
}
// AddAPIKeyIDs adds the "api_keys" edge to the APIKey entity by ids.
func (m *GroupMutation) AddAPIKeyIDs(ids ...int64) {
if m.api_keys == nil {
@@ -12048,7 +12106,7 @@ func (m *GroupMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *GroupMutation) Fields() []string {
fields := make([]string, 0, 30)
fields := make([]string, 0, 31)
if m.created_at != nil {
fields = append(fields, group.FieldCreatedAt)
}
@@ -12139,6 +12197,9 @@ func (m *GroupMutation) Fields() []string {
if m.messages_dispatch_model_config != nil {
fields = append(fields, group.FieldMessagesDispatchModelConfig)
}
if m.rpm_limit != nil {
fields = append(fields, group.FieldRpmLimit)
}
return fields
}
@@ -12207,6 +12268,8 @@ func (m *GroupMutation) Field(name string) (ent.Value, bool) {
return m.DefaultMappedModel()
case group.FieldMessagesDispatchModelConfig:
return m.MessagesDispatchModelConfig()
case group.FieldRpmLimit:
return m.RpmLimit()
}
return nil, false
}
@@ -12276,6 +12339,8 @@ func (m *GroupMutation) OldField(ctx context.Context, name string) (ent.Value, e
return m.OldDefaultMappedModel(ctx)
case group.FieldMessagesDispatchModelConfig:
return m.OldMessagesDispatchModelConfig(ctx)
case group.FieldRpmLimit:
return m.OldRpmLimit(ctx)
}
return nil, fmt.Errorf("unknown Group field %s", name)
}
@@ -12495,6 +12560,13 @@ func (m *GroupMutation) SetField(name string, value ent.Value) error {
}
m.SetMessagesDispatchModelConfig(v)
return nil
case group.FieldRpmLimit:
v, ok := value.(int)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetRpmLimit(v)
return nil
}
return fmt.Errorf("unknown Group field %s", name)
}
@@ -12536,6 +12608,9 @@ func (m *GroupMutation) AddedFields() []string {
if m.addsort_order != nil {
fields = append(fields, group.FieldSortOrder)
}
if m.addrpm_limit != nil {
fields = append(fields, group.FieldRpmLimit)
}
return fields
}
@@ -12566,6 +12641,8 @@ func (m *GroupMutation) AddedField(name string) (ent.Value, bool) {
return m.AddedFallbackGroupIDOnInvalidRequest()
case group.FieldSortOrder:
return m.AddedSortOrder()
case group.FieldRpmLimit:
return m.AddedRpmLimit()
}
return nil, false
}
@@ -12652,6 +12729,13 @@ func (m *GroupMutation) AddField(name string, value ent.Value) error {
}
m.AddSortOrder(v)
return nil
case group.FieldRpmLimit:
v, ok := value.(int)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.AddRpmLimit(v)
return nil
}
return fmt.Errorf("unknown Group numeric field %s", name)
}
@@ -12838,6 +12922,9 @@ func (m *GroupMutation) ResetField(name string) error {
case group.FieldMessagesDispatchModelConfig:
m.ResetMessagesDispatchModelConfig()
return nil
case group.FieldRpmLimit:
m.ResetRpmLimit()
return nil
}
return fmt.Errorf("unknown Group field %s", name)
}
@@ -32681,6 +32768,8 @@ type UserMutation struct {
balance_notify_extra_emails *string
total_recharged *float64
addtotal_recharged *float64
rpm_limit *int
addrpm_limit *int
clearedFields map[string]struct{}
api_keys map[int64]struct{}
removedapi_keys map[int64]struct{}
@@ -33772,6 +33861,62 @@ func (m *UserMutation) ResetTotalRecharged() {
m.addtotal_recharged = nil
}
// SetRpmLimit sets the "rpm_limit" field.
func (m *UserMutation) SetRpmLimit(i int) {
m.rpm_limit = &i
m.addrpm_limit = nil
}
// RpmLimit returns the value of the "rpm_limit" field in the mutation.
func (m *UserMutation) RpmLimit() (r int, exists bool) {
v := m.rpm_limit
if v == nil {
return
}
return *v, true
}
// OldRpmLimit returns the old "rpm_limit" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldRpmLimit(ctx context.Context) (v int, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldRpmLimit is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldRpmLimit requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldRpmLimit: %w", err)
}
return oldValue.RpmLimit, nil
}
// AddRpmLimit adds i to the "rpm_limit" field.
func (m *UserMutation) AddRpmLimit(i int) {
if m.addrpm_limit != nil {
*m.addrpm_limit += i
} else {
m.addrpm_limit = &i
}
}
// AddedRpmLimit returns the value that was added to the "rpm_limit" field in this mutation.
func (m *UserMutation) AddedRpmLimit() (r int, exists bool) {
v := m.addrpm_limit
if v == nil {
return
}
return *v, true
}
// ResetRpmLimit resets all changes to the "rpm_limit" field.
func (m *UserMutation) ResetRpmLimit() {
m.rpm_limit = nil
m.addrpm_limit = nil
}
// AddAPIKeyIDs adds the "api_keys" edge to the APIKey entity by ids.
func (m *UserMutation) AddAPIKeyIDs(ids ...int64) {
if m.api_keys == nil {
@@ -34454,7 +34599,7 @@ func (m *UserMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *UserMutation) Fields() []string {
fields := make([]string, 0, 22)
fields := make([]string, 0, 23)
if m.created_at != nil {
fields = append(fields, user.FieldCreatedAt)
}
@@ -34521,6 +34666,9 @@ func (m *UserMutation) Fields() []string {
if m.total_recharged != nil {
fields = append(fields, user.FieldTotalRecharged)
}
if m.rpm_limit != nil {
fields = append(fields, user.FieldRpmLimit)
}
return fields
}
@@ -34573,6 +34721,8 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) {
return m.BalanceNotifyExtraEmails()
case user.FieldTotalRecharged:
return m.TotalRecharged()
case user.FieldRpmLimit:
return m.RpmLimit()
}
return nil, false
}
@@ -34626,6 +34776,8 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er
return m.OldBalanceNotifyExtraEmails(ctx)
case user.FieldTotalRecharged:
return m.OldTotalRecharged(ctx)
case user.FieldRpmLimit:
return m.OldRpmLimit(ctx)
}
return nil, fmt.Errorf("unknown User field %s", name)
}
@@ -34789,6 +34941,13 @@ func (m *UserMutation) SetField(name string, value ent.Value) error {
}
m.SetTotalRecharged(v)
return nil
case user.FieldRpmLimit:
v, ok := value.(int)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetRpmLimit(v)
return nil
}
return fmt.Errorf("unknown User field %s", name)
}
@@ -34809,6 +34968,9 @@ func (m *UserMutation) AddedFields() []string {
if m.addtotal_recharged != nil {
fields = append(fields, user.FieldTotalRecharged)
}
if m.addrpm_limit != nil {
fields = append(fields, user.FieldRpmLimit)
}
return fields
}
@@ -34825,6 +34987,8 @@ func (m *UserMutation) AddedField(name string) (ent.Value, bool) {
return m.AddedBalanceNotifyThreshold()
case user.FieldTotalRecharged:
return m.AddedTotalRecharged()
case user.FieldRpmLimit:
return m.AddedRpmLimit()
}
return nil, false
}
@@ -34862,6 +35026,13 @@ func (m *UserMutation) AddField(name string, value ent.Value) error {
}
m.AddTotalRecharged(v)
return nil
case user.FieldRpmLimit:
v, ok := value.(int)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.AddRpmLimit(v)
return nil
}
return fmt.Errorf("unknown User numeric field %s", name)
}
@@ -34994,6 +35165,9 @@ func (m *UserMutation) ResetField(name string) error {
case user.FieldTotalRecharged:
m.ResetTotalRecharged()
return nil
case user.FieldRpmLimit:
m.ResetRpmLimit()
return nil
}
return fmt.Errorf("unknown User field %s", name)
}

View File

@@ -595,6 +595,10 @@ func init() {
groupDescMessagesDispatchModelConfig := groupFields[26].Descriptor()
// group.DefaultMessagesDispatchModelConfig holds the default value on creation for the messages_dispatch_model_config field.
group.DefaultMessagesDispatchModelConfig = groupDescMessagesDispatchModelConfig.Default.(domain.OpenAIMessagesDispatchModelConfig)
// groupDescRpmLimit is the schema descriptor for rpm_limit field.
groupDescRpmLimit := groupFields[27].Descriptor()
// group.DefaultRpmLimit holds the default value on creation for the rpm_limit field.
group.DefaultRpmLimit = groupDescRpmLimit.Default.(int)
idempotencyrecordMixin := schema.IdempotencyRecord{}.Mixin()
idempotencyrecordMixinFields0 := idempotencyrecordMixin[0].Fields()
_ = idempotencyrecordMixinFields0
@@ -1575,6 +1579,10 @@ func init() {
userDescTotalRecharged := userFields[18].Descriptor()
// user.DefaultTotalRecharged holds the default value on creation for the total_recharged field.
user.DefaultTotalRecharged = userDescTotalRecharged.Default.(float64)
// userDescRpmLimit is the schema descriptor for rpm_limit field.
userDescRpmLimit := userFields[19].Descriptor()
// user.DefaultRpmLimit holds the default value on creation for the rpm_limit field.
user.DefaultRpmLimit = userDescRpmLimit.Default.(int)
userallowedgroupFields := schema.UserAllowedGroup{}.Fields()
_ = userallowedgroupFields
// userallowedgroupDescCreatedAt is the schema descriptor for created_at field.

View File

@@ -145,6 +145,11 @@ func (Group) Fields() []ent.Field {
Default(domain.OpenAIMessagesDispatchModelConfig{}).
SchemaType(map[string]string{dialect.Postgres: "jsonb"}).
Comment("OpenAI Messages 调度模型配置:按 Claude 系列/精确模型映射到目标 GPT 模型"),
// 分组级每分钟请求数上限0 = 不限制)。设置后优先于用户级兜底生效。
field.Int("rpm_limit").
Default(0).
Comment("分组 RPM 上限0 表示不限制;设置后接管该分组用户的限流"),
}
}

View File

@@ -108,6 +108,10 @@ func (User) Fields() []ent.Field {
field.Float("total_recharged").
SchemaType(map[string]string{dialect.Postgres: "decimal(20,8)"}).
Default(0),
// 用户级每分钟请求数上限0 = 不限制)。仅当所在分组未设置 rpm_limit 时作为兜底生效。
field.Int("rpm_limit").
Default(0),
}
}

View File

@@ -61,6 +61,8 @@ type User struct {
BalanceNotifyExtraEmails string `json:"balance_notify_extra_emails,omitempty"`
// TotalRecharged holds the value of the "total_recharged" field.
TotalRecharged float64 `json:"total_recharged,omitempty"`
// RpmLimit holds the value of the "rpm_limit" field.
RpmLimit int `json:"rpm_limit,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the UserQuery when eager-loading is set.
Edges UserEdges `json:"edges"`
@@ -226,7 +228,7 @@ func (*User) scanValues(columns []string) ([]any, error) {
values[i] = new(sql.NullBool)
case user.FieldBalance, user.FieldBalanceNotifyThreshold, user.FieldTotalRecharged:
values[i] = new(sql.NullFloat64)
case user.FieldID, user.FieldConcurrency:
case user.FieldID, user.FieldConcurrency, user.FieldRpmLimit:
values[i] = new(sql.NullInt64)
case user.FieldEmail, user.FieldPasswordHash, user.FieldRole, user.FieldStatus, user.FieldUsername, user.FieldNotes, user.FieldTotpSecretEncrypted, user.FieldSignupSource, user.FieldBalanceNotifyThresholdType, user.FieldBalanceNotifyExtraEmails:
values[i] = new(sql.NullString)
@@ -391,6 +393,12 @@ func (_m *User) assignValues(columns []string, values []any) error {
} else if value.Valid {
_m.TotalRecharged = value.Float64
}
case user.FieldRpmLimit:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field rpm_limit", values[i])
} else if value.Valid {
_m.RpmLimit = int(value.Int64)
}
default:
_m.selectValues.Set(columns[i], values[i])
}
@@ -569,6 +577,9 @@ func (_m *User) String() string {
builder.WriteString(", ")
builder.WriteString("total_recharged=")
builder.WriteString(fmt.Sprintf("%v", _m.TotalRecharged))
builder.WriteString(", ")
builder.WriteString("rpm_limit=")
builder.WriteString(fmt.Sprintf("%v", _m.RpmLimit))
builder.WriteByte(')')
return builder.String()
}

View File

@@ -59,6 +59,8 @@ const (
FieldBalanceNotifyExtraEmails = "balance_notify_extra_emails"
// FieldTotalRecharged holds the string denoting the total_recharged field in the database.
FieldTotalRecharged = "total_recharged"
// FieldRpmLimit holds the string denoting the rpm_limit field in the database.
FieldRpmLimit = "rpm_limit"
// EdgeAPIKeys holds the string denoting the api_keys edge name in mutations.
EdgeAPIKeys = "api_keys"
// EdgeRedeemCodes holds the string denoting the redeem_codes edge name in mutations.
@@ -203,6 +205,7 @@ var Columns = []string{
FieldBalanceNotifyThreshold,
FieldBalanceNotifyExtraEmails,
FieldTotalRecharged,
FieldRpmLimit,
}
var (
@@ -271,6 +274,8 @@ var (
DefaultBalanceNotifyExtraEmails string
// DefaultTotalRecharged holds the default value on creation for the "total_recharged" field.
DefaultTotalRecharged float64
// DefaultRpmLimit holds the default value on creation for the "rpm_limit" field.
DefaultRpmLimit int
)
// OrderOption defines the ordering options for the User queries.
@@ -391,6 +396,11 @@ func ByTotalRecharged(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldTotalRecharged, opts...).ToFunc()
}
// ByRpmLimit orders the results by the rpm_limit field.
func ByRpmLimit(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldRpmLimit, opts...).ToFunc()
}
// ByAPIKeysCount orders the results by api_keys count.
func ByAPIKeysCount(opts ...sql.OrderTermOption) OrderOption {
return func(s *sql.Selector) {

View File

@@ -165,6 +165,11 @@ func TotalRecharged(v float64) predicate.User {
return predicate.User(sql.FieldEQ(FieldTotalRecharged, v))
}
// RpmLimit applies equality check predicate on the "rpm_limit" field. It's identical to RpmLimitEQ.
func RpmLimit(v int) predicate.User {
return predicate.User(sql.FieldEQ(FieldRpmLimit, v))
}
// CreatedAtEQ applies the EQ predicate on the "created_at" field.
func CreatedAtEQ(v time.Time) predicate.User {
return predicate.User(sql.FieldEQ(FieldCreatedAt, v))
@@ -1295,6 +1300,46 @@ func TotalRechargedLTE(v float64) predicate.User {
return predicate.User(sql.FieldLTE(FieldTotalRecharged, v))
}
// RpmLimitEQ applies the EQ predicate on the "rpm_limit" field.
func RpmLimitEQ(v int) predicate.User {
return predicate.User(sql.FieldEQ(FieldRpmLimit, v))
}
// RpmLimitNEQ applies the NEQ predicate on the "rpm_limit" field.
func RpmLimitNEQ(v int) predicate.User {
return predicate.User(sql.FieldNEQ(FieldRpmLimit, v))
}
// RpmLimitIn applies the In predicate on the "rpm_limit" field.
func RpmLimitIn(vs ...int) predicate.User {
return predicate.User(sql.FieldIn(FieldRpmLimit, vs...))
}
// RpmLimitNotIn applies the NotIn predicate on the "rpm_limit" field.
func RpmLimitNotIn(vs ...int) predicate.User {
return predicate.User(sql.FieldNotIn(FieldRpmLimit, vs...))
}
// RpmLimitGT applies the GT predicate on the "rpm_limit" field.
func RpmLimitGT(v int) predicate.User {
return predicate.User(sql.FieldGT(FieldRpmLimit, v))
}
// RpmLimitGTE applies the GTE predicate on the "rpm_limit" field.
func RpmLimitGTE(v int) predicate.User {
return predicate.User(sql.FieldGTE(FieldRpmLimit, v))
}
// RpmLimitLT applies the LT predicate on the "rpm_limit" field.
func RpmLimitLT(v int) predicate.User {
return predicate.User(sql.FieldLT(FieldRpmLimit, v))
}
// RpmLimitLTE applies the LTE predicate on the "rpm_limit" field.
func RpmLimitLTE(v int) predicate.User {
return predicate.User(sql.FieldLTE(FieldRpmLimit, v))
}
// HasAPIKeys applies the HasEdge predicate on the "api_keys" edge.
func HasAPIKeys() predicate.User {
return predicate.User(func(s *sql.Selector) {

View File

@@ -325,6 +325,20 @@ func (_c *UserCreate) SetNillableTotalRecharged(v *float64) *UserCreate {
return _c
}
// SetRpmLimit sets the "rpm_limit" field.
func (_c *UserCreate) SetRpmLimit(v int) *UserCreate {
_c.mutation.SetRpmLimit(v)
return _c
}
// SetNillableRpmLimit sets the "rpm_limit" field if the given value is not nil.
func (_c *UserCreate) SetNillableRpmLimit(v *int) *UserCreate {
if v != nil {
_c.SetRpmLimit(*v)
}
return _c
}
// AddAPIKeyIDs adds the "api_keys" edge to the APIKey entity by IDs.
func (_c *UserCreate) AddAPIKeyIDs(ids ...int64) *UserCreate {
_c.mutation.AddAPIKeyIDs(ids...)
@@ -604,6 +618,10 @@ func (_c *UserCreate) defaults() error {
v := user.DefaultTotalRecharged
_c.mutation.SetTotalRecharged(v)
}
if _, ok := _c.mutation.RpmLimit(); !ok {
v := user.DefaultRpmLimit
_c.mutation.SetRpmLimit(v)
}
return nil
}
@@ -687,6 +705,9 @@ func (_c *UserCreate) check() error {
if _, ok := _c.mutation.TotalRecharged(); !ok {
return &ValidationError{Name: "total_recharged", err: errors.New(`ent: missing required field "User.total_recharged"`)}
}
if _, ok := _c.mutation.RpmLimit(); !ok {
return &ValidationError{Name: "rpm_limit", err: errors.New(`ent: missing required field "User.rpm_limit"`)}
}
return nil
}
@@ -802,6 +823,10 @@ func (_c *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
_spec.SetField(user.FieldTotalRecharged, field.TypeFloat64, value)
_node.TotalRecharged = value
}
if value, ok := _c.mutation.RpmLimit(); ok {
_spec.SetField(user.FieldRpmLimit, field.TypeInt, value)
_node.RpmLimit = value
}
if nodes := _c.mutation.APIKeysIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
@@ -1362,6 +1387,24 @@ func (u *UserUpsert) AddTotalRecharged(v float64) *UserUpsert {
return u
}
// SetRpmLimit sets the "rpm_limit" field.
func (u *UserUpsert) SetRpmLimit(v int) *UserUpsert {
u.Set(user.FieldRpmLimit, v)
return u
}
// UpdateRpmLimit sets the "rpm_limit" field to the value that was provided on create.
func (u *UserUpsert) UpdateRpmLimit() *UserUpsert {
u.SetExcluded(user.FieldRpmLimit)
return u
}
// AddRpmLimit adds v to the "rpm_limit" field.
func (u *UserUpsert) AddRpmLimit(v int) *UserUpsert {
u.Add(user.FieldRpmLimit, v)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
@@ -1771,6 +1814,27 @@ func (u *UserUpsertOne) UpdateTotalRecharged() *UserUpsertOne {
})
}
// SetRpmLimit sets the "rpm_limit" field.
func (u *UserUpsertOne) SetRpmLimit(v int) *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.SetRpmLimit(v)
})
}
// AddRpmLimit adds v to the "rpm_limit" field.
func (u *UserUpsertOne) AddRpmLimit(v int) *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.AddRpmLimit(v)
})
}
// UpdateRpmLimit sets the "rpm_limit" field to the value that was provided on create.
func (u *UserUpsertOne) UpdateRpmLimit() *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.UpdateRpmLimit()
})
}
// Exec executes the query.
func (u *UserUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
@@ -2346,6 +2410,27 @@ func (u *UserUpsertBulk) UpdateTotalRecharged() *UserUpsertBulk {
})
}
// SetRpmLimit sets the "rpm_limit" field.
func (u *UserUpsertBulk) SetRpmLimit(v int) *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.SetRpmLimit(v)
})
}
// AddRpmLimit adds v to the "rpm_limit" field.
func (u *UserUpsertBulk) AddRpmLimit(v int) *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.AddRpmLimit(v)
})
}
// UpdateRpmLimit sets the "rpm_limit" field to the value that was provided on create.
func (u *UserUpsertBulk) UpdateRpmLimit() *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.UpdateRpmLimit()
})
}
// Exec executes the query.
func (u *UserUpsertBulk) Exec(ctx context.Context) error {
if u.create.err != nil {

View File

@@ -389,6 +389,27 @@ func (_u *UserUpdate) AddTotalRecharged(v float64) *UserUpdate {
return _u
}
// SetRpmLimit sets the "rpm_limit" field.
func (_u *UserUpdate) SetRpmLimit(v int) *UserUpdate {
_u.mutation.ResetRpmLimit()
_u.mutation.SetRpmLimit(v)
return _u
}
// SetNillableRpmLimit sets the "rpm_limit" field if the given value is not nil.
func (_u *UserUpdate) SetNillableRpmLimit(v *int) *UserUpdate {
if v != nil {
_u.SetRpmLimit(*v)
}
return _u
}
// AddRpmLimit adds value to the "rpm_limit" field.
func (_u *UserUpdate) AddRpmLimit(v int) *UserUpdate {
_u.mutation.AddRpmLimit(v)
return _u
}
// AddAPIKeyIDs adds the "api_keys" edge to the APIKey entity by IDs.
func (_u *UserUpdate) AddAPIKeyIDs(ids ...int64) *UserUpdate {
_u.mutation.AddAPIKeyIDs(ids...)
@@ -1008,6 +1029,12 @@ func (_u *UserUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if value, ok := _u.mutation.AddedTotalRecharged(); ok {
_spec.AddField(user.FieldTotalRecharged, field.TypeFloat64, value)
}
if value, ok := _u.mutation.RpmLimit(); ok {
_spec.SetField(user.FieldRpmLimit, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedRpmLimit(); ok {
_spec.AddField(user.FieldRpmLimit, field.TypeInt, value)
}
if _u.mutation.APIKeysCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
@@ -1930,6 +1957,27 @@ func (_u *UserUpdateOne) AddTotalRecharged(v float64) *UserUpdateOne {
return _u
}
// SetRpmLimit sets the "rpm_limit" field.
func (_u *UserUpdateOne) SetRpmLimit(v int) *UserUpdateOne {
_u.mutation.ResetRpmLimit()
_u.mutation.SetRpmLimit(v)
return _u
}
// SetNillableRpmLimit sets the "rpm_limit" field if the given value is not nil.
func (_u *UserUpdateOne) SetNillableRpmLimit(v *int) *UserUpdateOne {
if v != nil {
_u.SetRpmLimit(*v)
}
return _u
}
// AddRpmLimit adds value to the "rpm_limit" field.
func (_u *UserUpdateOne) AddRpmLimit(v int) *UserUpdateOne {
_u.mutation.AddRpmLimit(v)
return _u
}
// AddAPIKeyIDs adds the "api_keys" edge to the APIKey entity by IDs.
func (_u *UserUpdateOne) AddAPIKeyIDs(ids ...int64) *UserUpdateOne {
_u.mutation.AddAPIKeyIDs(ids...)
@@ -2579,6 +2627,12 @@ func (_u *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) {
if value, ok := _u.mutation.AddedTotalRecharged(); ok {
_spec.AddField(user.FieldTotalRecharged, field.TypeFloat64, value)
}
if value, ok := _u.mutation.RpmLimit(); ok {
_spec.SetField(user.FieldRpmLimit, field.TypeInt, value)
}
if value, ok := _u.mutation.AddedRpmLimit(); ok {
_spec.AddField(user.FieldRpmLimit, field.TypeInt, value)
}
if _u.mutation.APIKeysCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,