dubbo/filter/
context.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use std::time::{SystemTime, UNIX_EPOCH};
19
20use crate::logger::tracing::debug;
21use serde_json::Value;
22
23use crate::{
24    codegen::Request,
25    context::{Context, RpcContext},
26    filter::{TIMEOUT_COUNTDOWN, TIMEOUT_DEFAULT, TRI_TIMEOUT_DEADLINE_IN_NANOS},
27    status::Status,
28};
29
30use super::Filter;
31
32#[derive(Clone)]
33pub struct ContextFilter {}
34
35impl Filter for ContextFilter {
36    fn call(&mut self, req: Request<()>) -> Result<Request<()>, Status> {
37        let headers = &mut req.metadata.into_headers();
38
39        let timeout = headers.get(TIMEOUT_COUNTDOWN);
40
41        let time = SystemTime::now()
42            .duration_since(UNIX_EPOCH)
43            .unwrap()
44            .as_nanos();
45
46        let mut dead_line_in_nanos = 0_u128;
47
48        if let Some(t) = timeout {
49            let timeout: u128 = t.to_str().unwrap().parse().unwrap();
50            if timeout > 0_u128 {
51                dead_line_in_nanos = time + timeout * 1000000;
52            }
53        } else {
54            let timeout: u128 = TIMEOUT_DEFAULT * 1000000;
55            dead_line_in_nanos = time + timeout;
56        }
57
58        debug!(
59            "ContextFilter tri-timeout-deadline-in-nanos : {}",
60            dead_line_in_nanos
61        );
62        if let Some(at) = RpcContext::get_attachments() {
63            let mut attachments = at.lock().unwrap();
64            attachments.insert(
65                String::from(TRI_TIMEOUT_DEADLINE_IN_NANOS),
66                Value::from(dead_line_in_nanos.to_string()),
67            );
68        }
69
70        Ok(req)
71    }
72}