Packages
unleash_fresha
5.0.0
5.1.4
5.1.3
5.1.2
5.1.1
5.1.0
5.0.0
4.0.0
4.0.0-git-bda3
3.0.1
3.0.0
2.1.0
2.0.1
2.0.0-git-75d8
2.0.0-git-67b2
2.0.0-git-4b61
2.0.0-git-49f4
1.16.0
1.15.0
1.14.1-git-e754
1.14.0
1.14.0-git-71e9
1.13.0
1.13.0-git-4ba6
1.12.0
1.11.0
1.11.0-git-1b62
1.10.2
1.10.2-git-b835
1.10.1
1.10.0
1.9.3-alpha0
1.9.2
1.9.1
1.9.0
An Unleash Feature Flag client for Elixir, forked from [unleash](https://gitlab.com/afontaine/unleash_ex)
Current section
Files
Jump to
Current section
Files
lib/propagation/grpc/context_client_interceptor.ex
defmodule Unleash.Propagation.GRPC.ContextClientInterceptor do
@moduledoc """
A gRPC Client Interceptor designed to propagate Unleash context and overrides downstream.
The context and overrides must be established in the propagation context,
either manually (using `Unleash.Propagation.put_context/1`)
or through middleware, such as
`Unleash.Propagation.GRPC.ContextClientInterceptor` or
`Unleash.Propagation.Plugs.extract_unleash_context/2`.
For further details on the propagation mechanism, see `Unleash.Propagation`.
Note that this interceptor will raise if it finds invalid context or overrides values
in the propagation context that it can't serialize. This can happen if you put values
in the propagation context manually and you do so incorrectly.
If you'd like to prevent such instances from breaking your gRPC requests,
you can easily build your own interceptor that wraps this one and catches errors, for example:
defmodule MyUnleashContextClientInterceptorWrapper do
@moduledoc false
@behaviour GRPC.ClientInterceptor
@impl true
def init(_), do: nil
def call(stream, req, next, opts) do
Unleash.Propagation.GRPC.ContextClientInterceptor.call(stream, req, next, opts)
rescue
err ->
Logger.error("your error message here")
next.(stream, req) # Continue the gRPC request processing
end
end
"""
@behaviour GRPC.ClientInterceptor
import Unleash.Propagation.Serialization, only: [serialize_context!: 1, serialize_overrides!: 1]
alias Unleash.Propagation
@impl true
def init(_), do: nil
@impl true
def call(stream, req, next, _opts) do
context = Propagation.get_context()
overrides = Propagation.get_overrides()
headers =
Map.filter(
%{
"x-unleash-context" => if(is_nil(context), do: nil, else: serialize_context!(context)),
"x-unleash-overrides" =>
if(is_nil(overrides), do: nil, else: serialize_overrides!(overrides))
},
fn {_key, val} -> not is_nil(val) end
)
stream = Map.update(stream, :headers, headers, &Map.merge(&1, headers))
next.(stream, req)
end
end