Skip to content

Commit

Permalink
Merge pull request #230 from ChainSafe/dapplion/rpc-metrics
Browse files Browse the repository at this point in the history
Fix rpc.control metrics and reduce object creation
  • Loading branch information
wemeetagain authored Apr 25, 2022
2 parents fe07f20 + 00b24f2 commit 703ea64
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
8 changes: 4 additions & 4 deletions ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1943,16 +1943,16 @@ export default class Gossipsub extends EventEmitter {
}

if (outRpc.control) {
outRpc.control.graft = outRpc.control.graft && outRpc.control.graft.concat(tograft)
outRpc.control.prune = outRpc.control.prune && outRpc.control.prune.concat(toprune)
outRpc.control.graft = outRpc.control.graft ? outRpc.control.graft.concat(tograft) : tograft
outRpc.control.prune = outRpc.control.prune ? outRpc.control.prune.concat(toprune) : toprune
} else {
outRpc.control = { ihave: [], iwant: [], graft: tograft, prune: toprune }
outRpc.control = { graft: tograft, prune: toprune }
}
}

private piggybackGossip(id: PeerIdStr, outRpc: IRPC, ihave: RPC.IControlIHave[]): void {
if (!outRpc.control) {
outRpc.control = { ihave: [], iwant: [], graft: [], prune: [] }
outRpc.control = { ihave: [] }
}
outRpc.control.ihave = ihave
}
Expand Down
14 changes: 9 additions & 5 deletions ts/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,15 @@ export function getMetrics(
if (rpc.subscriptions) this.rpcSentSubscription.inc(rpc.subscriptions.length)
if (rpc.messages) this.rpcSentMessage.inc(rpc.messages.length)
if (rpc.control) {
this.rpcSentControl.inc(1)
if (rpc.control.ihave) this.rpcSentIHave.inc(rpc.control.ihave.length)
if (rpc.control.iwant) this.rpcSentIWant.inc(rpc.control.iwant.length)
if (rpc.control.graft) this.rpcSentGraft.inc(rpc.control.graft.length)
if (rpc.control.prune) this.rpcSentPrune.inc(rpc.control.prune.length)
const ihave = rpc.control.ihave ? rpc.control.ihave.length : 0
const iwant = rpc.control.iwant ? rpc.control.iwant.length : 0
const graft = rpc.control.graft ? rpc.control.graft.length : 0
const prune = rpc.control.prune ? rpc.control.prune.length : 0
if (ihave > 0) this.rpcSentIHave.inc(ihave)
if (iwant > 0) this.rpcSentIWant.inc(iwant)
if (graft > 0) this.rpcSentGraft.inc(graft)
if (prune > 0) this.rpcSentPrune.inc(prune)
if (ihave > 0 || iwant > 0 || graft > 0 || prune > 0) this.rpcSentControl.inc(1)
}
},

Expand Down
9 changes: 2 additions & 7 deletions ts/utils/create-gossip-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ import { RPC, IRPC } from '../message/rpc'
/**
* Create a gossipsub RPC object
*/
export function createGossipRpc(messages: RPC.IMessage[] = [], control: Partial<RPC.IControlMessage> = {}): IRPC {
export function createGossipRpc(messages: RPC.IMessage[] = [], control?: Partial<RPC.IControlMessage>): IRPC {
return {
subscriptions: [],
messages,
control: {
ihave: control.ihave ?? [],
iwant: control.iwant ?? [],
graft: control.graft ?? [],
prune: control.prune ?? []
}
control
}
}

0 comments on commit 703ea64

Please sign in to comment.