Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:18:54+00:00 2026-06-11T22:18:54+00:00

I have skimmed through the Mochiweb code , but have not found any sign

  • 0

I have skimmed through the Mochiweb code, but have not found any sign of the State variable.

Does something similar to gen_server’s State variable exist in Mochiweb?

I need to store some small amount of state-related server-side (not session-related) data on the server and I do not want to use ETS or Mnesia for that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:18:55+00:00Added an answer on June 11, 2026 at 10:18 pm

    I think you have somewhat a misunderstanding of what gen_server state is.

    First, let me explain briefly how mochiweb works.

    Mochiweb doesn’t produce a gen_server process per client. Instead, it just spawns a new process using proc_lib:spawn/3 and creates a parametrized module, which is, basically, a tuple of the following kind:

    {mochiweb_request, #Port<0.623>, get, "/users", {1, 1}, []}
    

    which is

    {mochiweb_request, Socket, Method, RawPath, HTTPVersion, Headers}
    

    This tuple is used as an argument to a function that you pass as a loop parameter to mochiweb_http:start/1. So, when this “loop” function is called, it will look like this:

    handle_request(Req) ->
        %% The pattern matching below just shows what Req really is
        {mochiweb_request, _, _, _, _, _} = Req,
        ...
    

    Now, to explanation of gen_server state.

    Basically, gen_server is a process with approximately the following structure. Of course, IRL it’s more complicated, but this should give you the general idea:

    init(Options)
        State = ...
        loop(Module, State).
    
    loop(Module, State)
        NewState = receive
            {call, Msg, From} -> Module:handle_call(Msg, From, State)
            {cast, Msg} -> Module:handle_cast(Msg, State)
            Info -> Module:handle_info(Info, State)
        end,
        loop(Module, NewState).
    

    So, state is just an argument that you drag through all the function calls and change inside your loop. It doesn’t actually matter if your process is a gen_server or not, it doesn’t have what lifetime it has. In the following example the term [1, 2, 3] is a state too:

    a() ->
        b([1, 2, 3], now()).
    
    b(State, Timestamp) ->
        Result = do_something(Timestamp)
        c(State, Result).
    
    c(State, Payload) ->
        exit({State, Payload}).
    

    Now, back to mochiweb.

    If you need to create a state of your own, you can just add an extra function argument:

    handle_request(Req) ->
        User = Req:get(path),
        UserData = load_user_data(User),
        handle_request(Req, UserData).
    
    handle_request(Req, UserData) ->
        ...
    

    Now UserData is a state too. You can loop this process, or let it respond and end right away – but you won’t lose UserData as long as you pass it as an argument.

    Finally, if you really want to make this process a gen_server (which is really unreasonable in most cases), you can use gen_server:enter_loop/3 function that will make your current process a gen_server. And The 3rd argument of this function will be your state that will be stored inside the started gen_server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Skimmed through the Eclipse API doc and found no mentioning of this event. Could
Have the following code. The height is variable so the height of the floating
Have done quite a bit of searching for a guide (of any substance) for
I'm new to Maven and have skimmed over the documentation as I am following
Not sure why the plugin is behaving this way but whenever I refresh the
I have skimmed the online documentation, read the wiki entry, the posts and the
I've skimmed thru Date and Silberschatz but can't seem to find answers to these
I've been looking around and have probably just not been able to put the
I am not sure why the validation state doesn't get reflected in my user
I have slimmed my problem down to the following code: #include <iostream> using namespace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.