-
-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add reuse port (SO_REUSEPORT) example
- Loading branch information
1 parent
d553c0c
commit 8c777b6
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
var listenConfig = net.ListenConfig{ | ||
Control: func(network, address string, c syscall.RawConn) error { | ||
var opErr error | ||
if err := c.Control(func(fd uintptr) { | ||
opErr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1) | ||
}); err != nil { | ||
return err | ||
} | ||
return opErr | ||
}, | ||
} | ||
|
||
func main() { | ||
pid := os.Getpid() | ||
listener, err := listenConfig.Listen(context.Background(), "tcp", "127.0.0.1:8080") | ||
if err != nil { | ||
panic(err) | ||
} | ||
server := &http.Server{} | ||
http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { | ||
rw.WriteHeader(http.StatusOK) | ||
fmt.Fprintf(rw, "Hello from PID %d \n", pid) | ||
fmt.Printf("serving to %v (%v) with PID %d \n", req.RemoteAddr, req.Header.Get("X-Forwarded-For"), pid) | ||
}) | ||
fmt.Printf("HTTP Server with PID %d is running \n", pid) | ||
|
||
panic(server.Serve(listener)) | ||
} |