mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-07 01:08:08 +08:00
* Delegated Routing. Implementation of Reframe specs (https://github.com/ipfs/specs/blob/master/REFRAME.md) using go-delegated-routing library. * Requested changes. * Init using op string * Separate possible ContentRouters for TopicDiscovery. If we don't do this, we have a ciclic dependency creating TieredRouter. Now we can create first all possible content routers, and after that, create Routers. * Set dht default routing type * Add tests and remove uneeded code * Add documentation. * docs: Routing.Routers * Requested changes. Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com> * Add some documentation on new fx functions. * Add changelog entry and integration tests * test: sharness for 'dht' in 'routing' commands Since 'routing' is currently the same as 'dht' (minus query command) we need to test both, that way we won't have unnoticed divergence in the default behavior. * test(sharness): delegated routing via reframe URL * Add more tests for delegated routing. * If any put operation fails, the tiered router will fail. * refactor: Routing.Routers: Parameters.Endpoint As agreed in https://github.com/ipfs/kubo/pull/8997#issuecomment-1175684716 * Try to improve CHANGELOG entry. * chore: update reframe spec link * Update go-delegated-routing dependency * Fix config error test * use new changelog format * Remove port conflict * go mod tidy * ProviderManyWrapper to ProviderMany * Update docs/changelogs/v0.14.md Co-authored-by: Adin Schmahmann <adin.schmahmann@gmail.com> Co-authored-by: Marcin Rataj <lidel@lidel.org> Co-authored-by: Adin Schmahmann <adin.schmahmann@gmail.com>
94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
package routing
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
drc "github.com/ipfs/go-delegated-routing/client"
|
|
drp "github.com/ipfs/go-delegated-routing/gen/proto"
|
|
"github.com/ipfs/kubo/config"
|
|
"github.com/libp2p/go-libp2p-core/routing"
|
|
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
|
|
)
|
|
|
|
type TieredRouter interface {
|
|
routing.Routing
|
|
ProvideMany() ProvideMany
|
|
}
|
|
|
|
var _ TieredRouter = &Tiered{}
|
|
|
|
// Tiered is a routing Tiered implementation providing some extra methods to fill
|
|
// some special use cases when initializing the client.
|
|
type Tiered struct {
|
|
routinghelpers.Tiered
|
|
}
|
|
|
|
// ProvideMany returns a ProvideMany implementation including all Routers that
|
|
// implements ProvideMany
|
|
func (ds Tiered) ProvideMany() ProvideMany {
|
|
var pms []ProvideMany
|
|
for _, r := range ds.Tiered.Routers {
|
|
pm, ok := r.(ProvideMany)
|
|
if !ok {
|
|
continue
|
|
}
|
|
pms = append(pms, pm)
|
|
}
|
|
|
|
if len(pms) == 0 {
|
|
return nil
|
|
}
|
|
|
|
return &ProvideManyWrapper{pms: pms}
|
|
}
|
|
|
|
const defaultPriority = 100000
|
|
|
|
// GetPriority extract priority from config params.
|
|
// Small numbers represent more important routers.
|
|
func GetPriority(params map[string]string) int {
|
|
param := params[string(config.RouterParamPriority)]
|
|
if param == "" {
|
|
return defaultPriority
|
|
}
|
|
|
|
p, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return defaultPriority
|
|
}
|
|
|
|
return p
|
|
}
|
|
|
|
// RoutingFromConfig creates a Routing instance from the specified configuration.
|
|
func RoutingFromConfig(c config.Router) (routing.Routing, error) {
|
|
switch {
|
|
case c.Type == string(config.RouterTypeReframe):
|
|
return reframeRoutingFromConfig(c)
|
|
default:
|
|
return nil, &RouterTypeNotFoundError{c.Type}
|
|
}
|
|
}
|
|
|
|
func reframeRoutingFromConfig(conf config.Router) (routing.Routing, error) {
|
|
var dr drp.DelegatedRouting_Client
|
|
|
|
param := string(config.RouterParamEndpoint)
|
|
addr, ok := conf.Parameters[param]
|
|
if !ok {
|
|
return nil, NewParamNeededErr(param, conf.Type)
|
|
}
|
|
|
|
dr, err := drp.New_DelegatedRouting_Client(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c := drc.NewClient(dr)
|
|
crc := drc.NewContentRoutingClient(c)
|
|
return &reframeRoutingWrapper{
|
|
Client: c,
|
|
ContentRoutingClient: crc,
|
|
}, nil
|
|
}
|