github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/memberlist/broadcast.go (about)

     1  package memberlist
     2  
     3  /*
     4  The broadcast mechanism works by maintaining a sorted list of messages to be
     5  sent out. When a message is to be broadcast, the retransmit count
     6  is set to zero and appended to the queue. The retransmit count serves
     7  as the "priority", ensuring that newer messages get sent first. Once
     8  a message hits the retransmit limit, it is removed from the queue.
     9  
    10  Additionally, older entries can be invalidated by new messages that
    11  are contradictory. For example, if we send "{suspect M1 inc: 1},
    12  then a following {alive M1 inc: 2} will invalidate that message
    13  */
    14  
    15  type memberlistBroadcast struct {
    16  	node   string
    17  	msg    []byte
    18  	notify chan struct{}
    19  }
    20  
    21  func NewMemberlistBroadcast(node string, msg []byte, notify chan struct{}) *memberlistBroadcast {
    22  	return &memberlistBroadcast{node: node, msg: msg, notify: notify}
    23  }
    24  
    25  func (b *memberlistBroadcast) Invalidates(other Broadcast) bool {
    26  	// Check if that broadcast is a memberlist type
    27  	mb, ok := other.(*memberlistBroadcast)
    28  	if !ok {
    29  		return false
    30  	}
    31  
    32  	// Invalidates any message about the same node
    33  	return b.node == mb.node
    34  }
    35  
    36  // memberlist.NamedBroadcast optional interface
    37  func (b *memberlistBroadcast) Name() string {
    38  	return b.node
    39  }
    40  
    41  func (b *memberlistBroadcast) Message() []byte {
    42  	return b.msg
    43  }
    44  
    45  func (b *memberlistBroadcast) Finished() {
    46  	select {
    47  	case b.notify <- struct{}{}:
    48  	default:
    49  	}
    50  }