Skip to content

Commit

Permalink
Fix 100% CPU problem
Browse files Browse the repository at this point in the history
  • Loading branch information
testn committed Nov 3, 2021
1 parent 3c300a8 commit 2329883
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/commands/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class Query extends Command {
if (this._receivedFieldsCount === this._fieldCount) {
const fields = this._fields[this._resultIndex];
this.emit('fields', fields);
this._rowParser = new (getTextParser(fields, this.options, connection.config))();
this._rowParser = new (getTextParser(fields, this.options, connection.config))(fields);
return Query.prototype.fieldsEOF;
}
return Query.prototype.readField;
Expand Down
70 changes: 33 additions & 37 deletions lib/parsers/text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ function compile(fields, options, config) {
options.typeCast = config.typeCast;
}

function wrap(field, _this) {
return {
type: typeNames[field.columnType],
length: field.columnLength,
db: field.schema,
table: field.table,
name: field.name,
string: function() {
return _this.packet.readLengthCodedString(field.encoding);
},
buffer: function() {
return _this.packet.readLengthCodedBuffer();
},
geometry: function() {
return _this.packet.parseGeometryValue();
}
};
}

const parserFn = genFunc();

/* eslint-disable no-trailing-spaces */
Expand All @@ -88,42 +107,14 @@ function compile(fields, options, config) {
);

// constructor method
parserFn('constructor() {');
parserFn('constructor(fields) {');
// node-mysql typeCast compatibility wrapper
// see https://github.com/mysqljs/mysql/blob/96fdd0566b654436624e2375c7b6604b1f50f825/lib/protocol/packets/Field.js
if (typeof options.typeCast === 'function') {
parserFn('const _this = this;');
for(let i=0; i<fields.length; ++i) {
const field = fields[i];
const encodingExpr = helpers.srcEscape(field.encoding);
const readCode = readCodeFor(
fields[i].columnType,
fields[i].characterSet,
encodingExpr,
config,
options
);
parserFn(`this.wrap${i} = {
type: ${helpers.srcEscape(typeNames[field.columnType])},
length: ${field.columnLength},
db: ${helpers.srcEscape(field.schema)},
table: ${helpers.srcEscape(field.table)},
name: ${helpers.srcEscape(field.name)},
string: function() {
return _this.packet.readLengthCodedString(${encodingExpr});
},
buffer: function() {
return _this.packet.readLengthCodedBuffer();
},
geometry: function() {
return _this.packet.parseGeometryValue();
},
readNext: function() {
const packet = _this.packet;
return ${readCode};
}
};`);
}
parserFn('for(let i=0; i<fields.length; ++i) {');
parserFn('this[`wrap${i}`] = wrap(fields[i], _this);');
parserFn('}');
}
parserFn('}');

Expand Down Expand Up @@ -165,9 +156,7 @@ function compile(fields, options, config) {
} else {
lvalue = `result[${fieldName}]`;
}
if (typeof options.typeCast === 'function') {
parserFn(`${lvalue} = options.typeCast(this.wrap${i}, this.wrap${i}.readNext);`);
} else if (options.typeCast === false) {
if (options.typeCast === false) {
parserFn(`${lvalue} = packet.readLengthCodedBuffer();`);
} else {
const encodingExpr = `fields[${i}].encoding`;
Expand All @@ -178,8 +167,12 @@ function compile(fields, options, config) {
config,
options
);
parserFn(`${lvalue} = ${readCode};`);
}
if (typeof options.typeCast === 'function') {
parserFn(`${lvalue} = options.typeCast(this.wrap${i}, function() { return ${readCode} });`);
} else {
parserFn(`${lvalue} = ${readCode};`);
}
}
}

parserFn('return result;');
Expand All @@ -196,6 +189,9 @@ function compile(fields, options, config) {
parserFn.toString()
);
}
if (typeof options.typeCast === 'function') {
return parserFn.toFunction({wrap});
}
return parserFn.toFunction();
}

Expand Down

0 comments on commit 2329883

Please sign in to comment.