Debian Codegraph @

lib/dispatcher/round-robin-pool.js

raw
1 'use strict'
2
3 const {
4 PoolBase,
5 kClients,
6 kNeedDrain,
7 kAddClient,
8 kGetDispatcher,
9 kHasDispatcher,
10 kRemoveClient
11 } = require('./pool-base')
12 const Client = require('./client')
13 const {
14 InvalidArgumentError
15 } = require('../core/errors')
16 const util = require('../core/util')
17 const { kUrl } = require('../core/symbols')
18 const buildConnector = require('../core/connect')
19
20 const kOptions = Symbol('options')
21 const kConnections = Symbol('connections')
22 const kFactory = Symbol('factory')
23 const kIndex = Symbol('index')
24
25 function defaultFactory (origin, opts) {
26 return new Client(origin, opts)
27 }
28
29 class RoundRobinPool extends PoolBase {
30 constructor (origin, {
31 connections,
32 factory = defaultFactory,
33 connect,
34 connectTimeout,
35 tls,
36 maxCachedSessions,
37 socketPath,
38 autoSelectFamily,
39 autoSelectFamilyAttemptTimeout,
40 allowH2,
41 clientTtl,
42 ...options
43 } = {}) {
44 if (connections != null && (!Number.isFinite(connections) || connections < 0)) {
45 throw new InvalidArgumentError('invalid connections')
46 }
47
48 if (typeof factory !== 'function') {
49 throw new InvalidArgumentError('factory must be a function.')
50 }
51
52 if (connect != null && typeof connect !== 'function' && typeof connect !== 'object') {
53 throw new InvalidArgumentError('connect must be a function or an object')
54 }
55
56 if (typeof connect !== 'function') {
57 connect = buildConnector({
58 ...tls,
59 maxCachedSessions,
60 allowH2,
61 socketPath,
62 timeout: connectTimeout,
63 ...(typeof autoSelectFamily === 'boolean' ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
64 ...connect
65 })
66 }
67
68 super()
69
70 this[kConnections] = connections || null
71 this[kUrl] = util.parseOrigin(origin)
72 this[kOptions] = { ...util.deepClone(options), connect, allowH2, clientTtl, socketPath }
73 this[kFactory] = factory
74 this[kIndex] = -1
75
76 this.on('connect', (origin, targets) => {
77 if (clientTtl != null && clientTtl > 0) {
78 for (const target of targets) {
79 Object.assign(target, { ttl: Date.now() })
80 }
81 }
82 })
83
84 this.on('connectionError', (origin, targets, error) => {
85 for (const target of targets) {
86 const idx = this[kClients].indexOf(target)
87 if (idx !== -1) {
88 this[kClients].splice(idx, 1)
89 }
90 }
91 })
92 }
93
94 [kGetDispatcher] () {
95 const clientTtlOption = this[kOptions].clientTtl
96
97 // If we have no clients yet, create one
98 if (this[kClients].length === 0) {
99 const dispatcher = this[kFactory](this[kUrl], this[kOptions])
100 this[kAddClient](dispatcher)
101 return dispatcher
102 }
103
104 // Round-robin through existing clients
105 let checked = 0
106 while (checked < this[kClients].length) {
107 this[kIndex] = (this[kIndex] + 1) % this[kClients].length
108 const client = this[kClients][this[kIndex]]
109
110 // Check if client is stale (TTL expired)
111 if (clientTtlOption != null && clientTtlOption > 0 && client.ttl && ((Date.now() - client.ttl) > clientTtlOption)) {
112 this[kRemoveClient](client)
113 this[kIndex]--
114 continue
115 }
116
117 // Return client if it's not draining
118 if (!client[kNeedDrain]) {
119 return client
120 }
121
122 checked++
123 }
124
125 // All clients are busy, create a new one if we haven't reached the limit
126 if (!this[kConnections] || this[kClients].length < this[kConnections]) {
127 const dispatcher = this[kFactory](this[kUrl], this[kOptions])
128 this[kAddClient](dispatcher)
129 return dispatcher
130 }
131 }
132
133 [kHasDispatcher] () {
134 const clientTtlOption = this[kOptions].clientTtl
135 for (let i = 0; i < this[kClients].length; i++) {
136 const client = this[kClients][i]
137
138 if (clientTtlOption != null && clientTtlOption > 0 && client.ttl && ((Date.now() - client.ttl) > clientTtlOption)) {
139 this[kRemoveClient](client)
140 if (i <= this[kIndex]) {
141 this[kIndex]--
142 }
143 i--
144 } else if (!client[kNeedDrain]) {
145 return true
146 }
147 }
148
149 if (!this[kConnections] || this[kClients].length < this[kConnections]) {
150 const dispatcher = this[kFactory](this[kUrl], this[kOptions])
151 this[kAddClient](dispatcher)
152 return true
153 }
154
155 return false
156 }
157 }
158
159 module.exports = RoundRobinPool