This repository has been archived by the owner on Dec 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (139 loc) · 3.81 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"flag"
"log"
"math/rand"
"net"
"strings"
"time"
"./prefork"
"./process_manager"
"./server"
)
// Flags
var (
host = flag.String(
"host", "127.0.0.1:8080", "The address for the server to bind to.")
app = flag.String(
"app", "", "The WSGI, ASGI or raw app name and path, e.g 'my_server:app'")
adapter = flag.String(
"adapter", "", "Adapter type to use. (ASGI, WSGI, RAW)")
workerCount = flag.Int(
"workers", 1, "The amount of server workers to spawn.")
// External process options
workerCommand = flag.String(
"workercmd",
"python",
"The command to be used to start a external worker. e.g. 'python'")
shardsPerProc = flag.Int(
"shardsperproc", 1, "The amount of shards per process to use.")
processRatio = flag.Int(
"procratio", 1, "The amount of external workers to spawn to 1 Go worker.")
// Fast Http Settings
//name = flag.String(
//"name", "Sandman", "Server name")
//concurrency = flag.Int(
//"concurrency", 0, "Set the concurrency limit.")
//disableKeepAlive = flag.Bool(
// "nokeepalive", false, "Disable keep-alive.")
//maxConnsPerIp = flag.Int(
// "maxconnperip",
// 0,
// "The maximum number of connections allowed by server per ip.")
//maxReqPerConn = flag.Int(
// "maxreqperconn",
// 0,
// "The maximum number of requests allowed per connection.")
//tcpKeepAlive = flag.Bool(
// "tcpkeepalive",
// false,
// "Enable/Disable TCP keep alive")
//reduceMemory = flag.Bool(
// "reducememory",
// false,
// "Start the server in reduce memory mode, this will " +
// "try to minimise the amount of memory used. (This may hinder performance)")
//maxRequestBodySize = flag.Int(
// "maxreqsize", 0, "The maximum body size allowed in a request.")
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
flag.Parse()
if *app == "" {
log.Fatalln("--app is a required flag, e.g 'myfile:app'")
} else if *adapter == "" {
log.Fatalln("--adapter is a required flag, e.g 'asgi'")
}
var targetFile string
splitString := strings.Split(*app, ":")
if len(splitString) == 2 {
targetFile = splitString[0]
} else {
log.Printf(
"Cannot split %v into file and object parts, "+
"make sure the format is `file:object` e.g. `my_file:app`", *app)
return
}
free, err := getFreePort()
if err != nil {
log.Fatalln(err)
}
manager := process_manager.ExternalWorkers{
RunnerCall: *workerCommand,
TargetFile: targetFile,
App: *app,
Adapter: *adapter,
ConnectionPort: free,
WorkerCount: *processRatio,
ShardsPerProc: *shardsPerProc,
WorkerAuth: randStringBytes(16),
}
startServers(*host, *workerCount, manager)
}
// Starts the main servers, it will only start worker servers if
// the process is a child because the main thread is used for
// process management and does not connect to a socket.
func startServers(host string, workerCount int, workerManager process_manager.ExternalWorkers) {
if prefork.IsChild() {
ended := make(chan error)
go func() {
err := server.StartWorkerServer(workerManager)
ended <- err
}()
go func() {
server.StartMainServer(host, workerCount)
ended <- nil
}()
err := <-ended
if err != nil {
log.Fatalln(err)
}
log.Println("Shutting down server...")
} else {
server.StartMainServer(host, workerCount)
}
}
// Produces a free port from polling the OS.
func getFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
// Random Auth generation
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func randStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}