Skip to content

Commit

Permalink
fix: add more debug log (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jan 10, 2023
1 parent 0dc3a8c commit 698105f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const debug = require('debug')('cluster-client:server');
const debug = require('debug')('cluster-client:lib:server');
const net = require('net');
const Base = require('sdk-base');
const sleep = require('mz-modules/sleep');
Expand Down Expand Up @@ -46,8 +46,8 @@ function claimServer(port) {

function tryToConnect(port) {
return new Promise(resolve => {
debug('try to connecting port:%j', port);
const socket = net.connect(port, '127.0.0.1');
debug('try to connecting %s', port);
let success = false;
socket.on('connect', () => {
success = true;
Expand Down Expand Up @@ -76,7 +76,7 @@ class ClusterServer extends Base {
*/
constructor(options) {
super();

debug('new ClusterServer(%j)', options);
this._sockets = new Map();
this._server = options.server;
this._port = options.port;
Expand Down Expand Up @@ -172,12 +172,15 @@ class ClusterServer extends Base {
const key = `${name}@${port}`;
let instance = serverMap.get(port);
if (instance && !instance.isClosed) {
debug('create %j instance exists', key);
if (typeSet.has(key)) {
debug('%j instance in typeSet', key);
return null;
}
typeSet.add(key);
return instance;
}
debug('create %j instance not exists, try to create a new one with port: %j', key, port);
// compete for the local port, if got => leader, otherwise follower
try {
const server = await claimServer(port);
Expand All @@ -186,6 +189,7 @@ class ClusterServer extends Base {
serverMap.set(port, instance);
return instance;
} catch (err) {
debug('create %j instance error: %s', key, err);
// if exception, that mean compete for port failed, then double check
instance = serverMap.get(port);
if (instance && !instance.isClosed) {
Expand Down
4 changes: 3 additions & 1 deletion lib/wrapper/base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const debug = require('debug')('cluster-client');
const debug = require('debug')('cluster-client:lib:wrapper:base');
const is = require('is-type-of');
const Base = require('sdk-base');
const assert = require('assert');
Expand Down Expand Up @@ -36,7 +36,9 @@ class WrapperBase extends Base {
* @class
*/
constructor(options) {
options = options || {};
super(options);
debug('new WrapperBase({ port: %j, isLeader: %j })', options.port, options.isLeader);
this[subInfo] = new Map();
this[pubInfo] = new Map();
this[init]().catch(err => { this.ready(err); });
Expand Down
2 changes: 1 addition & 1 deletion lib/wrapper/cluster.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const debug = require('debug')('cluster-client');
const debug = require('debug')('cluster-client:lib:wrapper:cluster');
const Base = require('./base');
const Leader = require('../leader');
const Follower = require('../follower');
Expand Down

0 comments on commit 698105f

Please sign in to comment.