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
  • Loading branch information
pd4d10 committed May 6, 2021
1 parent 2ff93c8 commit 8816e85
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/dgram.js
Expand Up @@ -86,6 +86,9 @@ const SEND_BUFFER = false;

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

const errnoException = errors.errnoException;
const exceptionWithHostPort = errors.exceptionWithHostPort;
Expand Down Expand Up @@ -200,8 +203,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');
lazyLoadCluster();

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

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

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

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

let flags = 0;
if (state.reuseAddr)
Expand Down

0 comments on commit 8816e85

Please sign in to comment.