(w http.ResponseWriter, r *http.Request)
| 55 | } |
| 56 | |
| 57 | func (h *timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 58 | var t *time.Timer |
| 59 | timeout := h.testTimeout |
| 60 | if timeout == nil { |
| 61 | t = time.NewTimer(h.dt) |
| 62 | timeout = t.C |
| 63 | } |
| 64 | done := make(chan struct{}) |
| 65 | tw := &timeoutWriter{ |
| 66 | w: w, |
| 67 | h: make(http.Header), |
| 68 | } |
| 69 | go func() { |
| 70 | h.handler.ServeHTTP(tw, r) |
| 71 | close(done) |
| 72 | }() |
| 73 | select { |
| 74 | case <-done: |
| 75 | tw.mu.Lock() |
| 76 | defer tw.mu.Unlock() |
| 77 | dst := w.Header() |
| 78 | for k, vv := range tw.h { |
| 79 | dst[k] = vv |
| 80 | } |
| 81 | if !tw.wroteHeader { |
| 82 | tw.code = http.StatusOK |
| 83 | } |
| 84 | w.WriteHeader(tw.code) |
| 85 | w.Write(tw.wbuf.Bytes()) |
| 86 | if t != nil { |
| 87 | t.Stop() |
| 88 | } |
| 89 | case <-timeout: |
| 90 | tw.mu.Lock() |
| 91 | defer tw.mu.Unlock() |
| 92 | w.WriteHeader(http.StatusGatewayTimeout) |
| 93 | io.WriteString(w, h.errorBody()) |
| 94 | tw.timedOut = true |
| 95 | return |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | type timeoutWriter struct { |
| 100 | w http.ResponseWriter |
nothing calls this directly
no test coverage detected