libp2p / go-libp2p-discovery

Active Peer Discovery
MIT License
53 stars 13 forks source link

Update util.go #57

Closed cc14514 closed 4 years ago

cc14514 commented 4 years ago

If only one server node in a small private network , the routing table may be empty , sometime I want loop period is 1 minute , this error will broken that logic.

// util.go
// Advertise is a utility function that persistently advertises a service through an Advertiser.
func Advertise(ctx context.Context, a Advertiser, ns string, opts ...Option) {
    go func() {
        for {
            ttl, err := a.Advertise(ctx, ns, opts...)
            if err != nil {
                log.Debugf("Error advertising %s: %s", ns, err.Error())
                if ctx.Err() != nil {
                    return
                }
                                // TODO: this point
                select {
                case <-time.After(2 * time.Minute):
                    continue
                case <-ctx.Done():
                    return
                }
            }

            wait := 7 * ttl / 8
            select {
            case <-time.After(wait):
            case <-ctx.Done():
                return
            }
        }
    }()
}
Stebalien commented 4 years ago

This doesn't look right. If the routing table is empty (or advertising fails for any other reason), this loop will pause for 2 minutes and try again. Acting like the advertisement succeeded isn't the correct thing to do in this case.