-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
72 lines (58 loc) · 1.08 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"log"
"net"
"syscall"
)
type Server struct {
Conf *Config
}
func (s *Server) ListenAndServe() error {
var (
err error
l net.Listener
)
if l, err = net.ListenTCP("tcp", s.Conf.la); err != nil {
return err
}
return s.Serve(l)
}
func (s *Server) Serve(l net.Listener) error {
defer l.Close()
var (
err error
nc net.Conn
ne net.Error
ok bool
c *conn
)
for {
if nc, err = l.Accept(); err != nil {
if ne, ok = err.(net.Error); ok && ne.Temporary() {
continue
}
return err
}
if c, err = newConn(nc, s); err != nil {
continue
}
go c.serve()
}
}
func (s *Server) IncreaseRlimit() {
var (
err error
lim *syscall.Rlimit
)
// details: http://linux.die.net/man/2/setrlimit
lim = &syscall.Rlimit{
65535,
65535,
}
// details: http://stackoverflow.com/questions/17817204/how-to-set-ulimit-n-from-a-golang-program
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, lim)
if err != nil {
log.Println("Error occrred when increasing rlimit: " + err.Error())
log.Fatal("You may need to run this soft as root.")
}
}