count和queueSize要求是2的幂以避免求余计算,如果不是2的幂将会隐式转换为2的幂来构造 HashKey要求映射到count范围内,否则MultiQueue只会取低比特位
(module string, count uint8, queueSize int, options ...Option)
| 63 | // count和queueSize要求是2的幂以避免求余计算,如果不是2的幂将会隐式转换为2的幂来构造 |
| 64 | // HashKey要求映射到count范围内,否则MultiQueue只会取低比特位 |
| 65 | func NewOverwriteQueues(module string, count uint8, queueSize int, options ...Option) FixedMultiQueue { |
| 66 | if count > MAX_QUEUE_COUNT { |
| 67 | panic(fmt.Sprintf("queueCount超出最大限制%d", MAX_QUEUE_COUNT)) |
| 68 | } |
| 69 | |
| 70 | size := int(count) |
| 71 | queues := make([]*OverwriteQueue, size) |
| 72 | for i := 0; i < size; i++ { |
| 73 | opts := append(options, stats.OptionStatTags{"index": strconv.Itoa(i)}) |
| 74 | queue := new(OverwriteQueue) |
| 75 | queue.Init(module, queueSize, opts...) |
| 76 | queues[i] = queue |
| 77 | } |
| 78 | tableSize := 1 |
| 79 | for tableSize < size { |
| 80 | tableSize <<= 1 |
| 81 | } |
| 82 | table := make(FixedMultiQueue, tableSize) |
| 83 | for i := 0; i < tableSize; i++ { |
| 84 | table[i] = queues[i%size] |
| 85 | } |
| 86 | return table |
| 87 | } |