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

     1  package memberlist
     2  
     3  // EventDelegate is a simpler delegate that is used only to receive
     4  // notifications about members joining and leaving. The methods in this
     5  // delegate may be called by multiple goroutines, but never concurrently.
     6  // This allows you to reason about ordering.
     7  type EventDelegate interface {
     8  	// NotifyJoin is invoked when a node is detected to have joined.
     9  	// The Node argument must not be modified.
    10  	NotifyJoin(*Node)
    11  
    12  	// NotifyLeave is invoked when a node is detected to have left.
    13  	// The Node argument must not be modified.
    14  	NotifyLeave(*Node)
    15  
    16  	// NotifyUpdate is invoked when a node is detected to have
    17  	// updated, usually involving the meta data. The Node argument
    18  	// must not be modified.
    19  	NotifyUpdate(*Node)
    20  
    21  	// NotifyWeight is invoked when a node's weight is detected to have updated.
    22  	// The Node argument must not be modified.
    23  	NotifyWeight(*Node)
    24  
    25  	// NotifySuspectSateChange is invoked when a node state is changed from StateAlive to StateSuspect or from StateSuspect to StateAlive
    26  	NotifySuspectSateChange(*Node)
    27  }
    28  
    29  // ChannelEventDelegate is used to enable an application to receive
    30  // events about joins and leaves over a channel instead of a direct
    31  // function call.
    32  //
    33  // Care must be taken that events are processed in a timely manner from
    34  // the channel, since this delegate will block until an event can be sent.
    35  type ChannelEventDelegate struct {
    36  	Ch chan<- NodeEvent
    37  }
    38  
    39  // NodeEventType are the types of events that can be sent from the
    40  // ChannelEventDelegate.
    41  type NodeEventType int
    42  
    43  const (
    44  	NodeJoin NodeEventType = iota
    45  	NodeLeave
    46  	NodeUpdate
    47  	NodeWeight
    48  	NodeSuspect
    49  )
    50  
    51  // NodeEvent is a single event related to node activity in the memberlist.
    52  // The Node member of this struct must not be directly modified. It is passed
    53  // as a pointer to avoid unnecessary copies. If you wish to modify the node,
    54  // make a copy first.
    55  type NodeEvent struct {
    56  	Event NodeEventType
    57  	Node  *Node
    58  }
    59  
    60  func (c *ChannelEventDelegate) NotifyJoin(n *Node) {
    61  	node := *n
    62  	c.Ch <- NodeEvent{NodeJoin, &node}
    63  }
    64  
    65  func (c *ChannelEventDelegate) NotifyLeave(n *Node) {
    66  	node := *n
    67  	c.Ch <- NodeEvent{NodeLeave, &node}
    68  }
    69  
    70  func (c *ChannelEventDelegate) NotifyUpdate(n *Node) {
    71  	node := *n
    72  	c.Ch <- NodeEvent{NodeUpdate, &node}
    73  }
    74  
    75  func (c *ChannelEventDelegate) NotifyWeight(n *Node) {
    76  	node := *n
    77  	c.Ch <- NodeEvent{NodeWeight, &node}
    78  }
    79  
    80  func (c *ChannelEventDelegate) NotifySuspectSateChange(n *Node) {
    81  	node := *n
    82  	c.Ch <- NodeEvent{NodeSuspect, &node}
    83  }