diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..883b08a8df --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +.git +.idea/ +target/ + +# Ignore Files +*png +*pt +*pyc +*pyo +*wav +*~ diff --git a/.github/workflows/publish-to-gpr.yml b/.github/workflows/publish-to-gpr.yml new file mode 100644 index 0000000000..46f76a9d81 --- /dev/null +++ b/.github/workflows/publish-to-gpr.yml @@ -0,0 +1,22 @@ +name: Publish Docker image to GitHub Package Registry +on: push +jobs: + build: + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/master' # Only on master + + steps: + - name: Copy Repo Files + uses: actions/checkout@master + + #This Action Emits 2 Variables, IMAGE_SHA_NAME and IMAGE_URL + #which you can reference in subsequent steps + - name: Publish Docker Image to GPR + uses: machine-learning-apps/gpr-docker-publish@master + id: docker + with: + IMAGE_NAME: 'tts-service-proxy' + DOCKERFILE_PATH: 'Dockerfile' + BUILD_CONTEXT: '.' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..a6eaf47f59 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +.idea/ +target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000000..812794eecc --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,921 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85bb70cc08ec97ca5450e6eba421deeea5f172c0fc61f78b5357b2a8e8be195f" + +[[package]] +name = "arc-swap" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi 0.3.9", +] + +[[package]] +name = "autocfg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "bytes" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "fuchsia-zircon" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" +dependencies = [ + "bitflags", + "fuchsia-zircon-sys", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" + +[[package]] +name = "futures" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e05b85ec287aac0dc34db7d4a569323df697f9c55b99b15d6b4ef8cde49f613" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f366ad74c28cca6ba456d95e6422883cfb4b252a83bed929c83abfdbbf2967d5" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399" + +[[package]] +name = "futures-executor" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10d6bb888be1153d3abeb9006b11b02cf5e9b209fda28693c31ae1e4e012e314" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789" + +[[package]] +name = "futures-macro" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0b5a30a4328ab5473878237c447333c093297bded83a4983d10f4deea240d39" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f2032893cb734c7a05d85ce0cc8b8c4075278e93b24b66f9de99d6eb0fa8acc" + +[[package]] +name = "futures-task" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdb66b5f09e22019b1ab0830f7785bcea8e7a42148683f99214f73f8ec21a626" +dependencies = [ + "once_cell", +] + +[[package]] +name = "futures-util" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8764574ff08b701a084482c3c7031349104b07ac897393010494beaa18ce32c6" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project", + "pin-utils", + "proc-macro-hack", + "proc-macro-nested", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "h2" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993f9e0baeed60001cf565546b0d3dbe6a6ad23f2bd31644a133c641eccf6d53" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hermit-abi" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9586eedd4ce6b3c498bc3b4dd92fc9f11166aa908a914071953768066c67909" +dependencies = [ + "libc", +] + +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi 0.3.9", +] + +[[package]] +name = "http" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13d5ff830006f7646652e057693569bfe0d51760c0085a071769d142a205111b" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "httparse" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" + +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + +[[package]] +name = "hyper" +version = "0.13.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e68a8dd9716185d9e64ea473ea6ef63529252e3e27623295a0378a19665d5eb" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "itoa", + "pin-project", + "socket2", + "time", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-reverse-proxy" +version = "0.5.0" +source = "git+https://github.com/felipenoris/hyper-reverse-proxy.git#d109fcbd629c62eeb5c34bec1074d737e7b58529" +dependencies = [ + "hyper", + "lazy_static", + "unicase", +] + +[[package]] +name = "indexmap" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c398b2b113b55809ceb9ee3e753fcbac793f1956663f3c36549c1346015c2afe" +dependencies = [ + "autocfg", +] + +[[package]] +name = "iovec" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" +dependencies = [ + "libc", +] + +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +dependencies = [ + "winapi 0.2.8", + "winapi-build", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" + +[[package]] +name = "log" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + +[[package]] +name = "memchr" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" + +[[package]] +name = "mio" +version = "0.6.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430" +dependencies = [ + "cfg-if", + "fuchsia-zircon", + "fuchsia-zircon-sys", + "iovec", + "kernel32-sys", + "libc", + "log", + "miow 0.2.1", + "net2", + "slab", + "winapi 0.2.8", +] + +[[package]] +name = "mio-named-pipes" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656" +dependencies = [ + "log", + "mio", + "miow 0.3.5", + "winapi 0.3.9", +] + +[[package]] +name = "mio-uds" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0" +dependencies = [ + "iovec", + "libc", + "mio", +] + +[[package]] +name = "miow" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" +dependencies = [ + "kernel32-sys", + "net2", + "winapi 0.2.8", + "ws2_32-sys", +] + +[[package]] +name = "miow" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07b88fb9795d4d36d62a012dfbf49a8f5cf12751f36d31a9dbe66d528e58979e" +dependencies = [ + "socket2", + "winapi 0.3.9", +] + +[[package]] +name = "net2" +version = "0.2.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7" +dependencies = [ + "cfg-if", + "libc", + "winapi 0.3.9", +] + +[[package]] +name = "newrelic" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7ec1eb312ccd33c2d88ff317d59164465a41014ad465ba8a67e2c062343e3a8" +dependencies = [ + "log", + "newrelic-sys", +] + +[[package]] +name = "newrelic-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a9b9030b5f433ac2afb956eeaf89020e07d3cb9b0561cd07f1737c97a4af93a" + +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" + +[[package]] +name = "pin-project" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca4433fff2ae79342e497d9f8ee990d174071408f28f726d6d83af93e58e48aa" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c0e815c3ee9a031fdf5af21c10aa17c573c9c6a566328d99e3936c34e36461f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282adbf10f2698a7a77f8e983a74b2d18176c19a7fd32a45446139ae7b02b715" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "ppv-lite86" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" + +[[package]] +name = "proc-macro-hack" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598" + +[[package]] +name = "proc-macro-nested" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" + +[[package]] +name = "proc-macro2" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" + +[[package]] +name = "regex" +version = "1.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", + "thread_local", +] + +[[package]] +name = "regex-syntax" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "serde" +version = "1.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3" + +[[package]] +name = "serde_derive" +version = "1.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec2c5d7e739bc07a3e73381a39d61fdb5f671c60c1df26a130690665803d8226" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "signal-hook-registry" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e12110bc539e657a646068aaf5eb5b63af9d0c1f7b29c97113fad80e15f035" +dependencies = [ + "arc-swap", + "libc", +] + +[[package]] +name = "slab" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" + +[[package]] +name = "socket2" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "winapi 0.3.9", +] + +[[package]] +name = "syn" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8d5d96e8cbb005d6959f119f773bfaebb5684296108fb32600c00cde305b2cd" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi 0.3.9", +] + +[[package]] +name = "tokio" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "iovec", + "lazy_static", + "libc", + "memchr", + "mio", + "mio-named-pipes", + "mio-uds", + "num_cpus", + "pin-project-lite", + "signal-hook-registry", + "slab", + "tokio-macros", + "winapi 0.3.9", +] + +[[package]] +name = "tokio-macros" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-util" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "toml" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" +dependencies = [ + "serde", +] + +[[package]] +name = "tower-service" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" + +[[package]] +name = "tracing" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d79ca061b032d6ce30c660fded31189ca0b9922bf483cd70759f13a2d86786c" +dependencies = [ + "cfg-if", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f0e00789804e99b20f12bc7003ca416309d28a6f495d6af58d1e2c2842461b5" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "try-lock" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" + +[[package]] +name = "tts-service-proxy" +version = "0.0.2" +dependencies = [ + "anyhow", + "env_logger", + "futures", + "hostname", + "hyper", + "hyper-reverse-proxy", + "log", + "newrelic", + "rand", + "serde", + "serde_derive", + "serde_json", + "tokio", + "toml", +] + +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi 0.3.9", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "ws2_32-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" +dependencies = [ + "winapi 0.2.8", + "winapi-build", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000000..4b1a26f644 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "tts-service-proxy" +edition = "2018" +version = "0.0.2" +authors = [ + "Brandon Thomas ", + "Brandon Thomas ", +] +publish = false + +[[bin]] +name = "tts-service-proxy" +path = "src/main.rs" + +[dependencies] +anyhow = "1.0.31" +env_logger = "0.7.1" +futures = "0.3" +hostname = "0.3.1" +hyper = "0.13" +hyper-reverse-proxy = { git = "https://github.com/felipenoris/hyper-reverse-proxy.git", commit = "d109fcbd629c62eeb5c34bec1074d737e7b58529" } +log = "0.4.8" +newrelic = "0.2.2" +rand = "0.7.3" +serde = "1.0.104" +serde_derive = "1.0.104" +serde_json = "1.0.55" +tokio = { version = "0.2", features = ["full"] } +toml = "0.5.6" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000..a93d6eaf82 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,63 @@ +# Unsure if I want to create minimal images yet that run under Alpine Linux. +# I have instructions for that here: https://hub.docker.com/r/echelon/testcontainer/dockerfile +FROM ubuntu:xenial as build +WORKDIR /tmp + +# libpcre3-dev is for newrelic +RUN apt-get update \ + && apt-get install -y \ + build-essential \ + curl \ + libpcre3-dev \ + libssl-dev \ + libssl1.0.0 \ + pkg-config + +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \ + | sh -s -- --default-toolchain stable -y + +COPY Cargo.lock . +COPY Cargo.toml . +COPY src/ ./src + +RUN $HOME/.cargo/bin/cargo fetch + +RUN curl https://github.com/newrelic/c-sdk/archive/v1.3.0.tar.gz -L -o newrelic.tar.gz +RUN tar -xf newrelic.tar.gz + +# NB: There's also a dynamic library target +# Static output is './c-sdk-1.3.0/libnewrelic.a' +RUN cd ./c-sdk-1.3.0 && make static +RUN cp ./c-sdk-1.3.0/libnewrelic.a . + +#RUN LD_LIBRARY_PATH=/usr/lib:${LD_LIBRARY_PATH} $HOME/.cargo/bin/cargo build +RUN LD_LIBRARY_PATH=/usr/lib:${LD_LIBRARY_PATH} $HOME/.cargo/bin/cargo build --release + +# Final image +FROM ubuntu:xenial +WORKDIR / + +#COPY --from=build /tmp/target/debug/tts-service-proxy / +COPY --from=build /tmp/target/release/tts-service-proxy / + +# SSL certs are required for crypto +COPY --from=build /etc/ssl /etc/ssl + +# Required dynamically linked libraries +COPY --from=build /lib/x86_64-linux-gnu/libssl.* /lib/x86_64-linux-gnu/ +COPY --from=build /lib/x86_64-linux-gnu/libcrypto.* /lib/x86_64-linux-gnu/ + +# Make sure all the links resolve +RUN ldd tts-service-proxy + +# Install things we want in the final container +RUN apt-get update \ + && apt-get install -y \ + curl \ + && apt-get clean + +COPY proxy_configs.toml . + +EXPOSE 8080 +CMD LD_LIBRARY_PATH=/usr/lib /tts-service-proxy + diff --git a/README.md b/README.md new file mode 100644 index 0000000000..9d22da4084 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +voder-proxy +=========== + +Intercept requests to `voder/tts_service` and determine the appropriate +backend to send them to. This acts as a load balancer of sorts. + diff --git a/proxy_configs.toml b/proxy_configs.toml new file mode 100644 index 0000000000..414d2e00e3 --- /dev/null +++ b/proxy_configs.toml @@ -0,0 +1,9 @@ +[[backends]] +voice = "trump" +ip = "127.0.0.1" +port = "12345" + +[[backends]] +voice = "glow-ljs-single" +ip = "localhost" +port = "12345" diff --git a/src/api.rs b/src/api.rs new file mode 100644 index 0000000000..3756ef5d52 --- /dev/null +++ b/src/api.rs @@ -0,0 +1,101 @@ +use anyhow::Result as AnyhowResult; +use futures::TryStreamExt; +use hyper::http::HeaderValue; +use hyper::{Body, Request, HeaderMap, Response, StatusCode}; + +/// NB: This much match the shape of SpeakRequest in the 'voder/tts_service' code. +/// This is used for both /speak and /speak_spectrogram requests. +#[derive(Deserialize, Debug)] +pub struct ExternalSpeakRequest { + /// Slug for the speaker + pub speaker: String, + + /// Raw text to be spoken + pub text: String, +} + +/// The full request +pub struct ExternalHttpRequest { + pub headers: HeaderMap, + pub speak_request: ExternalSpeakRequest, +} + +#[derive(Serialize, Debug)] +pub struct InternalSpeakRequest { + /// Slug for the speaker + pub speaker: String, + + /// Raw text to be spoken + pub text: String, + + /// Retry attempt. + /// The first request is "0", and all subsequent requests are incremented. + /// Subsequent requests do not trigger the rate limiter. + pub retry_attempt_number: u8, + + /// Admin/debug requests get to skip the rate limiter. + pub skip_rate_limiter: bool, +} + +pub enum ErrorType { + /// The proxy couldn't find a healthy host (or it died mid-connection) + ProxyError, + // The backend server reported a 500 + ServerError, + /// Client is rate limited + RateLimitError, +} + +pub struct ErrorResponse { + /// The type of error + pub error_type: ErrorType, + /// Description of the error + pub error_description: String, +} + +impl ExternalHttpRequest { + + pub async fn decode_request(request: Request) -> AnyhowResult { + let headers = request.headers().clone(); + let (_parts, body) = request.into_parts(); + + let body_bytes = body_to_bytes(body).await?; + let speak_request = serde_json::from_slice::(&body_bytes)?; + + Ok(ExternalHttpRequest { + headers: headers, + speak_request: speak_request, + }) + } +} + +impl ErrorType { + pub fn status_code(&self) -> StatusCode { + match *self { + ErrorType::ProxyError =>StatusCode::INTERNAL_SERVER_ERROR, + ErrorType::ServerError => StatusCode::INTERNAL_SERVER_ERROR, + ErrorType::RateLimitError => StatusCode::TOO_MANY_REQUESTS, + } + } +} + +impl ErrorResponse { + pub fn to_response(&self) -> Response { + let status_code = self.error_type.status_code(); + let body = Body::from(self.error_description.clone()); + Response::builder() + .status(status_code) + .body(body) + .unwrap() + } +} + +async fn body_to_bytes(body: Body) -> AnyhowResult> { + // Also: let body_bytes = hyper::body::to_bytes(body) + let bytes = body.try_fold(Vec::new(), |mut data, chunk| async move { + data.extend_from_slice(&chunk); + Ok(data) + }).await?; + + Ok(bytes) +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000000..4423c43d8a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,292 @@ +#[macro_use] extern crate anyhow; +#[macro_use] extern crate log; +#[macro_use] extern crate serde_derive; + +pub mod api; +pub mod newrelic_logger; +pub mod speak_proxy; + +use anyhow::Result as AnyhowResult; +use crate::newrelic_logger::NewRelicLogger; +use crate::speak_proxy::speak_proxy_with_retry; +use hyper::server::conn::AddrStream; +use hyper::service::{service_fn, make_service_fn}; +use hyper::{Body, Request, Response, Server}; +use hyper::{StatusCode, Method}; +use rand::seq::IteratorRandom; +use std::collections::HashMap; +use std::convert::Infallible; +use std::env; +use std::fmt::Debug; +use std::fmt::Display; +use std::net::IpAddr; +use std::str::FromStr; +use std::sync::Arc; + +const ENV_DEFAULT_ROUTE : &'static str = "DEFAULT_ROUTE"; +const ENV_MAX_RETRIES : &'static str = "MAX_RETRIES"; +const ENV_NEWRELIC_API_KEY : &'static str = "NEWRELIC_API_KEY"; +const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE"; +const ENV_RETRY_WAIT_MS : &'static str = "RETRY_WAIT_MS"; +const ENV_RUST_LOG : &'static str = "RUST_LOG"; +const ENV_SERVICE_PORT : &'static str = "SERVICE_PORT"; + +const DEFAULT_DEFAULT_ROUTE : &'static str = "http://127.0.0.1:12345"; +const DEFAULT_MAX_RETRIES : u8 = 3; +const DEFAULT_NEWRELIC_API_KEY : &'static str = ""; +const DEFAULT_PROXY_CONFIG_FILE : &'static str = "proxy_configs.toml"; +const DEFAULT_RETRY_WAIT_MS : u64 = 400; +const DEFAULT_RUST_LOG : &'static str = "tokio_reactor=warn,hyper=info,debug"; +const DEFAULT_SERVICE_PORT : u16 = 5555; + +#[derive(Deserialize, Debug, Clone)] +struct ProxyConfigs { + pub backends: Vec, +} + +#[derive(Deserialize, Debug, Clone)] +struct ProxyConfig { + pub voice : String, + pub ip: String, + pub port: String, +} + +impl ProxyConfigs { + pub fn load_from_file(filename: &str) -> AnyhowResult { + let contents = std::fs::read_to_string(filename)?; + let configs = toml::from_str(&contents)?; + Ok(configs) + } +} + +fn get_env_string(env_name: &str, default: &str) -> String { + match env::var(env_name).as_ref().ok() { + Some(s) => s.to_string(), + None => { + warn!("Env var '{}' not supplied. Using default '{}'.", env_name, default); + default.to_string() + }, + } +} + +// the trait `std::error::Error` is not implemented for `::Err` +fn get_env_num(env_name: &str, default: T) -> AnyhowResult + where T: FromStr + Display, + T::Err: Debug +{ + match env::var(env_name).as_ref().ok() { + None => { + warn!("Env var '{}' not supplied. Using default '{}'.", env_name, default); + Ok(default) + }, + Some(val) => { + match val.parse::() { + Err(e) => bail!("Can't parse value '{:?}'. Error: {:?}", val, e), + Ok(val) => Ok(val), + } + }, + } +} + +pub struct Router { + pub routes: HashMap, + pub default_route: String, +} + +impl Router { + fn get_random_host(&self) -> String { + let mut rng = rand::thread_rng(); + self.routes.values() + .choose(&mut rng) + .map(|url| url.to_string()) + .unwrap_or(self.default_route.clone()) + } + + fn get_speaker_host(&self, voice: &str) -> String { + self.routes.get(voice) + .map(|url| url.to_string()) + .unwrap_or(self.default_route.clone()) + } +} + +// fn debug_request(req: Request) -> Result, Infallible> { +// let body_str = format!("{:?}", req); +// Ok(Response::new(Body::from(body_str))) +// } + +fn health_check_response(_req: Request) -> Result, Infallible> { + Ok(Response::new(Body::from("healthy"))) +} + +pub async fn handle( + client_ip: IpAddr, + req: Request, + router: Arc, + newrelic_logger: Arc, + server_params: ServerParams) -> Result, Infallible> +{ + match (req.method(), req.uri().path()) { + (&Method::POST, "/speak") => { + let nr_transaction = newrelic_logger.web_transaction("/speak"); + let result = speak_proxy_with_retry( + client_ip, + req, + router, + nr_transaction, + server_params.max_retries, + server_params.retry_wait_ms, + &server_params.server_hostname, + "/speak").await; + match result { + Ok(response) => Ok(response), + Err(error) => Ok(error.to_response()), + } + }, + (&Method::POST, "/speak_spectrogram") => { + let nr_transaction = newrelic_logger.web_transaction("/speak_spectrogram"); + let result = speak_proxy_with_retry( + client_ip, + req, + router, + nr_transaction, + server_params.max_retries, + server_params.retry_wait_ms, + &server_params.server_hostname, + "/speak_spectrogram").await; + match result { + Ok(response) => Ok(response), + Err(error) => Ok(error.to_response()), + } + }, + (&Method::GET, "/proxy_health") => { + let _droppable_transaction = newrelic_logger.web_transaction("/proxy_health"); + health_check_response(req) + }, + _ => { + let _droppable_transaction = newrelic_logger + .web_transaction("unmatched endpoint"); + let forward = router.get_random_host(); + + info!("Forwarding to `{}` random host: {}", &forward, req.uri()); + + match hyper_reverse_proxy::call(client_ip, &forward, req).await { + Ok(response) => { + Ok(response) + }, + Err(_) => { + Ok(Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(Body::from("unknown proxy error")) + .unwrap()) + } + } + }, + } +} + +#[derive(Debug, Clone)] +pub struct ServerParams { + pub max_retries: u8, + pub retry_wait_ms: u64, + pub server_hostname: String, +} + +#[tokio::main] +async fn main() { + if env::var(ENV_RUST_LOG) + .as_ref() + .ok() + .is_none() + { + println!("Setting default logging level to \"{}\", override with env var {}.", + DEFAULT_RUST_LOG, ENV_RUST_LOG); + std::env::set_var(ENV_RUST_LOG, DEFAULT_RUST_LOG); + } + + env_logger::init(); + + let default_route = get_env_string(ENV_DEFAULT_ROUTE, DEFAULT_DEFAULT_ROUTE); + let service_port = get_env_num::(ENV_SERVICE_PORT, DEFAULT_SERVICE_PORT) + .expect("should have service port env var"); + let newrelic_api_key = get_env_string(ENV_NEWRELIC_API_KEY, DEFAULT_NEWRELIC_API_KEY); + + info!("Default route: {}", default_route); + + let proxy_configs_file = get_env_string(ENV_PROXY_CONFIG_FILE, DEFAULT_PROXY_CONFIG_FILE); + info!("Proxy config file: {}", proxy_configs_file); + + let max_retries = get_env_num::(ENV_MAX_RETRIES, DEFAULT_MAX_RETRIES) + .expect("should have max retries env var"); + + info!("Max retries: {}", max_retries); + + let retry_wait_ms = get_env_num::(ENV_RETRY_WAIT_MS, DEFAULT_RETRY_WAIT_MS) + .expect("should have retry wait ms env var"); + + info!("Retry wait: {}", retry_wait_ms); + + let proxy_configs = ProxyConfigs::load_from_file(&proxy_configs_file) + .expect("should have configs"); + + info!("Proxy configs: {:?}", proxy_configs); + + let server_hostname = hostname::get() + .ok() + .and_then(|h| h.into_string().ok()) + .unwrap_or("proxy-unknown".to_string()); + + let server_params = ServerParams { + max_retries, + retry_wait_ms, + server_hostname, + }; + + let mut route_map = HashMap::new(); + + for backend in proxy_configs.backends.iter() { + let host = format!("http://{}:{}", backend.ip, backend.port); + route_map.insert(backend.voice.clone(), host); + } + + let router = Arc::new(Router { + routes: route_map, + default_route: default_route, + }); + + let newrelic_logger = if newrelic_api_key.is_empty() { + NewRelicLogger::null_instance() + } else { + NewRelicLogger::try_new_or_null("voder-proxy", &newrelic_api_key) + }; + + let newrelic_logger = Arc::new(newrelic_logger); + + let make_service = make_service_fn(move |conn: &AddrStream| { + let remote_addr = conn.remote_addr().ip(); + let router2 = router.clone(); + let newrelic_logger2 = newrelic_logger.clone(); + let server_params = server_params.clone(); + + async move { + let router3 = router2.clone(); + let newrelic_logger3 = newrelic_logger2.clone(); + let server_params = server_params.clone(); + + Ok::<_, Infallible>(service_fn(move |req| { + let router4 = router3.clone(); + let newrelic_logger4 = newrelic_logger3.clone(); + handle(remote_addr, req, router4, newrelic_logger4, server_params.clone()) + })) + } + }); + + let addr = ([0, 0, 0, 0], service_port).into(); + + info!("Running server on {:?}", addr); + + let server = Server::bind(&addr).serve(make_service); + + if let Err(e) = server.await { + eprintln!("server error: {}", e); + } +} diff --git a/src/newrelic_logger.rs b/src/newrelic_logger.rs new file mode 100644 index 0000000000..4e654583a3 --- /dev/null +++ b/src/newrelic_logger.rs @@ -0,0 +1,56 @@ +use anyhow::Result as AnyhowResult; +use newrelic::{App, Transaction}; + +pub struct NewRelicLogger { + newrelic_api: Option, +} + +impl NewRelicLogger { + pub fn new(app_name: &str, api_key: &str) -> AnyhowResult { + let newrelic_api = App::new(app_name, api_key)?; + Ok(Self { + newrelic_api: Some(newrelic_api), + }) + } + + pub fn null_instance() -> Self { + Self { + newrelic_api: None, + } + } + + pub fn try_new_or_null(app_name: &str, api_key: &str) -> Self { + match Self::new(app_name, api_key) { + Ok(instance) => instance, + Err(_) => Self::null_instance(), + } + } + + pub fn web_transaction(&self, transaction_name: &str) -> MaybeNewRelicTransaction { + if let Some(ref newrelic_api) = self.newrelic_api { + info!("Logging request to NewRelic"); + let maybe_transaction = newrelic_api.web_transaction(transaction_name).ok(); + return MaybeNewRelicTransaction::from_optional(maybe_transaction); + } + MaybeNewRelicTransaction::from_optional(None) + } +} + +pub struct MaybeNewRelicTransaction { + transaction: Option, +} + +impl MaybeNewRelicTransaction { + pub fn from_optional(optional: Option) -> Self { + Self { + transaction: optional, + } + } + + pub fn add_attribute(&self, name: &str, value: &str) { + if let Some(ref transaction) = self.transaction { + info!("Logging attribute to NewRelic"); + let _result = transaction.add_attribute(name, value); + } + } +} \ No newline at end of file diff --git a/src/speak_proxy.rs b/src/speak_proxy.rs new file mode 100644 index 0000000000..4cb0ddddfa --- /dev/null +++ b/src/speak_proxy.rs @@ -0,0 +1,149 @@ +use crate::Router; +use crate::api::{ExternalHttpRequest, InternalSpeakRequest, ErrorResponse, ErrorType}; +use crate::newrelic_logger::{MaybeNewRelicTransaction}; +use hyper::Method; +use hyper::header::HeaderValue; +use hyper::http::header::HeaderName; +use hyper::{Body, Request, Response, HeaderMap}; +use std::fmt::Debug; +use std::net::IpAddr; +use std::sync::Arc; +use std::thread; +use std::time::Duration; + +pub async fn speak_proxy_with_retry( + ip_addr: IpAddr, + request: Request, + router: Arc, + _transaction: MaybeNewRelicTransaction, + max_retries: u8, + retry_wait_ms: u64, + proxy_hostname: &str, + endpoint: &'static str) -> Result, ErrorResponse> +{ + let mut attempt : u8 = 0; + + let external_http_request = ExternalHttpRequest::decode_request(request).await.unwrap(); + + // This is the IP of the Digital Ocean load balancer. + let gateway_ip = HeaderValue::from_str(&ip_addr.to_string()) + .ok() + .unwrap_or(HeaderValue::from_static("")); + + // This is the IP of the upstream client browser. + let upstream_client_ip = external_http_request.headers.get(HeaderName::from_static("x-forwarded-for")) + .map(|ip| ip.to_str().unwrap_or("")) + .unwrap_or(""); + + let upstream_client_ip = HeaderValue::from_str(upstream_client_ip) + .ok() + .unwrap_or(HeaderValue::from_static("")); + + let mut headers_to_proxy = external_http_request.headers.clone(); + + headers_to_proxy.insert(HeaderName::from_static("forwarded"), upstream_client_ip.clone()); + headers_to_proxy.insert(HeaderName::from_static("x-forwarded-for"), upstream_client_ip.clone()); + + // Unfortunately it looks like this middleware trounces the standard headers, so + // here we put it somewhere it won't be overwritten. + headers_to_proxy.insert(HeaderName::from_static("x-voder-proxy-for"), upstream_client_ip.clone()); + headers_to_proxy.insert(HeaderName::from_static("x-gateway-ip"), gateway_ip); + + // We're going to be changing the request body length, so remove the reported value + // so Hyper doesn't choke on any mismatch. + headers_to_proxy.remove(HeaderName::from_static("content-length")); + + // The proxy host is a Kubernetes load balancer. We're not doing the round robin magic here, + // so it makes sense to reuse the same value for each retry. + let load_balancer_hostname = router.get_speaker_host(&external_http_request.speak_request.speaker); + + loop { + let internal_speak_request = InternalSpeakRequest { + speaker: external_http_request.speak_request.speaker.clone(), + text: external_http_request.speak_request.text.clone(), + retry_attempt_number: attempt, + skip_rate_limiter: false, + }; + + let result = speak_proxy( + ip_addr, + internal_speak_request, + headers_to_proxy.clone(), + &load_balancer_hostname, + endpoint).await; + + match result { + Ok(good_response) => { + // All we want to do is add our special header + return Ok(modify_response(good_response, proxy_hostname, attempt)); + }, + Err(_) => {}, + } + + attempt += 1; + + thread::sleep(Duration::from_millis(retry_wait_ms)); // TODO: Needs backoff + jitter + + if attempt >= max_retries { + return Err(ErrorResponse { + error_type: ErrorType::ProxyError, + error_description: "max internal retry attempts reached".to_string(), + }); + } + } +} + +#[derive(Serialize, Debug)] +pub struct ResponseError { + pub reason: String, +} + +async fn speak_proxy( + ip_addr: IpAddr, + internal_speak_request: InternalSpeakRequest, + headers: HeaderMap, + load_balancer_hostname: &str, + endpoint: &'static str) -> Result, ()> +{ + info!("Routing {} for {} to {} (attempt # {})", endpoint, &internal_speak_request.speaker, + &load_balancer_hostname, internal_speak_request.retry_attempt_number); + + let mut request_builder = Request::builder() + .method(Method::POST) + .uri(endpoint); + + for (k, v) in headers.iter() { + // NB: Handling move because of builder class + request_builder = request_builder.header(k, v); + } + + let json_speak_request = serde_json::to_string(&internal_speak_request).unwrap(); + + let request = request_builder + .body(Body::from(json_speak_request)) + .unwrap(); + + match hyper_reverse_proxy::call(ip_addr, &load_balancer_hostname, request).await { + Ok(result) => Ok(result), + Err(_result) => Err(()), // TODO: Surface this? + } +} + +fn modify_response(mut response: Response, proxy_hostname: &str, retry_count: u8) + -> Response +{ + let headers = response.headers_mut(); + + let proxy_hostname = HeaderValue::from_str(proxy_hostname) + .ok() + .unwrap_or(HeaderValue::from_static("proxy-unknown")); + + let retry_count = HeaderValue::from_str(&retry_count.to_string()) + .ok() + .unwrap_or(HeaderValue::from_static("unknown")); + + headers.append(HeaderName::from_static("x-proxy-hostname"), proxy_hostname); + headers.append(HeaderName::from_static("x-retry-count"), retry_count); + + response +} \ No newline at end of file