/
circuit-breaker.js
58 lines (51 loc) · 2.02 KB
/
circuit-breaker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
const common = require("oci-common");
const identity = require("oci-identity");
const audit = require("oci-audit");
/* This script demostrates how to create a circuit breaker and share the circuit breaker among clients.
*/
const provider = new common.ConfigFileAuthenticationDetailsProvider();
const compartmentId = provider.getTenantId();
(async () => {
// Options for creating a circuit breaker
const options = {
timeout: 10000, // If our function takes longer than 10 seconds, trigger a failure
errorThresholdPercentage: 50, // When 50% of requests fail, trip the circuit
resetTimeout: 30000 // After 30 seconds, try again.
};
const clientConfiguration = {
circuitBreaker: new common.CircuitBreaker(options)
};
// Use the circuitBreaker to create identity client
const identityClient = new identity.IdentityClient(
{ authenticationDetailsProvider: provider },
clientConfiguration
);
// Share the same circuitBreaker to create audit client.
const auditClient = new audit.AuditClient(
{ authenticationDetailsProvider: provider },
clientConfiguration
);
let listUserReq = {
compartmentId: compartmentId
};
const response = await identityClient.listUsers(listUserReq);
console.log("List User Repsonse: ", response);
const endTime = new Date();
const offset = new Date().setDate(new Date().getDate() - 5);
const startTime = new Date(offset);
const listEventRequest = {
compartmentId: compartmentId,
startTime: startTime,
endTime: endTime
};
try {
const listEventResponse = await auditClient.listEvents(listEventRequest);
console.log("response: ", listEventResponse);
} catch (err) {
console.log("what is err: ", err);
}
})();