I am trying to set up a global state for my actix_web::HttpServer
, and it seems like register_data
is the proper API (I could be wrong).
From the documentation, it is not clear to me how to create a single instance of application data shared by all HttpServer
threads. Here is my code piece:
HttpServer::new(|| {
App::new()
.register_data(web::Data::new(Mutex::new(MyServer::new())))
.service(web::resource("/myservice").route(web::post().to(my_service)))
.service(web::resource("/list").to(list_service))
})
In the POST handler my_service
, I update the state of MyServer
and in the GET handler list_service
, it will print out the state.
While my_service
is successful in storing the state, the list_service
only prints empty output. How do I know if HttpServer
created a single instance of MyServer
or not? If not, how can I ensure it creates a single instance? The documentation of register_data
states:
Application data does not need to be Send
or Sync
. Internally Data
type uses Arc
. if your data implements Send
+ Sync
traits you can use web::Data::new()
and avoid double Arc
.
I'm confused by that. What should the user do so that register_data
will register a single instance of the data object? Does this following line in the example from the documentation create a single instance, or one instance per thread?
.register_data(data.clone())
I'm using actix-web 1.0.9.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…