Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
devlights committed Jan 21, 2023
1 parent 8f40c29 commit 69f1852
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
# vendor/

bin/
text-files/
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
66 changes: 52 additions & 14 deletions cmd/client/client.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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()
Expand Down
23 changes: 13 additions & 10 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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)
Expand Down

0 comments on commit 69f1852

Please sign in to comment.