summaryrefslogtreecommitdiff
path: root/app/listener.go
diff options
context:
space:
mode:
authorJames Mills <prologic@shortcircuit.net.au>2020-03-21 09:55:06 +1000
committerJames Mills <prologic@shortcircuit.net.au>2020-03-21 09:55:06 +1000
commit4dae45a68ff1ff32a251a5835b2a2723ae125d1c (patch)
treeb3abe641f39f50ac444aee2bb617d7b4ab44187d /app/listener.go
parent76fa613b7eaf1c8626d2a51901c623af7bf1a665 (diff)
Restructured source code layout, Added rice support for templates and static assets
Diffstat (limited to 'app/listener.go')
-rw-r--r--app/listener.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/app/listener.go b/app/listener.go
new file mode 100644
index 0000000..a4236d8
--- /dev/null
+++ b/app/listener.go
@@ -0,0 +1,37 @@
+// Instead of using the default new.Listener this file will construct a custom
+// one. The main purpose for this is to have more control over the settings
+// (like keep-alive) and to retrieve the assigned port when using port 0.
+
+package app
+
+import (
+ "fmt"
+ "net"
+ "time"
+)
+
+func newListener(cfg *ServerConfig) (net.Listener, error) {
+ addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
+ ln, err := net.Listen("tcp", addr)
+ if err != nil {
+ return nil, err
+ }
+ // set actual port on config object (in case original port was 0)
+ cfg.Port = ln.Addr().(*net.TCPAddr).Port
+ return tcpListener{ln.(*net.TCPListener)}, nil
+}
+
+// custom TCP listener with keep-alive timeout
+type tcpListener struct {
+ *net.TCPListener
+}
+
+func (ln tcpListener) Accept() (net.Conn, error) {
+ tc, err := ln.AcceptTCP()
+ if err != nil {
+ return nil, err
+ }
+ tc.SetKeepAlive(true)
+ tc.SetKeepAlivePeriod(3 * time.Minute)
+ return tc, nil
+}