Skip to content

Commit

Permalink
feat: add metrics callbacks + update constructors (libp2p#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Apr 23, 2023
1 parent 835efc4 commit 2a40a0d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
},
"dependencies": {
"@chainsafe/libp2p-noise": "^11.0.0",
"@libp2p/interface-connection": "^4.0.4",
"@libp2p/interface-connection": "^4.0.0",
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-peer-store": "^1.2.8",
"@libp2p/interface-registrar": "^2.0.8",
Expand All @@ -150,6 +150,7 @@
"@multiformats/multiaddr": "^12.1.1",
"@protobuf-ts/runtime": "^2.8.0",
"abortable-iterator": "^4.0.2",
"detect-browser": "^5.3.0",
"err-code": "^3.0.1",
"it-length-prefixed": "^8.0.3",
"it-merge": "^2.0.0",
Expand All @@ -163,19 +164,19 @@
"protons-runtime": "^5.0.0",
"timeout-abort-controller": "^3.0.0",
"uint8arraylist": "^2.3.3",
"uint8arrays": "^4.0.2",
"detect-browser": "^5.3.0"
"uint8arrays": "^4.0.2"
},
"devDependencies": {
"@libp2p/interface-mocks": "^9.2.1",
"@libp2p/peer-id-factory": "^2.0.2",
"@protobuf-ts/plugin": "^2.8.2",
"@protobuf-ts/protoc": "^2.8.2",
"@libp2p/interface-mocks": "^9.0.0",
"@libp2p/peer-id-factory": "^2.0.0",
"@protobuf-ts/plugin": "^2.8.0",
"@protobuf-ts/protoc": "^2.8.0",
"@types/uuid": "^9.0.1",
"aegir": "^38.1.7",
"aegir": "^38.1.6",
"eslint-plugin-etc": "^2.0.2",
"it-pair": "^2.0.3",
"protons": "^7.0.2",
"sinon": "^15.0.2"
"sinon": "^15.0.1",
"sinon-ts": "^1.0.0"
}
}
17 changes: 7 additions & 10 deletions src/muxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export class DataChannelMuxerFactory implements StreamMuxerFactory {
*/
private readonly peerConnection: RTCPeerConnection
private streamBuffer: WebRTCStream[] = []
private readonly metrics?: CounterGroup

constructor (peerConnection: RTCPeerConnection, readonly protocol = '/webrtc') {
constructor (peerConnection: RTCPeerConnection, metrics?: CounterGroup, readonly protocol = '/webrtc') {
this.peerConnection = peerConnection
// store any datachannels opened before upgrade has been completed
this.peerConnection.ondatachannel = ({ channel }) => {
Expand All @@ -34,10 +35,11 @@ export class DataChannelMuxerFactory implements StreamMuxerFactory {
})
this.streamBuffer.push(stream)
}
this.metrics = metrics
}

createStreamMuxer (init?: StreamMuxerInit | undefined): StreamMuxer {
return new DataChannelMuxer(this.peerConnection, this.metrics, this.streamBuffer, this.protocol, init)
return new DataChannelMuxer(this.peerConnection, this.streamBuffer, this.protocol, init, this.metrics)
}
}

Expand All @@ -55,11 +57,6 @@ export class DataChannelMuxer implements StreamMuxer {
*/
private readonly metrics?: CounterGroup

/**
* The protocol as represented in the multiaddress
*/
readonly protocol: string = '/webrtc'

/**
* Array of streams in the data channel
*/
Expand All @@ -85,7 +82,7 @@ export class DataChannelMuxer implements StreamMuxer {
*/
sink: Sink<Uint8Array, Promise<void>> = nopSink

constructor (peerConnection: RTCPeerConnection, metrics?: CounterGroup, streams: Stream[], readonly protocol = '/webrtc', init?: StreamMuxerInit) {
constructor (peerConnection: RTCPeerConnection, streams: Stream[], readonly protocol: string = '/webrtc', init?: StreamMuxerInit, metrics?: CounterGroup) {
/**
* Initialized stream muxer
*/
Expand Down Expand Up @@ -137,7 +134,7 @@ export class DataChannelMuxer implements StreamMuxer {
newStream (): Stream {
// The spec says the label SHOULD be an empty string: https://github.com/libp2p/specs/blob/master/webrtc/README.md#rtcdatachannel-label
const channel = this.peerConnection.createDataChannel('')
const closeCb = (stream: WebRTCStream): void => {
const closeCb = (stream: Stream): void => {
this.metrics?.increment({ stream_end: true })
this.init?.onStreamEnd?.(stream)
}
Expand All @@ -149,7 +146,7 @@ export class DataChannelMuxer implements StreamMuxer {
open: 0
}
},
closeCb: this.wrapStreamEnd(this.init?.onStreamEnd)
closeCb: this.wrapStreamEnd(closeCb)
})
this.streams.push(stream)

Expand Down
5 changes: 1 addition & 4 deletions src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ export class WebRTCDirectTransport implements Transport {
metrics: this.metrics?.dialerEvents
})

const muxerFactory = new DataChannelMuxerFactory({
peerConnection,
metrics: this.metrics?.dialerEvents
})
const muxerFactory = new DataChannelMuxerFactory(peerConnection, this.metrics?.dialerEvents)

// For outbound connections, the remote is expected to start the noise handshake.
// Therefore, we need to secure an inbound noise connection from the remote.
Expand Down
12 changes: 6 additions & 6 deletions test/transport.browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ describe('WebRTC Transport', () => {

before(async () => {
metrics = stubObject<Metrics>({
trackMultiaddrConnection: (maConn) => {},
trackProtocolStream: (stream, connection) => {},
registerMetric: (name, options) => {
trackMultiaddrConnection: (_maConn) => {},
trackProtocolStream: (_stream, _connection) => {},
registerMetric: (_name, _options) => {
return stubObject<Metric>({
increment: () => {},
reset: () => {},
Expand All @@ -36,7 +36,7 @@ describe('WebRTC Transport', () => {
}
})
},
registerMetricGroup: (name, options) => {
registerMetricGroup: (_name, _options) => {
return stubObject<MetricGroup>({
increment: () => {
},
Expand All @@ -48,13 +48,13 @@ describe('WebRTC Transport', () => {
}
})
},
registerCounter: (name, options) => {
registerCounter: (_name, _options) => {
return stubObject<Counter>({
increment: () => {},
reset: () => {}
})
},
registerCounterGroup: (name, options) => {
registerCounterGroup: (_name, _options) => {
return stubObject<CounterGroup>({
increment: () => {},
reset: () => {}
Expand Down

0 comments on commit 2a40a0d

Please sign in to comment.