1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import { StatusObject } from './call-interface';
|
19 | import { Metadata } from './metadata';
|
20 | import { Status } from './constants';
|
21 | import { LoadBalancer } from './load-balancer';
|
22 | import { SubchannelInterface } from './subchannel-interface';
|
23 |
|
24 | export enum PickResultType {
|
25 | COMPLETE,
|
26 | QUEUE,
|
27 | TRANSIENT_FAILURE,
|
28 | DROP,
|
29 | }
|
30 |
|
31 | export interface PickResult {
|
32 | pickResultType: PickResultType;
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 | subchannel: SubchannelInterface | null;
|
39 | |
40 |
|
41 |
|
42 |
|
43 | status: StatusObject | null;
|
44 | onCallStarted: (() => void) | null;
|
45 | onCallEnded: ((statusCode: Status) => void) | null;
|
46 | }
|
47 |
|
48 | export interface CompletePickResult extends PickResult {
|
49 | pickResultType: PickResultType.COMPLETE;
|
50 | subchannel: SubchannelInterface | null;
|
51 | status: null;
|
52 | onCallStarted: (() => void) | null;
|
53 | onCallEnded: ((statusCode: Status) => void) | null;
|
54 | }
|
55 |
|
56 | export interface QueuePickResult extends PickResult {
|
57 | pickResultType: PickResultType.QUEUE;
|
58 | subchannel: null;
|
59 | status: null;
|
60 | onCallStarted: null;
|
61 | onCallEnded: null;
|
62 | }
|
63 |
|
64 | export interface TransientFailurePickResult extends PickResult {
|
65 | pickResultType: PickResultType.TRANSIENT_FAILURE;
|
66 | subchannel: null;
|
67 | status: StatusObject;
|
68 | onCallStarted: null;
|
69 | onCallEnded: null;
|
70 | }
|
71 |
|
72 | export interface DropCallPickResult extends PickResult {
|
73 | pickResultType: PickResultType.DROP;
|
74 | subchannel: null;
|
75 | status: StatusObject;
|
76 | onCallStarted: null;
|
77 | onCallEnded: null;
|
78 | }
|
79 |
|
80 | export interface PickArgs {
|
81 | metadata: Metadata;
|
82 | extraPickInfo: { [key: string]: string };
|
83 | }
|
84 |
|
85 | /**
|
86 | * A proxy object representing the momentary state of a load balancer. Picks
|
87 | * subchannels or returns other information based on that state. Should be
|
88 | * replaced every time the load balancer changes state.
|
89 | */
|
90 | export interface Picker {
|
91 | pick(pickArgs: PickArgs): PickResult;
|
92 | }
|
93 |
|
94 | /**
|
95 | * A standard picker representing a load balancer in the TRANSIENT_FAILURE
|
96 | * state. Always responds to every pick request with an UNAVAILABLE status.
|
97 | */
|
98 | export class UnavailablePicker implements Picker {
|
99 | private status: StatusObject;
|
100 | constructor(status?: StatusObject) {
|
101 | if (status !== undefined) {
|
102 | this.status = status;
|
103 | } else {
|
104 | this.status = {
|
105 | code: Status.UNAVAILABLE,
|
106 | details: 'No connection established',
|
107 | metadata: new Metadata(),
|
108 | };
|
109 | }
|
110 | }
|
111 | pick(pickArgs: PickArgs): TransientFailurePickResult {
|
112 | return {
|
113 | pickResultType: PickResultType.TRANSIENT_FAILURE,
|
114 | subchannel: null,
|
115 | status: this.status,
|
116 | onCallStarted: null,
|
117 | onCallEnded: null
|
118 | };
|
119 | }
|
120 | }
|
121 |
|
122 | /**
|
123 | * A standard picker representing a load balancer in the IDLE or CONNECTING
|
124 | * state. Always responds to every pick request with a QUEUE pick result
|
125 | * indicating that the pick should be tried again with the next `Picker`. Also
|
126 | * reports back to the load balancer that a connection should be established
|
127 | * once any pick is attempted.
|
128 | */
|
129 | export class QueuePicker {
|
130 | private calledExitIdle = false;
|
131 | // Constructed with a load balancer. Calls exitIdle on it the first time pick is called
|
132 | constructor(private loadBalancer: LoadBalancer) {}
|
133 |
|
134 | pick(pickArgs: PickArgs): QueuePickResult {
|
135 | if (!this.calledExitIdle) {
|
136 | process.nextTick(() => {
|
137 | this.loadBalancer.exitIdle();
|
138 | });
|
139 | this.calledExitIdle = true;
|
140 | }
|
141 | return {
|
142 | pickResultType: PickResultType.QUEUE,
|
143 | subchannel: null,
|
144 | status: null,
|
145 | onCallStarted: null,
|
146 | onCallEnded: null
|
147 | };
|
148 | }
|
149 | }
|
150 |
|
\ | No newline at end of file |