syntax = "proto3";

package v1;

import "google/protobuf/wrappers.proto";
import "google/protobuf/duration.proto";
import "model.proto";

// 同一服务下限流规则集合
message RateLimit {
  // 限流规则集合
	repeated Rule rules = 1;
  // 限流规则汇总的revision信息
  google.protobuf.StringValue revision = 2;
}

// 单个限流规则信息
message Rule {
  // 限流规则唯一标识
  google.protobuf.StringValue id = 1;
	// 限流规则所属服务名
	google.protobuf.StringValue service = 2;
	// 限流规则所属命名空间
	google.protobuf.StringValue namespace = 3;
	// 限流规则所属集群名
	google.protobuf.StringValue cluster = 4;
  // 限流规则优先级，0值最高
  google.protobuf.UInt32Value priority = 5;
  // 限流资源
	enum Resource {
    // 针对QPS进行限流
		QPS = 0;
    // 针对并发数进行限流
    CONCURRENCY = 1;
	}
	Resource resource = 6;
	// 限流类型
	// global全局限流(默认)或者local单机限流
	enum Type {
		GLOBAL = 0;
		LOCAL = 1;
	}
	Type type = 7;
	// 业务标签集合，通过KV进行匹配，匹配到一个则使用该规则
	map<string, MatchString> labels = 8;
	// 限流阈值
	// 可以有多个粒度的配置（比如同时针对秒级，分钟级，天级），匹配一个则进行限流
  // 全局限流模式下，该值为服务配额总量；单机限流模式下，该值为单个节点能处理的配额量
	repeated Amount amounts = 9;
	// 限流动作，对应着客户端的插件名字
	google.protobuf.StringValue action = 10;
  // 是否停用该限流规则，默认启用
	google.protobuf.BoolValue disable = 11;
	// 限流上报方式，同时支持按固定周期上报，以及达到配额百分比后上报
	Report report = 12;
  // 限流规则创建时间
  google.protobuf.StringValue ctime = 13;
  // 限流规则修改时间
	google.protobuf.StringValue mtime = 14;
  // 限流规则revision信息
	google.protobuf.StringValue revision = 15;
  // 服务的TOKEN信息，仅用于控制台，discover接口不下发
	google.protobuf.StringValue service_token = 16 [json_name="service_token"];  
}

// 限流配额
message Amount {
	// 时间周期内的最大配额数
	google.protobuf.UInt32Value maxAmount = 1;
	// 配额生效的时间周期，必须大于等于1s
	google.protobuf.Duration validDuration = 2;
}

// 限流上报方式
message Report {
	// 配额固定上报周期，单位毫秒
	google.protobuf.Duration interval = 1;
	// 使用了百分之多少配额后启动一次实时上报，值范围(0,100]
	google.protobuf.UInt32Value amountPercent = 2;
}
