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

Added default parameter ForceWaitTimeout to OpQuery and WaitForReply #18

Open
wants to merge 6 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
20 changes: 15 additions & 5 deletions Grijjy.MongoDB.Protocol.pas
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ TgoMongoProtocol = class
FAuthErrorCode: Integer;
private
procedure Send(const AData: TBytes);
function WaitForReply(const ARequestId: Integer): IgoMongoReply;
function WaitForReply(const ARequestId: Integer; AForceWaitTimeout : Boolean = false): IgoMongoReply;
function TryGetReply(const ARequestId: Integer; out AReply: IgoMongoReply): Boolean; inline;
function LastPartialReply(const ARequestID: Integer; out ALastRecv: TDateTime): Boolean;
function OpReplyValid(out AIndex: Integer): Boolean;
Expand Down Expand Up @@ -255,7 +255,8 @@ TgoMongoProtocol = class
function OpQuery(const AFullCollectionName: UTF8String;
const AFlags: TgoMongoQueryFlags; const ANumberToSkip,
ANumberToReturn: Integer; const AQuery: TBytes;
const AReturnFieldsSelector: TBytes = nil): IgoMongoReply;
const AReturnFieldsSelector: TBytes = nil;
const AForceWaitTimeout : Boolean = false): IgoMongoReply;

{ Implements the OP_GET_MORE opcode, used to get an additional page of
documents from the database.
Expand Down Expand Up @@ -750,7 +751,8 @@ function TgoMongoProtocol.OpGetMore(const AFullCollectionName: UTF8String;
function TgoMongoProtocol.OpQuery(const AFullCollectionName: UTF8String;
const AFlags: TgoMongoQueryFlags; const ANumberToSkip,
ANumberToReturn: Integer; const AQuery,
AReturnFieldsSelector: TBytes): IgoMongoReply;
AReturnFieldsSelector: TBytes;
const AForceWaitTimeout : Boolean): IgoMongoReply;
{ https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#wire-op-query }
var
Header: TMsgHeader;
Expand Down Expand Up @@ -785,7 +787,7 @@ function TgoMongoProtocol.OpQuery(const AFullCollectionName: UTF8String;
finally
Data.Free;
end;
Result := WaitForReply(Header.RequestID);
Result := WaitForReply(Header.RequestID, AForceWaitTimeout);
end;

function TgoMongoProtocol.OpReplyMsgHeader(out AMsgHeader): Boolean;
Expand Down Expand Up @@ -935,18 +937,26 @@ function TgoMongoProtocol.TryGetReply(const ARequestId: Integer;
end;

function TgoMongoProtocol.WaitForReply(
const ARequestId: Integer): IgoMongoReply;
const ARequestId: Integer; AForceWaitTimeout : Boolean = false): IgoMongoReply;
var
LastRecv: TDateTime;
InitRecv: TDateTime;
begin
Result := nil;
InitRecv := Now;
while (ConnectionState = TgoConnectionState.Connected) and
(not TryGetReply(ARequestID, Result)) do
begin
if LastPartialReply(ARequestID, LastRecv) and
(MillisecondsBetween(Now, LastRecv) > FSettings.ReplyTimeout)
then
Break;
// in case we didn't receive any response, stop if timout
// is reached and AForceWaitTimeout=true is passed
if ((Int(LastRecv) = 0) and (AForceWaitTimeout = true) and
(MillisecondsBetween(Now, InitRecv) > FSettings.ReplyTimeout))
then
Break;
Sleep(5);
end;

Expand Down
5 changes: 1 addition & 4 deletions Grijjy.MongoDB.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,6 @@ function TgoMongoClient.GetInstanceInfo(const ASaslSupportedMechs: String = '';
Reply: IgoMongoReply;
Doc: TgoBsonDocument;
InstArray: TgoBsonArray;
Databases: TgoBsonArray;
Value: TgoBsonValue;
I: Integer;
begin
Writer := TgoBsonWriter.Create;
Expand All @@ -1215,7 +1213,7 @@ function TgoMongoClient.GetInstanceInfo(const ASaslSupportedMechs: String = '';
Writer.WriteString('Comment', AComment);
end;
Writer.WriteEndDocument;
Reply := FProtocol.OpQuery(COLLECTION_ADMIN_COMMAND, [], 0, -1, Writer.ToBson, nil);
Reply := FProtocol.OpQuery(COLLECTION_ADMIN_COMMAND, [], 0, -1, Writer.ToBson, nil, true);
HandleCommandReply(Reply);

if not(Reply.Documents = nil) then
Expand Down Expand Up @@ -1336,7 +1334,6 @@ function TgoMongoDatabase.CreateCollection(const AName: String;
var
Writer: IgoBsonWriter;
Reply: IgoMongoReply;
i: Integer;
begin
Writer := TgoBsonWriter.Create;

Expand Down