Skip to content

Commit

Permalink
feat: use libp2p component logger
Browse files Browse the repository at this point in the history
Refactors code to use the component logger from libp2p to allow
more flexible logging patterns.

Refs: https://github.com/libp2p/js-libp2p/issue/2105
Refs: libp2p/js-libp2p#2198
Refs: https://github.com/libp2p/js-libp2p/issue/378
  • Loading branch information
achingbrain committed Nov 26, 2023
1 parent 8495ae3 commit 44ec1ad
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
9 changes: 0 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { CodeError } from '@libp2p/interface/errors'
import { logger, type Logger } from '@libp2p/logger'
import { ERR_INVALID_CONFIG, INITIAL_STREAM_WINDOW, MAX_STREAM_WINDOW } from './constants.js'

// TOOD use config items or delete them
export interface Config {
/**
* Used to control the log destination
*
* It can be disabled by explicitly setting to `undefined`
*/
log?: Logger

/**
* Used to do periodic keep alive messages using a ping.
*/
Expand Down Expand Up @@ -55,7 +47,6 @@ export interface Config {
}

export const defaultConfig: Config = {
log: logger('libp2p:yamux'),
enableKeepAlive: true,
keepAliveInterval: 30_000,
maxInboundStreams: 1_000,
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Yamux } from './muxer.js'
import type { YamuxMuxerInit } from './muxer.js'
import type { StreamMuxerFactory } from '@libp2p/interface/stream-muxer'
import type { ComponentLogger } from '@libp2p/logger'
export { GoAwayCode } from './frame.js'

export function yamux (init: YamuxMuxerInit = {}): () => StreamMuxerFactory {
return () => new Yamux(init)
export interface YamuxComponents {
logger: ComponentLogger
}

export function yamux (init: YamuxMuxerInit = {}): (components: YamuxComponents) => StreamMuxerFactory {
return (components) => new Yamux(components, init)
}
33 changes: 22 additions & 11 deletions src/muxer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CodeError } from '@libp2p/interface/errors'
import { setMaxListeners } from '@libp2p/interface/events'
import { logger, type Logger } from '@libp2p/logger'
import { getIterator } from 'get-iterator'
import { pushable, type Pushable } from 'it-pushable'
import { Uint8ArrayList } from 'uint8arraylist'
Expand All @@ -10,9 +9,11 @@ import { Decoder } from './decode.js'
import { encodeHeader } from './encode.js'
import { Flag, type FrameHeader, FrameType, GoAwayCode } from './frame.js'
import { StreamState, YamuxStream } from './stream.js'
import type { YamuxComponents } from './index.js'
import type { AbortOptions } from '@libp2p/interface'
import type { Stream } from '@libp2p/interface/connection'
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface/stream-muxer'
import type { ComponentLogger, Logger } from '@libp2p/logger'
import type { Sink, Source } from 'it-stream-types'

const YAMUX_PROTOCOL_ID = '/yamux/1.0.0'
Expand All @@ -24,13 +25,15 @@ export interface YamuxMuxerInit extends StreamMuxerInit, Partial<Config> {
export class Yamux implements StreamMuxerFactory {
protocol = YAMUX_PROTOCOL_ID
private readonly _init: YamuxMuxerInit
private readonly components: YamuxComponents

constructor (init: YamuxMuxerInit = {}) {
constructor (components: YamuxComponents, init: YamuxMuxerInit = {}) {
this._init = init
this.components = components
}

createStreamMuxer (init?: YamuxMuxerInit): YamuxMuxer {
return new YamuxMuxer({
return new YamuxMuxer(this.components, {
...this._init,
...init
})
Expand All @@ -47,7 +50,8 @@ export class YamuxMuxer implements StreamMuxer {
sink: Sink<Source<Uint8ArrayList | Uint8Array>, Promise<void>>

private readonly config: Config
private readonly log?: Logger
private readonly log: Logger
private readonly logger: ComponentLogger

/** Used to close the muxer from either the sink or source */
private readonly closeController: AbortController
Expand Down Expand Up @@ -78,10 +82,11 @@ export class YamuxMuxer implements StreamMuxer {
private readonly onIncomingStream?: (stream: Stream) => void
private readonly onStreamEnd?: (stream: Stream) => void

constructor (init: YamuxMuxerInit) {
constructor (components: YamuxComponents, init: YamuxMuxerInit) {
this.client = init.direction === 'outbound'
this.config = { ...defaultConfig, ...init }
this.log = this.config.log
this.log = components.logger.forComponent('libp2p:yamux')
this.logger = components.logger
verifyConfig(this.config)

this.closeController = new AbortController()
Expand Down Expand Up @@ -164,14 +169,18 @@ export class YamuxMuxer implements StreamMuxer {
this.nextPingID = 0
this.rtt = -1

this.log?.trace('muxer created')
this.log.trace('muxer created')

if (this.config.enableKeepAlive) {
this.keepAliveLoop().catch(e => this.log?.error('keepalive error: %s', e))
this.keepAliveLoop().catch(e => {
this.log.error('keepalive error: %s', e)
})
}

// send an initial ping to establish RTT
this.ping().catch(e => this.log?.error('ping error: %s', e))
this.ping().catch(e => {
this.log.error('ping error: %s', e)
})
}

get streams (): YamuxStream[] {
Expand Down Expand Up @@ -363,7 +372,7 @@ export class YamuxMuxer implements StreamMuxer {
this.closeStream(id)
this.onStreamEnd?.(stream)
},
log: logger(`libp2p:yamux:${direction}:${id}`),
log: this.logger.forComponent(`libp2p:yamux:${direction}:${id}`),
config: this.config,
getRTT: this.getRTT.bind(this)
})
Expand Down Expand Up @@ -396,7 +405,9 @@ export class YamuxMuxer implements StreamMuxer {
timeoutId = setTimeout(resolve, this.config.keepAliveInterval)
})
])
this.ping().catch(e => this.log?.error('ping error: %s', e))
this.ping().catch(e => {
this.log.error('ping error: %s', e)
})
} catch (e) {
// closed
clearInterval(timeoutId)
Expand Down
3 changes: 2 additions & 1 deletion test/compliance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-env mocha */

import tests from '@libp2p/interface-compliance-tests/stream-muxer'
import { defaultLogger } from '@libp2p/logger'
import { TestYamux } from './util.js'

describe('compliance', () => {
tests({
async setup () {
return new TestYamux({})
return new TestYamux({ logger: defaultLogger() })
},
async teardown () {}
})
Expand Down
9 changes: 4 additions & 5 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logger } from '@libp2p/logger'
import { prefixLogger } from '@libp2p/logger'
import { duplexPair } from 'it-pair/duplex'
import { pipe } from 'it-pipe'
import { type Uint8ArrayList } from 'uint8arraylist'
Expand Down Expand Up @@ -29,16 +29,15 @@ export const testConf: Partial<Config> = {
export class TestYamux extends Yamux {
createStreamMuxer (init?: YamuxMuxerInit): YamuxMuxer {
const client = isClient()
return super.createStreamMuxer({ ...testConf, ...init, direction: client ? 'outbound' : 'inbound', log: logger(`libp2p:yamux${client ? 1 : 2}`) })
return super.createStreamMuxer({ ...testConf, ...init, direction: client ? 'outbound' : 'inbound' })
}
}

export function testYamuxMuxer (name: string, client: boolean, conf: YamuxMuxerInit = {}): YamuxMuxer {
return new YamuxMuxer({
return new YamuxMuxer({ logger: prefixLogger(name) }, {
...testConf,
...conf,
direction: client ? 'outbound' : 'inbound',
log: logger(name)
direction: client ? 'outbound' : 'inbound'
})
}

Expand Down

0 comments on commit 44ec1ad

Please sign in to comment.