func (r *receptor) Random(publicKey []byte) ([]byte, []byte, time.Time, error) {
	r.Lock()
	defer r.Unlock()
loop:
	for tries := 1; tries <= 5; tries += 1 {
		max := big.NewInt(int64(r.connectable.Count()))
		num, err := rand.Int(rand.Reader, max)
		if nil != err {
			continue loop
		}
		n := int(num.Int64())  
		node := r.connectable.Get(n)
		if nil == node {
			node = r.connectable.First()
		}
		if nil == node {
			break loop
		}
		e := node.Value().(*Entity)
		if bytes.Equal(e.PublicKey, r.id) || bytes.Equal(e.PublicKey, publicKey) {
			continue loop
		}
		return e.PublicKey, e.Listeners, e.Timestamp, nil
	}
	return []byte{}, []byte{}, time.Now(), fault.InvalidPublicKey
}
Random - fetch a random node data in the ring not matching a given public key
package receptor
import (
	"bytes"
	"crypto/rand"
	"math/big"
	"sync"
	"time"
	"github.com/bitmark-inc/bitmarkd/announce/helper"
	"github.com/bitmark-inc/bitmarkd/announce/id"
	"github.com/bitmark-inc/bitmarkd/announce/parameter"
	"github.com/bitmark-inc/bitmarkd/avl"
	"github.com/bitmark-inc/bitmarkd/fault"
	"github.com/bitmark-inc/bitmarkd/messagebus"
	"github.com/bitmark-inc/logger"
)
const timeFormat = "2006-01-02 15:04:05"
type Receptor interface {
	Add([]byte, []byte, uint64) bool
	SetSelf([]byte, []byte) error
	Next([]byte) ([]byte, []byte, time.Time, error)
	Random([]byte) ([]byte, []byte, time.Time, error)
	ReBalance()
	UpdateTime([]byte, time.Time)
	IsChanged() bool
	Change(bool)
	IsInitialised() bool
	Connectable() *avl.Tree
	ID() id.ID
	Self() *avl.Node
	SelfListener() []byte
	Expire()
}
type receptor struct {
	sync.RWMutex
	connectable *avl.Tree
	self        *avl.Node
	changed     bool
	id          id.ID
	log         *logger.L
	initialised bool
	listeners   []byte
}
func (r *receptor) Add(publicKey []byte, listeners []byte, timestamp uint64) bool {
	r.Lock()
	defer r.Unlock()
	ts := helper.ResetFutureTimestampToNow(timestamp)
	if helper.IsExpiredAfterDuration(ts, parameter.ExpiryInterval) {
		return false
	}
	e := &Entity{
		PublicKey: publicKey,
		Listeners: listeners,
		Timestamp: ts,
	}
	if node, _ := r.connectable.Search(id.ID(publicKey)); nil != node {
		e := node.Value().(*Entity)
		if ts.Sub(e.Timestamp) < parameter.RebroadcastInterval {
			return false
		}
	}
	recordAdded := r.connectable.Insert(id.ID(publicKey), e)
	r.log.Debugf("added: %t  nodes in the connectable tree: %d", recordAdded, r.connectable.Count())
	if bytes.Equal(r.id, publicKey) {
		return false
	}
	if recordAdded {
		r.changed = true
	}
	return true
}
func (r *receptor) SetSelf(publicKey []byte, listeners []byte) error {
	r.Lock()
	if r.initialised {
		r.Unlock()
		return fault.AlreadyInitialised
	}
	r.id = publicKey
	r.listeners = listeners
	r.initialised = true
	r.Unlock()
	r.Add(publicKey, listeners, uint64(time.Now().Unix()))
	r.Lock()
	r.self, _ = r.connectable.Search(id.ID(publicKey))
	r.Unlock()
	r.ReBalance()
	return nil
}
func (r *receptor) ReBalance() {
	r.Lock()
	defer r.Unlock()
	log := r.log
	if nil == r.self {
		log.Errorf("determineConnections called to early")
		return  
	}
	_, index := r.connectable.Search(r.self.Key())
	count := r.connectable.Count()
	log.Infof("N0: index: %d  tree: %d  public key: %x", index, count, r.id)
	e := count / 8
	q := count / 4
	h := count / 2
	jump := 3       
	if count < 4 {  
		jump = 1  
	}
	names := [11]string{
		"N1",
		"N3",
		"X1",
		"X2",
		"X3",
		"X4",
		"X5",
		"X6",
		"X7",
		"P1",
		"P3",
	}
	var n [11]int
	n[0] = index + 1              
	n[1] = index + jump           
	n[2] = e + index              
	n[3] = q + index              
	n[4] = q + e + index          
	n[5] = h + index              
	n[6] = h + e + index          
	n[7] = h + q + index          
	n[8] = h + q + e + index      
	n[9] = index + count - 1      
	n[10] = index + count - jump  
	u := -1
deduplicate:
	for i, v := range n {
		if v == index || v == u {
			continue deduplicate
		}
		u = v
		if v >= count {
			v -= count
		}
		node := r.connectable.Get(v)
		if nil != node {
			e := node.Value().(*Entity)
			if nil != e {
				log.Infof("%s: connectable entity: %s", names[i], e)
				messagebus.Bus.Connector.Send(names[i], e.PublicKey, e.Listeners)
			}
		}
	}
}
func (r *receptor) Next(publicKey []byte) ([]byte, []byte, time.Time, error) {
	r.Lock()
	defer r.Unlock()
	node, _ := r.connectable.Search(id.ID(publicKey))
	if nil != node {
		node = node.Next()
	}
	if nil == node {
		node = r.connectable.First()
	}
	if nil == node {
		return nil, nil, time.Now(), fault.InvalidPublicKey
	}
	e := node.Value().(*Entity)
	return e.PublicKey, e.Listeners, e.Timestamp, nil
}