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 bd0a9a4
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/dgram.js
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

0 comments on commit bd0a9a4

Please sign in to comment.