RoundTrip implements the http.RoundTripper interface
(req *http.Request)
| 31 | |
| 32 | // RoundTrip implements the http.RoundTripper interface |
| 33 | func (c GRPCCollector[T]) RoundTrip(req *http.Request) error { |
| 34 | ctx := req.Context() |
| 35 | ctx, cancel := context.WithCancel(ctx) // create a new context with a cancel function |
| 36 | defer cancel() |
| 37 | |
| 38 | ctx, span := tracer.Start(ctx, "GRPCCollector.RoundTrip") |
| 39 | defer span.End() |
| 40 | |
| 41 | req = req.WithContext(ctx) |
| 42 | resps, err := c.next.RoundTrip(NewHTTPRequest(req)) |
| 43 | if err != nil { |
| 44 | return grpcError(err) |
| 45 | } |
| 46 | span.AddEvent("next.RoundTrip done") |
| 47 | |
| 48 | lastUpdate := time.Now() |
| 49 | // sendDiffCb should return an error if the context is cancelled, |
| 50 | // callback's error is used to exit early from the loop and return the error to the caller |
| 51 | sendDiffCb := func() error { |
| 52 | // check if we should send an update |
| 53 | if time.Since(lastUpdate) > 500*time.Millisecond { |
| 54 | lastUpdate = time.Now() |
| 55 | // check and return the context errors, like ctx cancelled, etc |
| 56 | if req.Context().Err() != nil { |
| 57 | return req.Context().Err() |
| 58 | } |
| 59 | |
| 60 | // send a diff only during streaming |
| 61 | resp, err := c.combiner.GRPCDiff() |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | return c.sendSegmented(req, resp, c.maxSegmentSize) |
| 67 | } |
| 68 | |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | err = consumeAndCombineResponses(ctx, c.consumers, resps, c.combiner, sendDiffCb) |
| 73 | if err != nil { |
| 74 | return grpcError(err) |
| 75 | } |
| 76 | span.AddEvent("consumeAndCombineResponses done") |
| 77 | |
| 78 | // send the final diff if there is anything left |
| 79 | resp, err := c.combiner.GRPCFinal() |
| 80 | if err != nil { |
| 81 | return grpcError(err) |
| 82 | } |
| 83 | span.AddEvent("combiner.GRPCFinal() done") |
| 84 | |
| 85 | return c.sendSegmented(req, resp, c.maxSegmentSize) |
| 86 | } |
| 87 | |
| 88 | func (c GRPCCollector[T]) sendSegmented(req *http.Request, resp T, maxSize int) error { |
| 89 | // If no max, then send as-is. |
nothing calls this directly
no test coverage detected