Your first line of defense for closing a gap between you and other riders is not to let a gap open in the first place. In order to do this you need to anticipate when the surges are going to happen in the pack. You can do that by keeping your eyes up the road (where they should be anyway.) When you see the riders at the front standing up to accelerate you need to start ramping up your speed too. If you wait until that chain reaction of acceleration trickles down through all the riders in front of you, you’ll find that the gap is already open before you even start accelerating.
Anticipate the group’s increase in intensity and start increasing your speed at the same time as the riders ahead of you. This will help reduce the size and frequency of gaps occurring in front of you. Closing gaps as quickly as possible will save valuable energy. The larger that gap gets, the harder it will be to close, and will cost you a lot more effort and energy to bridge it. Additionally, the faster you close the gap, the sooner you will be back in the draft and able to recover. If the gap is just a few bike lengths you can usually just close it with a quick acceleration by increasing your cadence and maybe shifting to a harder gear.
Once that gap gets to a more measurable distance, you will need to judge the distance and the necessary effort/pace you will need to close it, and close it as quickly as you can. It is much better to do an effort at 100% for 1 minute to close down a 10 second gap, than to do an effort at 85% and chase for 5 minutes to get back on to the group. The idea is to close the gap as quickly as possible so that you can get back in the draft sooner and have a longer recovery before the next acceleration of the group. Don’t be scared about blowing up by doing a quick, hard chase back on to the group. You’ll get to rest once you’re back in the draft.