Skip to content

Commit

Permalink
fix: interceptor should send only once per client (#384)
Browse files Browse the repository at this point in the history
Previously we used a `static` variable to keep track of whether the
agent header was sent. This would send one agent header
unconditionally of the client type.

In order to send a single agent header per client type (cache vs
topics vs store) we make the variable an instance variable of the
`HeaderInterceptor`.
  • Loading branch information
malandis authored Jul 8, 2024
1 parent b3d219b commit 8ed2e86
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions sdk/src/grpc/header_interceptor.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use std::convert::TryFrom;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

#[derive(Clone)]
pub struct HeaderInterceptor {
auth_token: String,
sdk_agent: String,
are_only_once_header_sent: Arc<AtomicBool>,
}

impl HeaderInterceptor {
pub fn new(authorization: &str, sdk_agent: &str) -> HeaderInterceptor {
HeaderInterceptor {
auth_token: authorization.to_string(),
sdk_agent: sdk_agent.to_string(),
are_only_once_header_sent: Arc::new(AtomicBool::new(false)),
}
}

Expand All @@ -34,17 +37,16 @@ impl tonic::service::Interceptor for HeaderInterceptor {
&mut self,
mut request: tonic::Request<()>,
) -> Result<tonic::Request<()>, tonic::Status> {
static ARE_ONLY_ONCE_HEADER_SENT: AtomicBool = AtomicBool::new(false);

self.insert_header(&mut request, "authorization", &self.auth_token)?;

if !ARE_ONLY_ONCE_HEADER_SENT.load(Ordering::Relaxed) {
if !self.are_only_once_header_sent.load(Ordering::Relaxed) {
self.insert_header(&mut request, "agent", &self.sdk_agent)?;

// Because the `runtime-version` header makes more sense for interpreted languages,
// we send this sentinel value to ensure we report *some* value for this sdk.
self.insert_header(&mut request, "runtime-version", "rust")?;
ARE_ONLY_ONCE_HEADER_SENT.store(true, Ordering::Relaxed);
self.are_only_once_header_sent
.store(true, Ordering::Relaxed);
}

Ok(request)
Expand Down

0 comments on commit 8ed2e86

Please sign in to comment.