You learn from situations like this so that the next time in a similar situation you will react or handle differently. Experience is your best friend here. There are 2 things I have learned over the years which I can suggest.
1.) Work on your peripheral vision, it's important to have a big picture of whats going on.
2.) When I'm following someone, I tend to keep 1 or 2 fingers resting on the brake lever at all times.
In fact I pretty much keep a finger on the brake lever at all times and when I brake, the word "SQUEEZE" pops up in my head, this was learned by repeated track time.
Glad your ok, and don't beat yourself up.