MCPcopy Create free account
hub / github.com/kataras/iris / getTopicConsumeSSEHandler

Function getTopicConsumeSSEHandler

_examples/kafka-api/main.go:304–391  ·  view source on GitHub ↗
(ctx iris.Context)

Source from the content-addressed store, hash-verified

302}
303
304func 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

Callers

nothing calls this directly

Calls 15

ResponseWriterMethod · 0.80
StopWithTextMethod · 0.80
ParamsMethod · 0.80
URLParamInt32DefaultMethod · 0.80
URLParamInt64DefaultMethod · 0.80
ApplicationMethod · 0.80
OnCloseMethod · 0.80
ErrorfMethod · 0.80
RemoteAddrMethod · 0.80
WritefMethod · 0.80
failFunction · 0.70
FlusherMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…