Home / Function/ multipleBindings() — fastify Function Reference

multipleBindings() — fastify Function Reference

Architecture documentation for the multipleBindings() function in server.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  183b2e81_49c4_37fc_93d0_da7f1b0f16f1["multipleBindings()"]
  39d7da74_a631_4522_4e09_0cc36c64a8f8["server.js"]
  183b2e81_49c4_37fc_93d0_da7f1b0f16f1 -->|defined in| 39d7da74_a631_4522_4e09_0cc36c64a8f8
  f8a56543_30d0_fd17_5f30_f1ddc20ab439["getServerInstance()"]
  183b2e81_49c4_37fc_93d0_da7f1b0f16f1 -->|calls| f8a56543_30d0_fd17_5f30_f1ddc20ab439
  style 183b2e81_49c4_37fc_93d0_da7f1b0f16f1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

lib/server.js lines 151–230

function multipleBindings (mainServer, httpHandler, serverOpts, listenOptions, onListen) {
  // the main server is started, we need to start the secondary servers
  this[kState].listening = false

  // let's check if we need to bind additional addresses
  dns.lookup(listenOptions.host, { all: true }, (dnsErr, addresses) => {
    if (dnsErr || this[kState].aborted) {
      // not blocking the main server listening
      // this.log.warn('dns.lookup error:', dnsErr)
      onListen()
      return
    }

    const isMainServerListening = mainServer.listening && serverOpts.serverFactory

    let binding = 0
    let bound = 0
    if (!isMainServerListening) {
      const primaryAddress = mainServer.address()
      for (const adr of addresses) {
        if (adr.address !== primaryAddress.address) {
          binding++
          const secondaryOpts = Object.assign({}, listenOptions, {
            host: adr.address,
            port: primaryAddress.port,
            cb: (_ignoreErr) => {
              bound++

              if (!_ignoreErr) {
                this[kServerBindings].push(secondaryServer)
              }

              if (bound === binding) {
                // regardless of the error, we are done
                onListen()
              }
            }
          })

          const secondaryServer = getServerInstance(serverOpts, httpHandler)
          const closeSecondary = () => {
            // To avoid falling into situations where the close of the
            // secondary server is triggered before the preClose hook
            // is done running, we better wait until the main server is closed.
            // No new TCP connections are accepted
            // We swallow any error from the secondary server
            secondaryServer.close(() => {})
            if (typeof secondaryServer.closeAllConnections === 'function' && serverOpts.forceCloseConnections === true) {
              secondaryServer.closeAllConnections()
            }
            if (typeof secondaryServer.closeHttp2Sessions === 'function') {
              secondaryServer.closeHttp2Sessions()
            }
          }

          secondaryServer.on('upgrade', mainServer.emit.bind(mainServer, 'upgrade'))
          mainServer.on('unref', closeSecondary)
          mainServer.on('close', closeSecondary)
          mainServer.on('error', closeSecondary)
          this[kState].listening = false
          listenCallback.call(this, secondaryServer, secondaryOpts)()
        }
      }
    }
    // no extra bindings are necessary
    if (binding === 0) {
      onListen()
      return
    }

    // in test files we are using unref so we need to propagate the unref event
    // to the secondary servers. It is valid only when the user is
    // listening on localhost
    const originUnref = mainServer.unref
    mainServer.unref = function () {
      originUnref.call(mainServer)
      mainServer.emit('unref')
    }
  })
}

Domain

Subdomains

Defined In

Frequently Asked Questions

What does multipleBindings() do?
multipleBindings() is a function in the fastify codebase, defined in lib/server.js.
Where is multipleBindings() defined?
multipleBindings() is defined in lib/server.js at line 151.
What does multipleBindings() call?
multipleBindings() calls 1 function(s): getServerInstance.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free