• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL
CLOSE
launchdarkly.com

EDIT ON GITHUB

Using the Lua SDK with HAProxy

Read time: 1 minute
Last edited: Jul 25, 2020

Overview

This guide explains how to use our Lua server-side SDK with HAProxy. You can use HAProxy with LaunchDarkly to implement dynamic rate limiting, access controls, rollout, and many other features at the edge of your application architecture.

You can extend HAProxy with Lua 5.3, enabling complex control of HAProxy functionality. HAProxy has substantial commercial adoption.

Find a basic Dockerized app in our GitHub repository at hello-haproxy.

Prerequisites

To complete this guide, you must have the following prerequisites:

  • Basic working knowledge of the LaunchDarkly Lua server-side SDK
  • Basic working knowledge of the LaunchDarkly C/C++ server-side SDK

The C server-side SDK is required because the Lua server-side SDK is implemented as a wrapper around the C server-side SDK.

Preparing the C/C++ server-side SDK

Make the binary of the C/C++ server-side SDK accessible to the dynamic linker. The most convenient way to do this is to install the binary system-wide at /usr/lib/libldserverapi.so.

Ensuring correct initialization

The most important part of effective SDK usage is managing the lifetime of clients correctly. When HAProxy utilizes process based concurrency, multiple clients initiate. If you accidentally initiate a client per request, the application is substantially slower because the SDK has to download a fresh ruleset from LaunchDarkly.

For ideal operations, initialize each HAProxy worker process exactly once. You can do this with the lua-load directive under global. This directive executes a script when a process is freshly spawned. Client initialization should reside in this script. In the example below, this file is called shared.lua.

Here is an example of initialization logic:

1local ld = require("launchdarkly_server_sdk")
2local os = require("os")
3
4local config = {
5 key = os.getenv("LD_SDK_KEY")
6}
7
8local client = ld.clientInit(config, 1000)

Later we can use the result of this initialization process in other services.

1core.register_service("launchdarkly", "http", function(applet)
2 applet:start_response()
3
4 local user = ld.makeUser({
5 key = "abc"
6 })
7
8 if client:boolVariation(user, os.getenv("LD_FLAG_KEY"), false) then
9 applet:send("<p>feature launched</p>")
10 else
11 applet:send("<p>feature not launched</p>")
12 end
13end)