DEV Community

Cover image for Welcome to Fiber — an Express.js styled web framework written in Go with ️

Welcome to Fiber — an Express.js styled web framework written in Go with ️

Vic Shóstak on February 03, 2020

Introduction Hello, World! 👋 Today we will deal with a young (but ambitious) Fiber web framework on Go and understand that this is not "...
Collapse
 
inerkyjad profile image
Hassan Aoutof

@koddr , i do very much like the idea of the framework and i would love to rewrite a slow Fastify project in it, ofc i'll first have to verify the claims being made and play with it myself and maybe even become a member of the community, but i'm getting skeptical because i see @kataras comments were hidden by you, did he have something bad to say about the framework ?? in general hiding comment's is bad and it doesn't inspire confidence nor trust. why was his comment hidden ?!

Collapse
 
koddr profile image
Vic Shóstak

Nothing wrong there, he just fixed an old bug, which he posted in the comments. That's all.

Exposed his comments, since it's so shocking to some of my readers, no problem.

Collapse
 
inerkyjad profile image
Hassan Aoutof

Thank you, i gave the project a GitHub star already because it doesn't matter if it has some bugs, that's fixable the important thing is the idea, and i'll start trying out soon.

Collapse
 
kataras profile image
Gerasimos (Makis) Maropoulos • Edited

Based on github.com/gofiber/fiber/blob/mast... I should be able to register a route of: app.Get("/api/values/:id", func(ctx *fiber.Ctx) but it can't even do that.... it panics

Anyway, I fixed it with PR: github.com/gofiber/fiber/pull/28

Screenshot with my PR

Good luck to this initiative! Please inform your users that this framework does not support HTTP/2.

Thanks,
Gerasimos Maropoulos. Author of the Iris web framework.

Collapse
 
koddr profile image
Vic Shóstak • Edited

Yes, framework is too young and have some trouble (its written by one man as side project... not me, but I active help to promote).

Thx for PR :D

P.S. btw, I'm really looking forward to the Iris CLI release ;)

Collapse
 
kataras profile image
Gerasimos (Makis) Maropoulos • Edited

Vic you are koddr! Oh my god good job mate I hope my projects gave you an idea about how the whole thing works, good luck and anytime you need my help you have it!

P.S I am working on it as we speak, I found the bug when I was iris-cli benchmark of fiber to test its performance :)

Thread Thread
 
koddr profile image
Vic Shóstak

Yes, my friend, it's me 🥰

Thx for this words, it's extremely important for me (really, thank you).

P.S. actually, your first commit of Iris CLI (yep, I seen this moment) gave me an idea for Create Go App. It's something like CRA (create-react-app), but for every Go web frameworks... ONE CLI TO RULE THEM ALL! haha 🤗

P.P.S. I'm not sure, what gophers have to say, but I personally need similar all-in-one solution (both hyped frontend/backend frameworks and configured Docker containers by one CLI command)... and why not to write it by myself 😉

Collapse
 
davidmz profile image
Давид Мзареулян • Edited

Why do we need prefork in Go? Isn't regular gorutines enough?

Collapse
 
kataras profile image
Info Comment hidden by post author - thread only accessible via permalink
Gerasimos (Makis) Maropoulos • Edited

In short, prefork is done through Unix' SO_REUSEADDR and it does not work on Windows hosts. You can implement preforking on any web framework, including the standard net/http. A good library (that iris uses too) is tcplisten by @valyala the creator of fasthttp and a good friend. Example code:

 listenerCfg := tcplisten.Config{
    ReusePort:   true,
    DeferAccept: true,
    FastOpen:    true,
}



for i:=0;i<runtime.NumCPU();i++ {

    /* 1. inline servers, share the same binary.
    ln, err := listenerCfg.NewListener("tcp4", ":8080")
    if err != nil {
       panic(err)
    }
    go http.Serve(ln, yourMux_Router)
    */

    /*2. Start the same binary with an argument of `--prefork`
         or anything that will mark the server as a fork. 

        https://github.com/gofiber/fiber/blob/master/application.go#L46
        https://github.com/gofiber/fiber/blob/master/application.go#L378
        and finally:
        https://github.com/gofiber/fiber/blob/master/application.go#L469
    */


}

http.ListenAndServe(":8080", yourMux_Router) // blocks. 

Also, see the discussion at the fasthttp repository itself:

I noticed that you recently removed the prefork method from your TechEmpower
benchmark code. Do you no longer believe the prefork method to offer better performance? by @manthedan at github.com/valyala/fasthttp/issues...

Collapse
 
fenny profile image
Fenny

Preforking is making use of multiple socket listeners on OS level. We explained preforking with images in our docs fiber.wiki/application#prefork

Collapse
 
davidmz profile image
Давид Мзареулян

Does prefork mean that workers are executed in different processes (not goroutines) and cannot communicate or share memory with each other?

Thread Thread
 
fenny profile image
Fenny • Edited

This is correct, preforking is for specific use cases. If you work with an external database it should be no problem, because you can pool connections. You can see a good result here: techempower.com/benchmarks/#sectio...

Collapse
 
ben_schoeffmann profile image
Benedikt Schöffmann

Hey Vic,

thanks for this article! I wanted to dive into Go for a long time, this is the kick in the butt I needed :)

Cheers,
Ben

P.S.: My first ever Dev.to posting.

Collapse
 
koddr profile image
Vic Shóstak

This is very cool! Glad I could move you.

Collapse
 
orenmizr profile image
Oren Mizrahi

How is it compared to expressJS - connections benchmarks-wise ? (i'm sure it is better, but how much better)

Collapse
 
fenny profile image
Fenny • Edited

Plaintext responses per second
Fiber - 6,114,300/s (0 errors) 0.5ms avg latency
Express - 261,708/s (59 errors) 608.9ms avg latency
techempower.com/benchmarks/#sectio...

JSON responses per second
Fiber - 1,212,833/s (0 errors) 0.1ms avg latency
Express - 244,061/s (0 errors) 1.1ms avg latency
techempower.com/benchmarks/#sectio...

20 database queries per request
Fiber - 19,757/s (0 errors) 25.6ms avg latency
Express - 4,259/s (0 errors) 118.4ms avg latency
techempower.com/benchmarks/#sectio...

Keep in mind that these benchmarks are 1 month old and fiber is being updated on a daily basis. Fiber v1.8 will have better performance.

I will update this comment when techempower tested v1.8.

Collapse
 
josephmbassey profile image
Joseph Michael

This is awesome, coming from a nodejs/expressjs background, will make it easy to understand and use Go. 👍

Some comments have been hidden by the post's author - find out more