Stochastic Averaging for Constrained Optimization with Application to Online Resource Allocation