As mentioned it's by wave.
However, the behavior seems a bit strange on how it stops. For mine, it gets the message to take a town portal after clearing Wave 5, but it takes till about Wave 7 to actually do it after I see multiple lines in DB showing taking town portal. As well, all it does it go to town for 5 seconds, and then return into the rift. It's usually enough of a delay to lose the wave, but still.