-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE REQUEST] make reuseport/reuseaddress as Listener option. #1544
Comments
Hello @Dexus, For Unix-based systems there is an example already at: // +build linux darwin dragonfly freebsd netbsd openbsd rumprun
package main
import (
"github.com/valyala/tcplisten"
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<b>Hello World!</b>")
})
listenerCfg := tcplisten.Config{
ReusePort: true,
DeferAccept: true,
FastOpen: true,
}
l, err := listenerCfg.NewListener("tcp4", ":8080")
if err != nil {
panic(err)
}
app.Run(iris.Listener(l))
} I didn't know about the package main
import (
"context"
"net"
"syscall"
"time"
"github.com/kataras/iris/v12"
"golang.org/x/sys/windows"
)
func main() {
app := iris.New()
startup := time.Now()
app.Get("/", func(ctx iris.Context) {
s := startup.Format(ctx.Application().ConfigurationReadOnly().GetTimeFormat())
ctx.Writef("this server started at: %s\n", s)
})
app.Get("/path", func(ctx iris.Context) {
ctx.Writef("path: %s\n", ctx.Path())
})
ln, err := ReusePort("tcp4", ":8080")
if err != nil {
panic(err)
}
app.Run(iris.Listener(ln))
}
func control(network, address string, c syscall.RawConn) (err error) {
return c.Control(func(fd uintptr) {
err = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_REUSEADDR, 1)
})
}
func ReusePort(network, addr string) (net.Listener, error) {
cfg := net.ListenConfig{
Control: control,
}
return cfg.Listen(context.Background(), network, addr)
} The windows behavior is: you can start as many servers as you want pointing to the same port however, all requests communicate with the first server started only, if the first server is terminated, then the requests will be fired to the second server one (try to change the http responses to test it) and so on... Refs: |
I know the example, but it would be nicer if you just used the module, because it runs on all systems, and if you are familiar with the ReUse theme, you know the pitfalls that arise. But it would make it easier if you had the box ready to use. Without the detour to do it yourself. But it's up to you whether you consider it integrable. |
And nobody should expect it to be a "loadbalancer" if you use the same port multiple times... |
OK, I agree. I must go to a meeting now. Will take a closer look in order to add a feature like this for all supported platforms easy. |
Hello @Dexus, there is an
I've also added comments for "feature details" on the setting itself, such as:
|
Thank you very much, that will help us to kick out old server without lost requests now. |
Former-commit-id: 0384baf593012377a94344d647ca41121294285a
Is your feature request related to a problem? Please describe.
I would like to make my app always up to date but without any losing requests. So i would like to have the option with reuseport/reuseaddress to use the already used Port and address form the currently running instance. So that I can start the updated Server and kill after it the old server.
Describe the solution you'd like
https://github.com/libp2p/go-reuseport
Describe alternatives you've considered
Own Listener but why I should make it my own? Ngnix and other Server also have this option included.
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: