Skip to content

Commit

Permalink
dgram: extract cluster lazy loading method to make it testable
Browse files Browse the repository at this point in the history
PR-URL: #38563
Refs: https://coverage.nodejs.org/coverage-26e318a321a872bc/lib/dgram.js.html#L202
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
pd4d10 authored and aduh95 committed May 9, 2021

Verified

This commit was signed with the committer’s verified signature.
xkeshav Keshav Mohta
1 parent 5934a44 commit 1b11398
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/dgram.js
Original file line number Diff line number Diff line change
@@ -85,7 +85,11 @@ const RECV_BUFFER = true;
const SEND_BUFFER = false;

// Lazily loaded
let cluster = null;
let _cluster = null;
function lazyLoadCluster() {
if (!_cluster) _cluster = require('cluster');
return _cluster;
}

const errnoException = errors.errnoException;
const exceptionWithHostPort = errors.exceptionWithHostPort;
@@ -200,8 +204,7 @@ function bufferSize(self, size, buffer) {

// Query primary process to get the server handle and utilize it.
function bindServerHandle(self, options, errCb) {
if (!cluster)
cluster = require('cluster');
const cluster = lazyLoadCluster();

const state = self[kStateSymbol];
cluster._getServer(self, options, (err, handle) => {
@@ -262,8 +265,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
const exclusive = !!port.exclusive;
const state = this[kStateSymbol];

if (!cluster)
cluster = require('cluster');
const cluster = lazyLoadCluster();

if (cluster.isWorker && !exclusive) {
bindServerHandle(this, {
@@ -325,8 +327,7 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
return;
}

if (!cluster)
cluster = require('cluster');
const cluster = lazyLoadCluster();

let flags = 0;
if (state.reuseAddr)

0 comments on commit 1b11398

Please sign in to comment.