Some nasty stories here. I'm glad all those who have experienced dog attacks are on the road to recovery.
I'm lucky because I grew up with dogs, and I can read their moods pretty well. I can also produce a very deep-voiced "no" when a dog is coming towards me that generally stops them. (When I was in my teens/20s, my mother had 2 dogs who tended to fight a lot, and she got bitten once trying to separate them. My grandfather could break up a fight with just his voice, and I'm still totally in awe of that, and try to emulate it.) The very deep, low voice emulates the low growl that a dominant dog uses to warn another off, and it will work with most dogs.
If you come across a dog that's the dominant male in its own right, and isn't going to respond to that, it's better to avoid eye contact (eye contact is seen as a challenge), get off the bike, and walk slowly away, keeping the bike between you and the dog. This is like being the submissive dog. The vast majority of these dominant dogs will be protecting what they see as their territory, and once you leave their territory, they lose interest in you because you're no longer seen as a threat.
As for dogs that are actually attacking .... I hope to never be in that situation, but if it's just one dog, I'd like to think I'd have judged the situation well enough to be on the ground with the bike between me and the dog, so I could use the bike to hold it off as I moved away. If a dog actually got hold of my leg, I like to think I'd have enough self control to play dead and wait for it to lose interest.
Apparently, there is a dog scarer that works. Eileen Sutherland talks about it in her book about cycling from Britain to Bulgaria, and she and her husband call it the "dog dazer". They used it more than once on their trip, and every time they said it confused the dogs long enough for them to ride away safely.