Class ProxyConnector

Description

Proxy connector

Features:

  • connection manager for simple proxy with/without http liveness probe
  • patroni compatible
  • stats page

Example

import { Server as HTTPServer } from 'http';
import { Server as TCPServer } from 'net';
import { inject, container, Types, Core } from '@biorate/inversion';
import { IConfig, Config } from '@biorate/config';
import { ProxyConnector } from '@biorate/proxy';

const httpPort = 8001;
const clientPort = 7001;
const serverPort = 7002;

export class Root extends Core() {
public static connect() {
const socket = new TCPSocket();
socket.connect(serverPort);
return socket;
}

@inject(ProxyConnector) public connector: ProxyConnector;

public http: HTTPServer;

public tcp: TCPServer;

protected constructor() {
super();
this.http = new HTTPServer();
this.http.listen(httpPort);
this.http.on('request', (req, res) => {
res.writeHead(200);
res.end('1');
});
this.tcp = new TCPServer();
this.tcp.listen(clientPort);
this.tcp.on('connection', (socket) =>
socket.on('data', (data) => socket.write(`${data} world!`)),
);
}
}

container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
container.bind<ProxyConnector>(ProxyConnector).toSelf().inSingletonScope();
container.bind<Root>(Root).toSelf().inSingletonScope();

container.get<IConfig>(Types.Config).merge({
Proxy: [
{
name: 'connection',
retry: 10,
server: {
address: {
host: 'localhost',
port: serverPort,
},
},
clients: [
{
liveness: `http://localhost:${httpPort}`,
address: {
host: 'localhost',
port: clientPort,
},
},
],
},
],
});

(async () => {
const root = <Root>container.get<Root>(Root);
await root.$run();
const socket = Root.connect();
socket.write('Hello');
socket.on('data', (data) => {
console.log(data.toString()); // Hello world!
});
})();

Hierarchy

Constructors

Properties

#connections: Map<string, ProxyConnection>

Description

Private connections storage

#connections: Map<string, ProxyConnection> = ...

Description

Private connections storage

#current: ProxyConnection

Description

Private link to selected (used) connection

#current: ProxyConnection = undefined

Description

Private link to selected (used) connection

config: IConfig

Description

Config dependency

namespace: "Proxy" = 'Proxy'

Description

Namespace path for fetching configuration

Accessors

Methods

  • Returns {
        active: boolean;
        backendHost: string;
        connection: string;
        proxyHost: string;
        read: number;
        write: number;
    }[]

    Description

    Get stat data

Generated using TypeDoc