* Create a server
(topology: Topology, description: ServerDescription, options: ServerOptions)
| 145 | * Create a server |
| 146 | */ |
| 147 | constructor(topology: Topology, description: ServerDescription, options: ServerOptions) { |
| 148 | super(); |
| 149 | this.on('error', noop); |
| 150 | |
| 151 | this.serverApi = options.serverApi; |
| 152 | |
| 153 | const poolOptions = { hostAddress: description.hostAddress, ...options }; |
| 154 | |
| 155 | this.topology = topology; |
| 156 | this.pool = new ConnectionPool(this, poolOptions); |
| 157 | |
| 158 | this.s = { |
| 159 | description, |
| 160 | options, |
| 161 | state: STATE_CLOSED, |
| 162 | operationCount: 0 |
| 163 | }; |
| 164 | |
| 165 | for (const event of [...CMAP_EVENTS, ...APM_EVENTS]) { |
| 166 | this.pool.on(event, (e: any) => this.emit(event, e)); |
| 167 | } |
| 168 | |
| 169 | this.pool.on(Connection.CLUSTER_TIME_RECEIVED, (clusterTime: ClusterTime) => { |
| 170 | this.clusterTime = clusterTime; |
| 171 | }); |
| 172 | |
| 173 | if (this.loadBalanced) { |
| 174 | this.monitor = null; |
| 175 | // monitoring is disabled in load balancing mode |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | // create the monitor |
| 180 | this.monitor = new Monitor(this, this.s.options); |
| 181 | |
| 182 | for (const event of HEARTBEAT_EVENTS) { |
| 183 | this.monitor.on(event, (e: any) => this.emit(event, e)); |
| 184 | } |
| 185 | |
| 186 | this.monitor.on('resetServer', (error: MongoServerError) => markServerUnknown(this, error)); |
| 187 | this.monitor.on(Server.SERVER_HEARTBEAT_SUCCEEDED, (event: ServerHeartbeatSucceededEvent) => { |
| 188 | this.emit( |
| 189 | Server.DESCRIPTION_RECEIVED, |
| 190 | new ServerDescription(this.description.hostAddress, event.reply, { |
| 191 | roundTripTime: this.monitor?.roundTripTime, |
| 192 | minRoundTripTime: this.monitor?.minRoundTripTime |
| 193 | }) |
| 194 | ); |
| 195 | |
| 196 | if (this.s.state === STATE_CONNECTING) { |
| 197 | stateTransition(this, STATE_CONNECTED); |
| 198 | this.emit(Server.CONNECT, this); |
| 199 | } |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | get clusterTime(): ClusterTime | undefined { |
| 204 | return this.topology.clusterTime; |
nothing calls this directly
no test coverage detected