Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rpc.control metrics and reduce object creation #230

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}