Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃 [Question]: How can i keep a connection alive untill timeout or i response some message? #2903

Open
kingstonduy opened this issue Mar 8, 2024 · 1 comment

Comments

@kingstonduy
Copy link

kingstonduy commented Mar 8, 2024

Question Description

Hi, i'm new from golang and fiber. I want to implement a system that: Client sends http request to service A. Service A communicates with service B through kafka. Then service A takes the response from kafka then matches with the request id from client and responses back to client. The normal approach of service A would be:

func main() {
    app := fiber.New()

    // An example to describe the question
    app.Post("/", func(c *fiber.Ct) error {
        var req share.SaferRequest
		if err := c.BodyParser(&req); err != nil {
			return c.Status(400).SendString(err.Error())
		} else {
			ProduceToKafka(req)
			res := ConsumeFromKafka(req.ID) 
			resBytes, _ := json.Marshal(res)
			c.Status(200).Send(resBytes)
		}
    })
    log.Fatal(app.Listen(":3000"))
}

Where we can see that we have to wait for the message to consume from Kafka.

Now my question is: Does fiber support passing the context through other goroutines so that i can use the context corresponding to the request.ID to response to the client. Draft code of my idea:

var m map[string]*fiber.Ctx

func() ConsumeFromKafka() {
    for {
        res := kafka.Consume()
        c := m[res.ID]
        g.Status(200).JSON(res)
    }
}

func main() {
    app := fiber.New()

    // An example to describe the question
    app.Post("/", func(c *fiber.Ct) error {
        var req share.SaferRequest
		if err := c.BodyParser(&req); err != nil {
			return c.Status(400).SendString(err.Error())
		} else {
			m[req.ID] = c
			ProduceToKafka(req)
		}
    })
    log.Fatal(app.Listen(":3000"))

    go ConsumeFromKafka()

     forever chan bool
    <- forever
}

On the about implementation, the connection must be kept alive during the process produce and consume message from kafka. The connection only shutdown when i response a message back to client. My teammate successfully implemented this idea using vertx using eventbus and RoutingContext so i guess there are some mechanics to work around that. Feel free to discuss.
Thank you for reading my question.

Copy link

welcome bot commented Mar 8, 2024

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant