forked from Ja7ad/grpc-unix-socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc.go
55 lines (46 loc) · 976 Bytes
/
grpc.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
package main
import (
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
"log"
"net"
"os"
"os/signal"
"syscall"
)
const (
// unix socket
PROTOCOL = "unix"
SOCKET = "/tmp/grpc.sock"
// tcp protocol
PROTOCOL_TCP = "tcp"
ADDR = "localhost:3000"
)
func main() {
ln, err := net.Listen(PROTOCOL, SOCKET)
if err != nil {
log.Fatal(err)
}
tcpLn, err := net.Listen(PROTOCOL_TCP, ADDR)
if err != nil {
log.Fatal(err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
os.Remove(SOCKET)
os.Exit(1)
}()
srv := grpc.NewServer()
grpc_health_v1.RegisterHealthServer(srv, health.NewServer())
reflection.Register(srv)
go func() {
log.Printf("grpc ran on tcp protocol %s", ADDR)
log.Fatal(srv.Serve(tcpLn))
}()
log.Printf("grpc ran on unix socket protocol %s", SOCKET)
log.Fatal(srv.Serve(ln))
}