UNIX插座协议上的GRPC
#go #grpc

GitHub logo Ja7ad / grpc-unix-socket

UNIX插座协议上的GRPC

GRPC over unix socket Protocol

Unix sockets, or Unix Domain Sockets, allow bidirectional data exchange between processes running on the same machine In order to transmit data between processes, the file system provides a reliable and efficient mechanism. The kernel is the only component involved in communication between processes. The processes communicate by reading and writing to the same socket file, which is managed by the kernel. Kernels handle communication details, such as synchronization, buffering, and error handling, and ensure that data is delivered reliably and correctly.

Advantage :

  • Fast communication
  • Efficient
  • Low overhead
  • Stability

Disadvantages :

  • Limited on same machine
sequenceDiagram

Client->>+Server: Health Request (1.02187 µs per response)

Server->>+Client: Serve message
$ netstat -a -p --unix | grep grpc
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
unix  2      [ ACC ]     STREAM     LISTENING

UNIX插座或UNIX域插座,允许在同一机器上运行的进程之间的双向数据交换。
为了在过程之间传输数据,文件系统提供了可靠,有效的机制。内核是过程之间通信涉及的唯一组件。这些过程通过读取和写入与内核管理的同一套接字文件进行通信。内核处理通信细节,例如同步,缓冲和错误处理,并确保可靠,正确地传递数据。

优势:

  • 快速交流
  • 有效
  • 低顶间
  • 稳定性

缺点:

  • 在同一台机器上有限

unix socket

$ netstat -a -p --unix | grep grpc

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
unix  2      [ ACC ]     STREAM     LISTENING     119977   98036/main           /tmp/grpc.sock

TCP/IP插座是通过网络之间在过程之间进行通信的机制。

tcp

测试基准和分析

goos: linux
goarch: amd64
pkg: github.com/Ja7ad/grpc-unix-socket/server
cpu: Intel(R) Core(TM) i5-3570 CPU @ 3.40GHz
Benchmark_UNIX
Benchmark_UNIX-4          100000            102187 ns/op            4960 B/op         96 allocs/op
goos: linux
goarch: amd64
pkg: github.com/Ja7ad/grpc-unix-socket/server
cpu: Intel(R) Core(TM) i5-3570 CPU @ 3.40GHz
Benchmark_TCP
Benchmark_TCP-4           100000            127188 ns/op            4961 B/op         96 allocs/op

100K基准测试的结果为20个时间测试,为新的/旧测试(台式)

name     old time/op    new time/op    delta
_UNIX-4     100µs ± 2%     100µs ± 2%    ~     (p=0.668 n=17+20)
_TCP-4      125µs ± 1%     124µs ± 1%  -0.15%  (p=0.031 n=20+20)

name     old alloc/op   new alloc/op   delta
_UNIX-4    4.96kB ± 0%    4.96kB ± 0%    ~     (p=0.454 n=20+20)
_TCP-4     4.96kB ± 0%    4.96kB ± 0%    ~     (all equal)

name     old allocs/op  new allocs/op  delta
_UNIX-4      96.0 ± 0%      96.0 ± 0%    ~     (all equal)
_TCP-4       96.0 ± 0%      96.0 ± 0%    ~     (all equal)

用户酶

当您在同一台计算机上运行许多服务并在容器或主机中运行它们时,您可以使用UNIX插座以进行高效且快速的通信。

容器需要通过卷共享袜子文件,因为UNIX插座套件上的文件。

docker volume create --name=socket
docker run socket:/var/socket serviceA
docker run socket:/var/socket serviceB

如何测试服务器/客户端?

服务器:

  • 运行服务器(GO1.19):
$ go run -mod vendor grpc.go
  • 基准命令:
$ go test -bench=. -benchtime=100000x -benchmem