-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extract hints from http header (#5128)
* feat: extract hints from http header * Update src/servers/src/http/hints.rs Co-authored-by: shuiyisong <[email protected]> * chore: by comment * refactor: get instead of loop --------- Co-authored-by: shuiyisong <[email protected]>
- Loading branch information
1 parent
c6b7caa
commit 9b4e855
Showing
5 changed files
with
207 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use http::HeaderMap; | ||
use tonic::metadata::MetadataMap; | ||
|
||
pub const HINT_KEYS: [&str; 5] = [ | ||
"x-greptime-hint-auto_create_table", | ||
"x-greptime-hint-ttl", | ||
"x-greptime-hint-append_mode", | ||
"x-greptime-hint-merge_mode", | ||
"x-greptime-hint-physical_table", | ||
]; | ||
|
||
pub(crate) fn extract_hints<T: ToHeaderMap>(headers: &T) -> Vec<(String, String)> { | ||
let mut hints = Vec::new(); | ||
for key in HINT_KEYS.iter() { | ||
if let Some(value) = headers.get(key) { | ||
let new_key = key.replace("x-greptime-hint-", ""); | ||
hints.push((new_key, value.trim().to_string())); | ||
} | ||
} | ||
hints | ||
} | ||
|
||
pub(crate) trait ToHeaderMap { | ||
fn get(&self, key: &str) -> Option<&str>; | ||
} | ||
|
||
impl ToHeaderMap for MetadataMap { | ||
fn get(&self, key: &str) -> Option<&str> { | ||
self.get(key).and_then(|v| v.to_str().ok()) | ||
} | ||
} | ||
|
||
impl ToHeaderMap for HeaderMap { | ||
fn get(&self, key: &str) -> Option<&str> { | ||
self.get(key).and_then(|v| v.to_str().ok()) | ||
} | ||
} | ||
#[cfg(test)] | ||
mod tests { | ||
use http::header::{HeaderMap, HeaderValue}; | ||
use tonic::metadata::{MetadataMap, MetadataValue}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_extract_hints_with_full_header_map() { | ||
let mut headers = HeaderMap::new(); | ||
headers.insert( | ||
"x-greptime-hint-auto_create_table", | ||
HeaderValue::from_static("true"), | ||
); | ||
headers.insert("x-greptime-hint-ttl", HeaderValue::from_static("3600d")); | ||
headers.insert( | ||
"x-greptime-hint-append_mode", | ||
HeaderValue::from_static("true"), | ||
); | ||
headers.insert( | ||
"x-greptime-hint-merge_mode", | ||
HeaderValue::from_static("false"), | ||
); | ||
headers.insert( | ||
"x-greptime-hint-physical_table", | ||
HeaderValue::from_static("table1"), | ||
); | ||
|
||
let hints = extract_hints(&headers); | ||
|
||
assert_eq!(hints.len(), 5); | ||
assert_eq!( | ||
hints[0], | ||
("auto_create_table".to_string(), "true".to_string()) | ||
); | ||
assert_eq!(hints[1], ("ttl".to_string(), "3600d".to_string())); | ||
assert_eq!(hints[2], ("append_mode".to_string(), "true".to_string())); | ||
assert_eq!(hints[3], ("merge_mode".to_string(), "false".to_string())); | ||
assert_eq!( | ||
hints[4], | ||
("physical_table".to_string(), "table1".to_string()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_extract_hints_with_missing_keys() { | ||
let mut headers = HeaderMap::new(); | ||
headers.insert( | ||
"x-greptime-hint-auto_create_table", | ||
HeaderValue::from_static("true"), | ||
); | ||
headers.insert("x-greptime-hint-ttl", HeaderValue::from_static("3600d")); | ||
|
||
let hints = extract_hints(&headers); | ||
|
||
assert_eq!(hints.len(), 2); | ||
assert_eq!( | ||
hints[0], | ||
("auto_create_table".to_string(), "true".to_string()) | ||
); | ||
assert_eq!(hints[1], ("ttl".to_string(), "3600d".to_string())); | ||
} | ||
|
||
#[test] | ||
fn test_extract_hints_with_metadata_map() { | ||
let mut metadata = MetadataMap::new(); | ||
metadata.insert( | ||
"x-greptime-hint-auto_create_table", | ||
MetadataValue::from_static("true"), | ||
); | ||
metadata.insert("x-greptime-hint-ttl", MetadataValue::from_static("3600d")); | ||
metadata.insert( | ||
"x-greptime-hint-append_mode", | ||
MetadataValue::from_static("true"), | ||
); | ||
metadata.insert( | ||
"x-greptime-hint-merge_mode", | ||
MetadataValue::from_static("false"), | ||
); | ||
metadata.insert( | ||
"x-greptime-hint-physical_table", | ||
MetadataValue::from_static("table1"), | ||
); | ||
|
||
let hints = extract_hints(&metadata); | ||
|
||
assert_eq!(hints.len(), 5); | ||
assert_eq!( | ||
hints[0], | ||
("auto_create_table".to_string(), "true".to_string()) | ||
); | ||
assert_eq!(hints[1], ("ttl".to_string(), "3600d".to_string())); | ||
assert_eq!(hints[2], ("append_mode".to_string(), "true".to_string())); | ||
assert_eq!(hints[3], ("merge_mode".to_string(), "false".to_string())); | ||
assert_eq!( | ||
hints[4], | ||
("physical_table".to_string(), "table1".to_string()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_extract_hints_with_partial_metadata_map() { | ||
let mut metadata = MetadataMap::new(); | ||
metadata.insert( | ||
"x-greptime-hint-auto_create_table", | ||
MetadataValue::from_static("true"), | ||
); | ||
metadata.insert("x-greptime-hint-ttl", MetadataValue::from_static("3600d")); | ||
|
||
let hints = extract_hints(&metadata); | ||
|
||
assert_eq!(hints.len(), 2); | ||
assert_eq!( | ||
hints[0], | ||
("auto_create_table".to_string(), "true".to_string()) | ||
); | ||
assert_eq!(hints[1], ("ttl".to_string(), "3600d".to_string())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use axum::http::Request; | ||
use axum::middleware::Next; | ||
use axum::response::Response; | ||
use session::context::QueryContext; | ||
|
||
use crate::hint_headers; | ||
|
||
pub async fn extract_hints<B>(mut request: Request<B>, next: Next<B>) -> Response { | ||
let hints = hint_headers::extract_hints(request.headers()); | ||
if let Some(query_ctx) = request.extensions_mut().get_mut::<QueryContext>() { | ||
for (key, value) in hints { | ||
query_ctx.set_extension(key, value); | ||
} | ||
} | ||
next.run(request).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters