github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/queue/eventPriority.go (about) 1 package queue 2 3 import ( 4 "fmt" 5 "math" 6 7 "github.com/onflow/flow-go/model/flow" 8 libp2pmessage "github.com/onflow/flow-go/model/libp2p/message" 9 "github.com/onflow/flow-go/model/messages" 10 "github.com/onflow/flow-go/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 // data exchange for execution of blocks 83 case *messages.ChunkDataRequest: 84 return HighPriority 85 case *messages.ChunkDataResponse: 86 return HighPriority 87 88 // request/response for result approvals 89 case *messages.ApprovalRequest: 90 return MediumPriority 91 case *messages.ApprovalResponse: 92 return MediumPriority 93 94 // generic entity exchange engines 95 case *messages.EntityRequest: 96 return LowPriority 97 case *messages.EntityResponse: 98 return LowPriority 99 100 // test message 101 case *libp2pmessage.TestMessage: 102 return LowPriority 103 104 // anything else 105 default: 106 return MediumPriority 107 } 108 } 109 110 // getPriorityBySize returns a priority of a message by size. Smaller messages have higher priority than larger ones. 111 func getPriorityBySize(size int) Priority { 112 switch { 113 case size > MiB: 114 return LowPriority 115 case size > KiB: 116 return MediumPriority 117 default: 118 return HighPriority 119 } 120 }