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

dgram: extract cluster lazy loading method to make it testable #38563

Merged
merged 1 commit into from
May 9, 2021
Merged
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
15 changes: 8 additions & 7 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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)
Expand Down