github.com/koko1123/flow-go-1@v0.29.6/network/queue/eventPriority.go (about)

     1  package queue
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  
     7  	"github.com/koko1123/flow-go-1/model/flow"
     8  	libp2pmessage "github.com/koko1123/flow-go-1/model/libp2p/message"
     9  	"github.com/koko1123/flow-go-1/model/messages"
    10  	"github.com/koko1123/flow-go-1/network/channels"
    11  )
    12  
    13  const (
    14  	_   = iota
    15  	KiB = 1 << (10 * iota)
    16  	MiB
    17  )
    18  
    19  // QMessage is the message that is enqueued for each incoming message
    20  type QMessage struct {
    21  	Payload  interface{}      // the decoded message
    22  	Size     int              // the size of the message in bytes
    23  	Target   channels.Channel // the target channel to lookup the engine
    24  	SenderID flow.Identifier  // senderID for logging
    25  }
    26  
    27  // GetEventPriority returns the priority of the flow event message.
    28  // It is an average of the priority by message type and priority by message size
    29  func GetEventPriority(message interface{}) (Priority, error) {
    30  	qm, ok := message.(QMessage)
    31  	if !ok {
    32  		return 0, fmt.Errorf("invalid message format: %T", message)
    33  	}
    34  	priorityByType := getPriorityByType(qm.Payload)
    35  	priorityBySize := getPriorityBySize(qm.Size)
    36  	return Priority(math.Ceil(float64(priorityByType+priorityBySize) / 2)), nil
    37  }
    38  
    39  // getPriorityByType maps a message type to its priority
    40  func getPriorityByType(message interface{}) Priority {
    41  	switch message.(type) {
    42  	// consensus
    43  	case *messages.BlockProposal:
    44  		return HighPriority
    45  	case *messages.BlockVote:
    46  		return HighPriority
    47  
    48  	// protocol state sync
    49  	case *messages.SyncRequest:
    50  		return LowPriority
    51  	case *messages.SyncResponse:
    52  		return LowPriority
    53  	case *messages.RangeRequest:
    54  		return MediumPriority
    55  	case *messages.BatchRequest:
    56  		return MediumPriority
    57  	case *messages.BlockResponse:
    58  		return HighPriority
    59  
    60  	// cluster consensus
    61  	case *messages.ClusterBlockProposal:
    62  		return HighPriority
    63  	case *messages.ClusterBlockVote:
    64  		return HighPriority
    65  	case *messages.ClusterBlockResponse:
    66  		return HighPriority
    67  
    68  	// collections, guarantees & transactions
    69  	case *flow.CollectionGuarantee:
    70  		return HighPriority
    71  	case *flow.TransactionBody:
    72  		return HighPriority
    73  	case *flow.Transaction:
    74  		return HighPriority
    75  
    76  	// core messages for execution & verification
    77  	case *flow.ExecutionReceipt:
    78  		return HighPriority
    79  	case *flow.ResultApproval:
    80  		return HighPriority
    81  
    82  	// execution state synchronization
    83  	case *messages.ExecutionStateSyncRequest:
    84  		return MediumPriority
    85  	case *messages.ExecutionStateDelta:
    86  		return HighPriority
    87  
    88  	// data exchange for execution of blocks
    89  	case *messages.ChunkDataRequest:
    90  		return HighPriority
    91  	case *messages.ChunkDataResponse:
    92  		return HighPriority
    93  
    94  	// request/response for result approvals
    95  	case *messages.ApprovalRequest:
    96  		return MediumPriority
    97  	case *messages.ApprovalResponse:
    98  		return MediumPriority
    99  
   100  	// generic entity exchange engines
   101  	case *messages.EntityRequest:
   102  		return LowPriority
   103  	case *messages.EntityResponse:
   104  		return LowPriority
   105  
   106  	// test message
   107  	case *libp2pmessage.TestMessage:
   108  		return LowPriority
   109  
   110  	// anything else
   111  	default:
   112  		return MediumPriority
   113  	}
   114  }
   115  
   116  // getPriorityBySize returns a priority of a message by size. Smaller messages have higher priority than larger ones.
   117  func getPriorityBySize(size int) Priority {
   118  	switch {
   119  	case size > MiB:
   120  		return LowPriority
   121  	case size > KiB:
   122  		return MediumPriority
   123  	default:
   124  		return HighPriority
   125  	}
   126  }