// tidyRules removes duplicate subnet rules, and use the lowest non-zero timeout.
//
// This is basically a copy of `Listener.SetFilter` from the proxyprotocol package.
tidyRules removes duplicate subnet rules, and use the lowest non-zero timeout.

This is basically a copy of `Listener.SetFilter` from the proxyprotocol package.
// tidyRules removes duplicate subnet rules and use the lowest non - zero timeout . // // This is basically a copy of ` Listener . SetFilter ` from the proxyprotocol package .
func (h *Handler) tidyRules() {
	rules := h.rules
	sort.Slice(rules, func(i, j int) bool {
		iOnes, iBits := rules[i].Subnet.Mask.Size()
		jOnes, jBits := rules[j].Subnet.Mask.Size()
		if iOnes != jOnes {
			return iOnes > jOnes
		}
		if iBits != jBits {
			return iBits > jBits
		}
		if rules[i].Timeout != rules[j].Timeout {
			if rules[j].Timeout == 0 {
				return true
			}
			return rules[i].Timeout < rules[j].Timeout
		}
		return rules[i].Timeout < rules[j].Timeout
	})
	if len(rules) > 0 {
		last := rules[0]
		nf := rules[1:1]
		for _, f := range rules[1:] {
			if last.Subnet.String() == f.Subnet.String() {
				continue
			}
			last = f
			nf = append(nf, f)
		}
	}
}
func ( h * Handler ) tidyRules ( ) { rules := h . rules sort . Slice ( rules , func ( i , j int ) bool { iOnes , iBits := rules [ i ] . Subnet . Mask . Size ( ) jOnes , jBits := rules [ j ] . Subnet . Mask . Size ( ) if iOnes != jOnes { return iOnes > jOnes } if iBits != jBits { return iBits > jBits } if rules [ i ] . Timeout != rules [ j ] . Timeout { if rules [ j ] . Timeout == 0 { return true } return rules [ i ] . Timeout < rules [ j ] . Timeout } return rules [ i ] . Timeout < rules [ j ] . Timeout } ) if len ( rules ) > 0 { last := rules [ 0 ] nf := rules [ 1 : 1 ] for _ , f := range rules [ 1 : ] { if last . Subnet . String ( ) == f . Subnet . String ( ) { continue } last = f nf = append ( nf , f ) } } }
tidyRules removes duplicate subnet rules, and use the lowest non-zero timeout.
tidyRules removes duplicate subnet rules and use the lowest non - zero timeout .
// dedup