# How to pass variables to hyper service handler?

**URL:** <https://users.rust-lang.org/t/how-to-pass-variables-to-hyper-service-handler/21326>\
**Category:** help\
**Created:** [October 16, 2018, 10:35am UTC](https://users.rust-lang.org/t/how-to-pass-variables-to-hyper-service-handler/21326 "2018-10-16T10:35:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cong-lian](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@cong-lian](https://users.rust-lang.org/u/cong-lian)\
**Post date:** [October 16, 2018, 10:35am UTC](https://users.rust-lang.org/t/how-to-pass-variables-to-hyper-service-handler/21326/1 "2018-10-16T10:35:36Z")

</div>

Hi, I am new to Rust, currently playing around with Hyper, try to build a server which can count the number of incoming requests.

```
// Build a hyper server, which serves our custom echo service.
let request_counter = Arc::new(AtomicUsize::new(0));
let fut =
    Server::builder(tls).serve(move || service_fn(|req| echo(req, request_counter.clone())));

// Run the future, keep going until an error occurs.
println!("Starting to serve on https://{}.", addr);
let mut rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(fut).map_err(|e| error(format!("{}", e)))?;
Ok(())

```

And now stuck in the static time issue. After several days of searching, still got nothing that is straight forward, even though I feel like somehow it could be done in an easy way.  
I got the following errors

```
   Compiling tls_server v0.1.0 (file:///home/lc/playground/tls_server)
error[E0597]: `*request_counter` does not live long enough
  --> tls_server/src/main.rs:79:71
   |
79 | Server::builder(tls).serve(move || service_fn(|req| echo(req, request_counter.clone())));
   | ----- ^^^^^^^^^^^^^^^ borrowed value does not live long enough
   | |
   | capture occurs here
   |
   = note: borrowed value must be valid for the static lifetime...
note: ...but borrowed value is only valid for the lifetime as defined on the body at 79:36
  --> tls_server/src/main.rs:79:36
   |
79 | Server::builder(tls).serve(move || service_fn(|req| echo(req, request_counter.clone())));
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
error: Could not compile `tls_server`.

To learn more, run the command again with --verbose.

```

and full code is the following:  
it was adopted from the server example of hyper-rustls.  
[https://github.com/cong-lian/playground/blob/master/tls\_server/src/main.rs](https://github.com/cong-lian/playground/blob/master/tls_server/src/main.rs)

any hint? I think my case is fairly simple but I just can't make it work. Can someone explain how in general could I pass mutable variables to Hyper service handler?

---

<div class="post-metadata">

**Author:** ![seunlanlege](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@seunlanlege](https://users.rust-lang.org/u/seunlanlege)\
**Post date:** [October 16, 2018, 10:49am UTC](https://users.rust-lang.org/t/how-to-pass-variables-to-hyper-service-handler/21326/2 "2018-10-16T10:49:19Z")

</div>

There's no need to clone, remove the call `.clone()`.

---

<div class="post-metadata">

**Author:** ![vitalyd](https://avatars.discourse-cdn.com/v4/letter/v/edb3f5/32.png) [@vitalyd](https://users.rust-lang.org/u/vitalyd)\
**Post date:** [October 16, 2018, 11:27am UTC](https://users.rust-lang.org/t/how-to-pass-variables-to-hyper-service-handler/21326/3 "2018-10-16T11:27:51Z")

</div>

You’re close:

```rust
let fut =
    Server::builder(tls)
     .serve(move || {
          let inner = Arc::clone(&request_counter);
          service_fn(move |req| echo(req, Arc::clone(&inner)))
      }
);
```

---

<div class="post-metadata">

**Author:** ![cong-lian](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@cong-lian](https://users.rust-lang.org/u/cong-lian)\
**Post date:** [October 16, 2018, 11:51am UTC](https://users.rust-lang.org/t/how-to-pass-variables-to-hyper-service-handler/21326/4 "2018-10-16T11:51:05Z")

</div>

Thank you so much! Provided solution is straightforward enough and most importantly It works!  
But could you please also give some explanation, just for better understanding.  
Why should we need to clone twice? because it smells like redundant overhead.

---

<div class="post-metadata">

**Author:** ![vitalyd](https://avatars.discourse-cdn.com/v4/letter/v/edb3f5/32.png) [@vitalyd](https://users.rust-lang.org/u/vitalyd)\
**Post date:** [October 16, 2018, 12:22pm UTC](https://users.rust-lang.org/t/how-to-pass-variables-to-hyper-service-handler/21326/5 "2018-10-16T12:22:09Z")

</div>

The `clone` inside the `service_fn` call is because `echo()` takes an `Arc<AtomicUsize>` by value, and thus consumes that argument on each call. The closure given to `service_fn` must be an `Fn` type, which means it must be callable multiple times. In order to keep it callable multiple times, we need to ensure that `inner` isn't consumed across the `echo()` calls - that's what the second clone achieves.

And in fact, your intuition about seemingly needless clones is right. `echo()` can take a `&AtomicUsize` parameter, rather than an `Arc<AtomicUsize>`. In that case, you can avoid the second clone:

```rust
let fut =
    Server::builder(tls)
     .serve(move || {
          let inner = Arc::clone(&request_counter);
          service_fn(move |req| echo(req, &inner))
      }
);

```

---

<div class="post-metadata">

**Author:** ![cong-lian](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@cong-lian](https://users.rust-lang.org/u/cong-lian)\
**Post date:** [October 16, 2018, 12:49pm UTC](https://users.rust-lang.org/t/how-to-pass-variables-to-hyper-service-handler/21326/6 "2018-10-16T12:49:04Z")

</div>

Perfect! Everything is clear now.  
Thank you!

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:50am UTC](https://users.rust-lang.org/t/how-to-pass-variables-to-hyper-service-handler/21326/7 "2023-01-12T08:50:13Z")

</div>

Closed on January 12, 2023, 8:50am UTC


