add path option for hop

This commit is contained in:
ginuerzh
2023-11-09 20:34:59 +08:00
parent 3eca21104a
commit 6bface4581
10 changed files with 143 additions and 147 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/json"
"io"
"net"
"sort"
"strings"
"sync"
"time"
@ -179,6 +180,26 @@ func (p *chainHop) Select(ctx context.Context, opts ...hop.SelectOption) *chain.
}
}
// filter by path
if path := options.Path; path != "" {
p.options.logger.Debugf("filter by path: %s", path)
sort.SliceStable(filters, func(i, j int) bool {
return len(filters[i].Options().Path) > len(filters[j].Options().Path)
})
var nodes []*chain.Node
for _, node := range filters {
if node.Options().Path == "" {
nodes = append(nodes, node)
continue
}
if strings.HasPrefix(path, node.Options().Path) {
nodes = append(nodes, node)
break
}
}
filters = nodes
}
var nodes []*chain.Node
for _, node := range filters {
if node == nil {