(ctx iris.Context)
| 302 | } |
| 303 | |
| 304 | func getTopicConsumeSSEHandler(ctx iris.Context) { |
| 305 | flusher, ok := ctx.ResponseWriter().Flusher() |
| 306 | if !ok { |
| 307 | ctx.StopWithText(iris.StatusHTTPVersionNotSupported, "streaming unsupported") |
| 308 | return |
| 309 | } |
| 310 | |
| 311 | ctx.ContentType("application/json, text/event-stream") |
| 312 | ctx.Header("Cache-Control", "no-cache") |
| 313 | ctx.Header("Connection", "keep-alive") |
| 314 | |
| 315 | master, err := sarama.NewConsumer(brokers, config) |
| 316 | if err != nil { |
| 317 | fail(ctx, iris.StatusInternalServerError, "unable to start master consumer: %v", err) |
| 318 | return |
| 319 | } |
| 320 | |
| 321 | fromTopic := ctx.Params().Get("topic") |
| 322 | // take the partition, defaults to the first found if not url query parameter "partition" passed. |
| 323 | var partition int32 |
| 324 | partitions, err := master.Partitions(fromTopic) |
| 325 | if err != nil { |
| 326 | master.Close() |
| 327 | fail(ctx, iris.StatusInternalServerError, "unable to get partitions for topic: '%s': %v", fromTopic, err) |
| 328 | return |
| 329 | } |
| 330 | |
| 331 | if len(partitions) > 0 { |
| 332 | partition = partitions[0] |
| 333 | } |
| 334 | |
| 335 | partition = ctx.URLParamInt32Default("partition", partition) |
| 336 | offset := ctx.URLParamInt64Default("offset", sarama.OffsetOldest) |
| 337 | |
| 338 | consumer, err := master.ConsumePartition(fromTopic, partition, offset) |
| 339 | if err != nil { |
| 340 | ctx.Application().Logger().Error(err) |
| 341 | master.Close() // close the master here to avoid any leaks, we will exit. |
| 342 | fail(ctx, iris.StatusInternalServerError, "unable to start partition consumer: %v", err) |
| 343 | return |
| 344 | } |
| 345 | |
| 346 | // `OnClose` fires when the request is finally done (all data read and handler exits) or interrupted by the user. |
| 347 | ctx.OnClose(func(_ iris.Context) { |
| 348 | ctx.Application().Logger().Warnf("a client left") |
| 349 | |
| 350 | // Close shuts down the consumer. It must be called after all child |
| 351 | // PartitionConsumers have already been closed. <-- That is what |
| 352 | // godocs says but it doesn't work like this. |
| 353 | // if err = consumer.Close(); err != nil { |
| 354 | // ctx.Application().Logger().Errorf("[%s] unable to close partition consumer: %v", ctx.RemoteAddr(), err) |
| 355 | // } |
| 356 | // so close the master only and omit the first ^ consumer.Close: |
| 357 | if err = master.Close(); err != nil { |
| 358 | ctx.Application().Logger().Errorf("[%s] unable to close master consumer: %v", ctx.RemoteAddr(), err) |
| 359 | } |
| 360 | }) |
| 361 |
nothing calls this directly
no test coverage detected
searching dependent graphs…