func (t *throttler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	select {
	case <-ctx.Done():
		http.Error(w, errContextCanceled, http.StatusServiceUnavailable)
		return
	case btok := <-t.backlogTokens:
		timer := time.NewTimer(t.backlogTimeout)

		defer func() {
			t.backlogTokens <- btok
		}()

		select {
		case <-timer.C:
			http.Error(w, errTimedOut, http.StatusServiceUnavailable)
			return
		case <-ctx.Done():
			http.Error(w, errContextCanceled, http.StatusServiceUnavailable)
			return
		case tok := <-t.tokens:
			defer func() {
				t.tokens <- tok
			}()
			t.h.ServeHTTP(w, r)
		}
		return
	default:
		http.Error(w, errCapacityExceeded, http.StatusServiceUnavailable)
		return
	}
}
func (t *throttler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	select {
	case <-ctx.Done():
		http.Error(w, errContextCanceled, http.StatusServiceUnavailable)
		return
	case btok := <-t.backlogTokens:
		timer := time.NewTimer(t.backlogTimeout)

		defer func() {
			t.backlogTokens <- btok
		}()

		select {
		case <-timer.C:
			http.Error(w, errTimedOut, http.StatusServiceUnavailable)
			return
		case <-ctx.Done():
			http.Error(w, errContextCanceled, http.StatusServiceUnavailable)
			return
		case tok := <-t.tokens:
			defer func() {
				t.tokens <- tok
			}()
			t.h.ServeHTTP(w, r)
		}
		return
	default:
		http.Error(w, errCapacityExceeded, http.StatusServiceUnavailable)
		return
	}
}
func ( t * throttler ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) { ctx := r . Context ( ) select { case <- ctx . Done ( ) : http . Error ( w , errContextCanceled , http . StatusServiceUnavailable ) return case btok := <- t . backlogTokens : timer := time . NewTimer ( t . backlogTimeout ) defer func ( ) { t . backlogTokens <- btok } ( ) select { case <- timer . C : http . Error ( w , errTimedOut , http . StatusServiceUnavailable ) return case <- ctx . Done ( ) : http . Error ( w , errContextCanceled , http . StatusServiceUnavailable ) return case tok := <- t . tokens : defer func ( ) { t . tokens <- tok } ( ) t . h . ServeHTTP ( w , r ) } return default : http . Error ( w , errCapacityExceeded , http . StatusServiceUnavailable ) return } }
// ServeHTTP is the primary throttler request handler
ServeHTTP is the primary throttler request handler