Skip to content

Commit

Permalink
condense provide and providepeer
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 14, 2024
1 parent 2b8f20f commit c645374
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions server_routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"errors"
"math"
"sync"
"time"

Expand Down Expand Up @@ -217,6 +218,57 @@ func (mi *manyIter[T]) Close() error {
return err
}

func (r parallelRouter) Provide(ctx context.Context, req *server.ProvideRequest) (time.Duration, error) {
return provide(ctx, r.routers, func(ctx context.Context, r router) (time.Duration, error) {
return r.Provide(ctx, req)
})

Check warning on line 224 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L221-L224

Added lines #L221 - L224 were not covered by tests
}

func (r parallelRouter) ProvidePeer(ctx context.Context, req *server.ProvidePeerRequest) (time.Duration, error) {
return provide(ctx, r.routers, func(ctx context.Context, r router) (time.Duration, error) {
return r.ProvidePeer(ctx, req)
})

Check warning on line 230 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L227-L230

Added lines #L227 - L230 were not covered by tests
}

func provide(ctx context.Context, routers []router, call func(context.Context, router) (time.Duration, error)) (time.Duration, error) {
switch len(routers) {
case 0:
return 0, nil
case 1:
return call(ctx, routers[0])

Check warning on line 238 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L233-L238

Added lines #L233 - L238 were not covered by tests
}

ctx, cancel := context.WithCancel(ctx)
defer cancel()

var wg sync.WaitGroup
resultsTTL := make([]time.Duration, len(routers))
resultsErr := make([]error, len(routers))
wg.Add(len(routers))
for i, ri := range routers {
go func(ri router, i int) {
resultsTTL[i], resultsErr[i] = call(ctx, ri)
wg.Done()
}(ri, i)

Check warning on line 252 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L241-L252

Added lines #L241 - L252 were not covered by tests
}
wg.Wait()

var err error
for _, e := range resultsErr {
err = errors.Join(err, e)
}

Check warning on line 259 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L254-L259

Added lines #L254 - L259 were not covered by tests

// Choose lowest TTL to return.
var ttl time.Duration = math.MaxInt64
for _, t := range resultsTTL {
if t < ttl {
ttl = t
}

Check warning on line 266 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L262-L266

Added lines #L262 - L266 were not covered by tests
}

return ttl, err

Check warning on line 269 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L269

Added line #L269 was not covered by tests
}

func (r parallelRouter) GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error) {
switch len(r.routers) {
case 0:
Expand Down Expand Up @@ -307,46 +359,6 @@ func (r parallelRouter) PutIPNS(ctx context.Context, name ipns.Name, record *ipn
return errs
}

func (r parallelRouter) Provide(ctx context.Context, req *server.ProvideRequest) (time.Duration, error) {
switch len(r.routers) {
case 0:
return req.TTL, nil
case 1:
return r.routers[0].Provide(ctx, req)
}

ctx, cancel := context.WithCancel(ctx)
defer cancel()

var wg sync.WaitGroup
ttl := req.TTL
results := make([]error, len(r.routers))
wg.Add(len(r.routers))
for i, ri := range r.routers {
go func(ri router, i int) {
var ittl time.Duration
ittl, results[i] = ri.Provide(ctx, req)

if results[i] == nil && ittl < ttl {
ttl = ittl
}

wg.Done()
}(ri, i)
}
wg.Wait()

var errs error
for _, err := range results {
errs = errors.Join(errs, err)
}
return ttl, errs
}

func (r parallelRouter) ProvidePeer(ctx context.Context, req *server.ProvidePeerRequest) (time.Duration, error) {
return 0, routing.ErrNotSupported
}

var _ router = libp2pRouter{}

type libp2pRouter struct {
Expand Down

0 comments on commit c645374

Please sign in to comment.