...

Source file src/go.etcd.io/etcd/client/pkg/v3/testutil/pauseable_handler.go

Documentation: go.etcd.io/etcd/client/pkg/v3/testutil

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutil
    16  
    17  import (
    18  	"net/http"
    19  	"sync"
    20  )
    21  
    22  type PauseableHandler struct {
    23  	Next   http.Handler
    24  	mu     sync.Mutex
    25  	paused bool
    26  }
    27  
    28  func (ph *PauseableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    29  	ph.mu.Lock()
    30  	paused := ph.paused
    31  	ph.mu.Unlock()
    32  	if !paused {
    33  		ph.Next.ServeHTTP(w, r)
    34  	} else {
    35  		hj, ok := w.(http.Hijacker)
    36  		if !ok {
    37  			panic("webserver doesn't support hijacking")
    38  		}
    39  		conn, _, err := hj.Hijack()
    40  		if err != nil {
    41  			panic(err.Error())
    42  		}
    43  		conn.Close()
    44  	}
    45  }
    46  
    47  func (ph *PauseableHandler) Pause() {
    48  	ph.mu.Lock()
    49  	defer ph.mu.Unlock()
    50  	ph.paused = true
    51  }
    52  
    53  func (ph *PauseableHandler) Resume() {
    54  	ph.mu.Lock()
    55  	defer ph.mu.Unlock()
    56  	ph.paused = false
    57  }
    58  

View as plain text