Use balancer_by_lua_block in token_server.conf

* Remove `YT_POT_BGUTIL_BASE_URL`

* Prioritize the linked container details
This commit is contained in:
tcely
2025-12-31 06:29:25 -05:00
committed by GitHub
parent 75216bf2b1
commit 7b8e02a8de
3 changed files with 71 additions and 25 deletions

View File

@@ -9,7 +9,13 @@ worker_processes auto;
worker_cpu_affinity auto;
pid /run/nginx.pid;
env YT_POT_BGUTIL_BASE_URL;
# link configuration
env POTSERVER_PORT_4416_TCP_ADDR;
env POTSERVER_PORT_4416_TCP_PORT;
# user configuration
env TUBESYNC_POT_HTTPS;
env TUBESYNC_POT_IPADDR;
env TUBESYNC_POT_PORT;
events {
worker_connections 1024;

View File

@@ -1,5 +1,68 @@
lua_shared_dict token_server 1m;
upstream token_server {
server 127.0.0.1:4406 max_fails=0;
balancer_by_lua_block {
local balancer = require "ngx.balancer"
local upstream = ngx.shared.token_server
local exit_error = ngx.HTTP_INTERNAL_SERVER_ERROR
local ok, err, empty_port, host, port
local default_host, default_port, tries = '127.0.0.1', '4406', 1
local user_host = os.getenv('TUBESYNC_POT_IPADDR')
local link_host = os.getenv('POTSERVER_PORT_4416_TCP_ADDR')
local use_https = os.getenv('TUBESYNC_POT_HTTPS')
if user_host and use_https then
use_https = true
empty_port = '443'
else
use_https = false
empty_port = '80'
end
ok, err = balancer.get_last_failure()
if ok then
host, port, tries = default_host, default_port, 0
elseif link_host then
host = link_host
local link_port = os.getenv('POTSERVER_PORT_4416_TCP_PORT')
if link_port then
port = link_port
else
port = empty_port
end
elseif user_host then
host = user_host
local user_port = os.getenv('TUBESYNC_POT_PORT')
if user_port then
port = user_port
else
port = empty_port
end
ok, err = balancer.set_upstream_tls(use_https)
if not ok then
ngx.log(ngx.ERR, "failed to set upstream TLS: ", err)
end
end
if not host then
host, port, tries = default_host, default_port, 0
end
ok, err = balancer.set_more_tries(tries)
if not ok then
ngx.log(ngx.err, ("failed to set tries to %d: "):format(tries), err)
end
ok, err = balancer.set_current_peer(host, port)
if ok then
upstream:set('addr', host)
upstream:set('port', port)
else
ngx.log(ngx.err, "failed to set the current peer: ", err)
return ngx.exit(exit_error)
end
}
}
server {
@@ -11,19 +74,7 @@ server {
# Server domain name
server_name _;
set_by_lua_block $pot_url {
local default = 'http://token_server'
local url = os.getenv('YT_POT_BGUTIL_BASE_URL')
if not url then
return default
end
if #url and url:find('://') then
return url
end
return default
}
location / {
proxy_pass $pot_url;
proxy_pass http://token_server;
}
}

View File

@@ -2,15 +2,4 @@
cd /
https="${TUBESYNC_POT_HTTPS:+https}"
ip_address="${TUBESYNC_POT_IPADDR:-${POTSERVER_PORT_4416_TCP_ADDR}}"
: "${TUBESYNC_POT_PORT:=${POTSERVER_PORT_4416_TCP_PORT}}"
port="${TUBESYNC_POT_PORT:+:}${TUBESYNC_POT_PORT}"
if [ -n "${ip_address}" ]
then
YT_POT_BGUTIL_BASE_URL="${https:-http}://${ip_address}${port}"
export YT_POT_BGUTIL_BASE_URL
fi
exec /usr/bin/openresty -c /etc/nginx/nginx.conf -e stderr