Inconsistent routing in Go fasthttp with fasthttp/router

162 Views Asked by At

I am working on a Go server that uses FastHTTP with github.com/fasthttp/router routing package. But I have been experiencing routing inconsistencies with it. I tried every fork of the fasthttp/router package and I am having problems with every single one. Here is the problem:

I have my main.go file here:

package main

import ...

func main() {
  // ...
  go routes.RegisterHealthRoutes(router)
  go routes.RegisterUserRoutes(router)
  go routes.RegisterPostRoutes(router)

  log.Fatal(fasthttp.ListenAndServe(addr, middleware.CORS(router.Handler)))
}

Now, in my routes package (I am only showing one of them but the other implementations are the same), I am registering controllers on specific endpoints based on the function.

Here is one of them (func RegisterPostRoutes())
routes/posts.go

package routes

import (
    post_controllers "tea-share/controllers/posts"

    "github.com/fasthttp/router"
)

func RegisterPostRoutes(router *router.Router) {
  go router.GET("/posts", post_controllers.GetPosts)
  go router.GET("/posts/search", post_controllers.GetPostsBySearchTerm)
  go router.POST("/posts", post_controllers.CreatePost)
  go router.PATCH("/posts/{id}/like", post_controllers.LikePost)
}

Here is my go build command:

$ go build -tags netgo -ldflags "-extldflags '-static' -s -w" -o bin/tea-share

This registers the routes successfully, sometimes...
Many times, one route registers, while many don't. In my testing environment, I ran the server, it registered all of the /posts, but when it came to /users, it missed two routes, (/users/login and /users/signup)

Then when I closed the server and started it again, it registered all users routes, but it failed to register /posts/{id}/like and POST /posts. I closed and ran it again, then it registered both /users and /posts correctly but failed to register /health (My health check route so my hosting provider thought my server had failed health check)

And remember, when I mean "failed to register", it can mean many things. What I am experiencing is either:

  • The endpoint returns 404 (Not Found)
  • The endpoint returns 405 (Method Not Allowed)

Another error that may seem weird but it does happen many times, that when I start the app, it panics and gives me this error:

fatal error: concurrent map writes

goroutine 39 [running]:
github.com/fasthttp/router.(*Router).Handle(0xc000126b60, {0xddd865, 0x3}, {0xde559d, 0xd}, 0xe35cf8)
        /home/dev-siri/Desktop/Projects/Go/pkg/mod/github.com/fasthttp/[email protected]/router.go:231 +0xa5
github.com/fasthttp/router.(*Router).GET(0x0?, {0xde559d?, 0xbd5f46?}, 0xc000126b60?)
        /home/dev-siri/Desktop/Projects/Go/pkg/mod/github.com/fasthttp/[email protected]/router.go:117 +0x33
created by tea-share/routes.RegisterUserRoutes
        /home/dev-siri/Desktop/Projects/JavaScript+Dart/Tea-Share/apps/server/routes/users.go:11 +0xad

Although, when I do nothing and start the app again, it starts up successfully, and then instead of the app crashing, it just has route registration errors like I mentioned above.

What is going on with fasthttp/router? Removing the go keyword before the router.[METHOD] calls also do nothing, things for some reason just break harder sometimes. I am unable to ship the app because of this unreliable routing mechanism. Is it something to do with the router or my code? Can someone please help?

0

There are 0 best solutions below