Weight calculate node's weight for sort. Highest weight node will be accessed first for next request.
()
| 110 | |
| 111 | //Weight calculate node's weight for sort. Highest weight node will be accessed first for next request. |
| 112 | func (this *NodeWeight) Weight() float32 { |
| 113 | avgSpeed := float32(0.0) |
| 114 | for _, s := range this.speed { |
| 115 | avgSpeed += s |
| 116 | } |
| 117 | avgSpeed = avgSpeed / float32(len(this.speed)) |
| 118 | |
| 119 | avgInterval := float32(0.0) |
| 120 | now := time.Now().UnixNano() / int64(time.Millisecond) |
| 121 | for _, t := range this.reqTime { |
| 122 | avgInterval += float32(now - t) |
| 123 | } |
| 124 | avgInterval = avgInterval / float32(len(this.reqTime)) |
| 125 | w := avgSpeed + avgInterval |
| 126 | return w |
| 127 | } |
| 128 | |
| 129 | //NodeWeights implement sorting |
| 130 | type NodeWeights []*NodeWeight |