Some test text!

Search
Hamburger Icon

Web / FAQ / Manual Load Balancing

Manual Load Balancing with HAProxy

WebViewer Server comes with a load balancer known as HAProxy located at our pdftron/wv-loadbalancer repository on DockerHub. This can be used in conjunction with WebViewer Server to load balance.

This guide details how to manage load balancing using the HAProxy image, in a way that is extensible to any environment.

How it works

HAProxy contains network entries called webAwebBwebC ... and so on. These entries indicate the location of a server that HAProxy can send requests to. The methods here will modify these entries to point at the correct server.

By default WebViewer Server does all load balancing using the leastconn method. This sends incoming requests to servers with the smallest number of active connections.

Setting initial servers

You can set the initial locations of these webX entries by setting the environment variables INITIAL_NODE_AINITIAL_NODE_B ... up to INITIAL_NODE_F on the wv-loadbalancer image. HAProxy will initially send requests to any healthy and responding servers set in these variables.

Setting servers dynamically

To set servers dynamically with HAProxy you can take advantage of something known as TCP Management - this can be used to change these webX entries to the current location of a server.

There are several ways to send requests to the server with this method, but all commands to HAProxy will be sent with the same structure.

set server nodes/[webX] addr [WVS ip address] [WVS port]

  • Locally in the server hosting HAProxy using sockets - this can be done through a socket communication application such as socat
socat /var/run/hapee.sock set server nodes/webA addr 192.168.1.1 8090
  • Remotely by setting ENABLE_TCP_MANAGEMENT to true on the Docker image and sending requests to the TCP Management port 4893 wherever the balancer is located. The following code does this using Node.js.
const connectSocket = (balancer) => {
      return new Promise((resolve, reject) => {
        var socket = new net.createConnection(4893, balancer);
        socket.on('connect', () => {
            resolve(socket);
        });

        socket.on('error', () => {
            reject(balancer + ": Balancer not online.");
        });
      });
};
const writeSocket = (command, socket, forceClose) => {
      return new Promise((resolve, reject) => {
          console.log(command);
          socket.write(command, (error, data) => {
            if (error) {
              console.log(error);
              reject();
            }
          });

          socket.on('data', (data) => {
              //console.log("balancer response:" + data);
              socket.end();
              resolve(data);
          });
      });
};
command = "set server nodes/webA addr 192.168.1.1 8090\n"
var socket = await connectSocket("insert balancer ip here");
await writeSocket(command, socket, false)

The above code sets webA to be the Webviewer Server at 192.168.1.1:8090. HAProxy will now send webA requests to this server.

Whenever a change occurs, you can dynamically rewrite remotely using the above code. The changes will instantly occur. More commands can be found at the HAProxy Management documentation

Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales