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

Add support for read only mode. #85

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,16 @@ Factory method to create a new zookeeper [client](#client) instance.
* `sessionTimeout` Session timeout in milliseconds, defaults to 30 seconds.
* `spinDelay` The delay (in milliseconds) between each connection attempts.
* `retries` The number of retry attempts for connection loss exception.
* `readOnly` Allow connection to server in read-only mode.

Defaults options:

```javascript
{
sessionTimeout: 30000,
spinDelay : 1000,
retries : 0
retries : 0,
readOnly: false
}
```

Expand Down
39 changes: 27 additions & 12 deletions lib/ConnectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,28 @@ ConnectionManager.prototype.connect = function () {
};

ConnectionManager.prototype.close = function () {
var self = this,
header = new jute.protocol.RequestHeader(),
request;

self.setState(STATES.CLOSING);
if (this.connectTimeoutHandler) {

clearTimeout(this.connectTimeoutHandler);

this.connectTimeoutHandler = null
this.state = STATES.CLOSING
this.socket.destroy()

} else {

var self = this,
header = new jute.protocol.RequestHeader(),
request;

header.type = jute.OP_CODES.CLOSE_SESSION;
request = new jute.Request(header, null);
self.setState(STATES.CLOSING);

self.queue(request);
header.type = jute.OP_CODES.CLOSE_SESSION;
request = new jute.Request(header, null);

self.queue(request);
}
};

ConnectionManager.prototype.onSocketClosed = function (hasError) {
Expand All @@ -260,7 +272,7 @@ ConnectionManager.prototype.onSocketClosed = function (hasError) {
break;
case STATES.SESSION_EXPIRED:
errorCode = Exception.SESSION_EXPIRED;
retry = false;
retry = true;
break;
case STATES.AUTHENTICATION_FAILED:
errorCode = Exception.AUTH_FAILED;
Expand Down Expand Up @@ -312,10 +324,10 @@ ConnectionManager.prototype.onSocketConnected = function () {
this.zxid,
this.sessionTimeout,
this.sessionId,
this.sessionPassword
this.sessionPassword,
this.options.readOnly || false
));

// XXX No read only support yet.
this.socket.write(connectRequest.toBuffer());

// Set auth info
Expand Down Expand Up @@ -428,8 +440,11 @@ ConnectionManager.prototype.onSocketData = function (buffer) {
connectResponse = new jute.protocol.ConnectResponse();
offset += connectResponse.deserialize(buffer, offset);


if (connectResponse.timeOut <= 0) {

self.sessionId.fill(0);
self.socket.destroy();

self.setState(STATES.SESSION_EXPIRED);

} else {
Expand All @@ -440,7 +455,7 @@ ConnectionManager.prototype.onSocketData = function (buffer) {
self.sessionPassword = connectResponse.passwd;
self.updateTimeout(connectResponse.timeOut);

self.setState(STATES.CONNECTED);
self.setState(connectResponse.readOnly ? STATES.CONNECTED_READ_ONLY : STATES.CONNECTED);

// Check if we have anything to send out just in case.
self.onPacketQueueReadable();
Expand Down
3 changes: 2 additions & 1 deletion lib/Exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var CODES = {
SESSION_EXPIRED : -112,
INVALID_CALLBACK : -113,
INVALID_ACL : -114,
AUTH_FAILED : -115
AUTH_FAILED : -115,
NOT_READ_ONLY : -119
};

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/jute/specification.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
{ "name" : "lastZxidSeen", "type" : "long"},
{ "name" : "timeOut", "type" : "int"},
{ "name" : "sessionId", "type" : "long"},
{ "name" : "passwd", "type" : "buffer"}
{ "name" : "passwd", "type" : "buffer"},
{ "name" : "readOnly", "type" : "boolean"}
],
"ConnectResponse" : [
{ "name" : "protocolVersion", "type" : "int" },
{ "name" : "timeOut", "type" : "int" },
{ "name" : "sessionId", "type" : "long" },
{ "name" : "passwd", "type" : "buffer" }
{ "name" : "passwd", "type" : "buffer" },
{ "name" : "readOnly", "type" : "boolean"}
],
"RequestHeader" : [
{ "name" : "xid", "type" : "int" },
Expand Down