there are a variety of studies all showing slightly different things, but pointing to a general state called the "Metabolic window" where protein consumed after a workout is vastly prioritized for muscle growth over energy storage. Some studies show that the gap closes fast and you should try to consume protein before 30 minutes after your workout, while others indicate it lasts up to 24 hours. I believe it is related to exercise boosting your testosterone levels, and that lasts for a while so I usually just try to get a good meal in within a couple hours because I that's about how long my personal testosterone "high" lasts.
As for the length of the workout, that is something you'll have to kind of feel out yourself. See how much exercise it takes to get a mood boost/change, then time how long it lasts so you know your window.
You can also look into it yourself, it isn't an exact science so it might not work for you, hell it might not work at all and it could just be some placebo effect. At the end of the day your main goal should be enjoying your workout, because time and consistency is how you guarantee results, so do what keeps you doing it, and forcing yourself to consume protein at specific times can easily suck the joy out of working out.