Skip to content

Commit

Permalink
UNIX DOMAIN SOCKET
Browse files Browse the repository at this point in the history
  • Loading branch information
pikachu0310 committed Nov 25, 2023
1 parent 45761fe commit 9904b07
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions s1/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ ISUCON13_MYSQL_DIALCONFIG_PARSETIME="true"
ISUCON13_POWERDNS_SUBDOMAIN_ADDRESS="176.34.46.12"
ISUCON13_POWERDNS_DISABLED="false"
PPROTEIN_GIT_REPOSITORY=/home/isucon
USE_SOCKET=1
9 changes: 7 additions & 2 deletions s1/etc/nginx/sites-enabled/isupipe.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
upstream app_local {
keepalive 256;
server unix:/tmp/app.sock;
}

server {
listen 80 default_server;
server_name _;
Expand Down Expand Up @@ -43,6 +48,6 @@ server {
}
location /api {
proxy_set_header Host $host;
proxy_pass http://localhost:8080;
proxy_pass http://app_local;
}
}
}
31 changes: 26 additions & 5 deletions webapp/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,32 @@ func main() {
}
powerDNSSubdomainAddress = subdomainAddr

// HTTPサーバ起動
listenAddr := net.JoinHostPort("", strconv.Itoa(listenPort))
if err := e.Start(listenAddr); err != nil {
e.Logger.Errorf("failed to start HTTP server: %v", err)
os.Exit(1)
if os.Getenv("USE_SOCKET") == "1" {
// ここからソケット接続設定 ---
socket_file := "/tmp/app.sock"
os.Remove(socket_file)

l, err := net.Listen("unix", socket_file)
if err != nil {
e.Logger.Fatal(err)
}

// go runユーザとnginxのユーザ(グループ)を同じにすれば777じゃなくてok
err = os.Chmod(socket_file, 0777)
if err != nil {
e.Logger.Fatal(err)
}

e.Listener = l
e.Logger.Fatal(e.Start(""))
// ここまで ---
} else {
// HTTPサーバ起動
listenAddr := net.JoinHostPort("", strconv.Itoa(listenPort))
if err := e.Start(listenAddr); err != nil {
e.Logger.Errorf("failed to start HTTP server: %v", err)
os.Exit(1)
}
}
}

Expand Down

0 comments on commit 9904b07

Please sign in to comment.