From 69f1852be59bed75d011ecbc30d28ecd96148b5a Mon Sep 17 00:00:00 2001 From: devlights Date: Sat, 21 Jan 2023 07:49:32 +0000 Subject: [PATCH] refactoring --- .gitignore | 1 + README.md | 9 +++--- Taskfile.yml | 3 +- cmd/client/client.go | 66 ++++++++++++++++++++++++++++++++++---------- cmd/server/server.go | 23 ++++++++------- 5 files changed, 73 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 99b794f..cc21dac 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ # vendor/ bin/ +text-files/ diff --git a/README.md b/README.md index 4155246..a234ca8 100644 --- a/README.md +++ b/README.md @@ -34,12 +34,12 @@ task run ```sh $ task install-requirements task: [install-requirements] mkdir tmp -task: [download-protoc] curl -L https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-linux-x86_64.zip --output protoc.zip +task: [_download-protoc] curl -L https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-linux-x86_64.zip --output protoc.zip % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 -100 1547k 100 1547k 0 0 1345k 0 0:00:01 0:00:01 --:--:-- 3530k -task: [unzip-protc] unzip ./protoc.zip -d protoc +100 1548k 100 1548k 0 0 4254k 0 --:--:-- --:--:-- --:--:-- 4254k +task: [_unzip-protoc] unzip ./protoc.zip -d protoc Archive: ./protoc.zip inflating: protoc/bin/protoc inflating: protoc/include/google/protobuf/any.proto @@ -57,10 +57,11 @@ Archive: ./protoc.zip inflating: protoc/readme.txt task: [install-requirements] mkdir -p bin task: [install-requirements] rm -rf bin/protoc -task: [locate-protoc] mv -f ./protoc/ ../bin +task: [_locate-protoc] mv -f ./protoc/ ../bin task: [install-requirements] rm -rf ./tmp task: [install-requirements] go install google.golang.org/protobuf/cmd/protoc-gen-go@latest task: [install-requirements] go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest +task: [install-requirements] rm -rf ./text-files; git clone --quiet https://github.com/devlights/text-files $ task protoc diff --git a/Taskfile.yml b/Taskfile.yml index 9244db5..7df2379 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -13,6 +13,7 @@ tasks: - rm -rf ./tmp - go install google.golang.org/protobuf/cmd/protoc-gen-go@latest - go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest + - rm -rf ./text-files; git clone --quiet https://github.com/devlights/text-files _download-protoc: dir: tmp cmds: @@ -35,6 +36,6 @@ tasks: cmds: - go run cmd/server/server.go & - sleep 1 - - go run cmd/client/client.go + - time go run cmd/client/client.go - sleep 1 - pkill server diff --git a/cmd/client/client.go b/cmd/client/client.go index 1b2e728..2fee00b 100644 --- a/cmd/client/client.go +++ b/cmd/client/client.go @@ -1,10 +1,22 @@ +// gRPC with Unix Domain Socket example (client side) +// +// # REFERENCES +// - https://qiita.com/marnie_ms4/items/4582a1a0db363fe246f3 +// - http://yamahiro0518.hatenablog.com/entry/2016/02/01/215908 +// - https://zenn.dev/hsaki/books/golang-grpc-starting/viewer/client +// - https://stackoverflow.com/a/46279623 +// - https://stackoverflow.com/a/18479916 + package main import ( + "bufio" "context" "fmt" + "io" "log" "net" + "os" "time" "github.com/devlights/go-grpc-uds-example/internal/pb" @@ -18,35 +30,61 @@ const ( ) func main() { - // - https://qiita.com/marnie_ms4/items/4582a1a0db363fe246f3 - // - http://yamahiro0518.hatenablog.com/entry/2016/02/01/215908 var ( rootCtx = context.Background() mainCtx, mainCxl = context.WithCancel(rootCtx) ) defer mainCxl() - dialer := func(ctx context.Context, addr string) (net.Conn, error) { - return net.Dial(protocol, addr) - } + // + // Connect + // + var ( + credentials = insecure.NewCredentials() // No SSL/TLS + dialer = func(ctx context.Context, addr string) (net.Conn, error) { + var d net.Dialer + return d.DialContext(ctx, protocol, addr) + } + options = []grpc.DialOption{ + grpc.WithTransportCredentials(credentials), + grpc.WithBlock(), + grpc.WithContextDialer(dialer), + } + ) - conn, err := grpc.Dial(sockAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialer)) + conn, err := grpc.Dial(sockAddr, options...) if err != nil { - log.Fatal(err) + panic(err) } defer conn.Close() + // + // Send & Recv + // var ( client = pb.NewEchoClient(conn) - values = []string{ - "hello world", - "golang", - "goroutine", - "this program is used gRPC with golang", - } + file = func() *os.File { + f, err := os.Open("text-files/agatha_complete.txt") + if err != nil { + panic(err) + } + return f + }() + values = func(r io.ReadCloser) <-chan string { + ch := make(chan string) + go func() { + defer r.Close() + defer close(ch) + scanner := bufio.NewScanner(r) + for scanner.Scan() { + ch <- scanner.Text() + } + }() + return ch + }(file) ) - for _, v := range values { + for v := range values { func() { ctx, cancel := context.WithTimeout(mainCtx, 1*time.Second) defer cancel() diff --git a/cmd/server/server.go b/cmd/server/server.go index ef51fc2..67b71ac 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -1,3 +1,13 @@ +// gRPC with Unix Domain Socket example (server side) +// +// # REFERENCES +// - https://qiita.com/marnie_ms4/items/4582a1a0db363fe246f3 +// - http://yamahiro0518.hatenablog.com/entry/2016/02/01/215908 +// - https://zenn.dev/hsaki/books/golang-grpc-starting/viewer/client +// - https://stackoverflow.com/a/46279623 +// - https://stackoverflow.com/a/18479916 +// - https://qiita.com/hnakamur/items/848097aad846d40ae84b + package main import ( @@ -16,19 +26,12 @@ const ( ) func main() { - // - https://qiita.com/marnie_ms4/items/4582a1a0db363fe246f3 - // - http://yamahiro0518.hatenablog.com/entry/2016/02/01/215908 - - cleanup := func() { - if _, err := os.Stat(sockAddr); err == nil { - if err := os.RemoveAll(sockAddr); err != nil { - log.Fatal(err) - } + if _, err := os.Stat(sockAddr); !os.IsNotExist(err) { + if err := os.RemoveAll(sockAddr); err != nil { + log.Fatal(err) } } - cleanup() - listener, err := net.Listen(protocol, sockAddr) if err != nil { log.Fatal(err)