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

docs: sample on setting per-request gaxOptions #1210

Merged
Merged
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
72 changes: 72 additions & 0 deletions samples/dml.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,64 @@ async function updateUsingBatchDml(instanceId, databaseId, projectId) {
// [END spanner_dml_batch_update]
}

async function insertWithCustomTimeoutAndRetrySettings(
instanceId,
databaseId,
projectId
) {
// [START spanner_set_custom_timeout_and_retry]
// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

// Creates a client
const spanner = new Spanner({
projectId: projectId,
});
const DEADLINE_EXCEEDED_STATUS_CODE = 4;
laljikanjareeya marked this conversation as resolved.
Show resolved Hide resolved
const UNAVAILABLE_STATUS_CODE = 14;
const retryAndTimeoutSettings = {
retry: {
retryCodes: [DEADLINE_EXCEEDED_STATUS_CODE, UNAVAILABLE_STATUS_CODE],
backoffSettings: {
// Configure retry delay settings.
initialRetryDelayMillis: 500,
maxRetryDelayMillis: 64000,
retryDelayMultiplier: 1.5,
// Configure RPC and total timeout settings.
initialRpcTimeoutMillis: 60000,
rpcTimeoutMultiplier: 1.0,
maxRpcTimeoutMillis: 60000,
totalTimeoutMillis: 60000,
},
},
};

// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);
const table = database.table('Singers');

const row = {
SingerId: 16,
FirstName: 'Martha',
LastName: 'Waller',
};

await table.insert(row, {
gaxOptions: retryAndTimeoutSettings,
});

console.log('record inserted.');
// [END spanner_set_custom_timeout_and_retry]
}

require('yargs')
.demand(1)
.command(
Expand Down Expand Up @@ -736,6 +794,17 @@ require('yargs')
opts =>
updateUsingBatchDml(opts.instanceName, opts.databaseName, opts.projectId)
)
.command(
'insertWithCustomTimeoutAndRetrySettings <instanceName> <databaseName> <projectId>',
'Insert records using custom timeout and retry settings.',
{},
opts =>
insertWithCustomTimeoutAndRetrySettings(
opts.instanceName,
opts.databaseName,
opts.projectId
)
)
.example('node $0 insertUsingDml "my-instance" "my-database" "my-project-id"')
.example('node $0 updateUsingDml "my-instance" "my-database" "my-project-id"')
.example('node $0 deleteUsingDml "my-instance" "my-database" "my-project-id"')
Expand Down Expand Up @@ -764,6 +833,9 @@ require('yargs')
.example(
'node $0 updateUsingBatchDml "my-instance" "my-database" "my-project-id"'
)
.example(
'node $0 insertWithCustomTimeoutAndRetrySettings "my-instance" "my-database" "my-project-id"'
)
.wrap(120)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/spanner/docs')
Expand Down
8 changes: 8 additions & 0 deletions samples/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,4 +876,12 @@ describe('Spanner', () => {
);
assert.match(output, /Backup deleted./);
});

// custom_timeout_and_retry
it('should insert with custom timeout and retry settings', async () => {
const output = execSync(
`${dmlCmd} insertWithCustomTimeoutAndRetrySettings ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`
);
assert.match(output, /record inserted./);
});
});