GithubHelp home page GithubHelp logo

Caching auth between users about goose HOT 5 CLOSED

buck06191 avatar buck06191 commented on August 27, 2024
Caching auth between users

from goose.

Comments (5)

jeremyandrews avatar jeremyandrews commented on August 27, 2024

To only log each user in once, you'd use set_on_start() and the GooseUser will log in and then make all subsequent requests as an authenticated user:
https://docs.rs/goose/latest/goose/goose/struct.Transaction.html#method.set_on_start

To truly only log in once, you could instead call test_start():
https://docs.rs/goose/latest/goose/struct.GooseAttack.html#method.test_start

However, you'd then be responsible for saving whatever session information is needed, most likely requiring that you build each user setting the appropriate header or cookie etc:
https://docs.rs/goose/latest/goose/goose/struct.GooseUser.html#method.set_client_builder

from goose.

buck06191 avatar buck06191 commented on August 27, 2024

That's really helpful. What's the idiomatic way to pass session information from that global level down into a GooseUser? The user created for the test_start method wouldn't have its information saved and I can't see a way to get information out of that thread and back up to the main function to be passed down to future users?

from goose.

jeremyandrews avatar jeremyandrews commented on August 27, 2024

When I did something similar for a client some time ago, I used OnceCell. I essentially populated it during test_start() which runs before the GooseUser's are created for the load test, and then used it in set_on_start() to simulate per GooseUser login.

from goose.

buck06191 avatar buck06191 commented on August 27, 2024

Grand. Thank you. I ended up leaning into std::sync::OnceLock. I'll share a very basic snippet of it here for anyone else that comes across this

// globals.rs 
pub static SESSION: OnceLock<Box<Session>> = OnceLock::new();

pub fn build_global_session(
    auth_token: SecretString,
    api_key: SecretString,
) {
    let session = Session {
        auth_token,
        api_key,
    };
    let boxed_session = Box::new(session);

    SESSION.get_or_init(|| boxed_session);
}

// authorisation.rs
pub async fn setup_custom_client(user: &mut GooseUser) -> TransactionResult {
    let session = SESSION.get().expect("Couldn't get the global session data");
    user.set_session_data(session);

    Ok(())
}

Thanks for your help!

from goose.

jeremyandrews avatar jeremyandrews commented on August 27, 2024

Great! If you'd like to make a quick PR, it would be great to add another example that demonstrates this:
https://github.com/tag1consulting/goose/tree/main/examples

from goose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.