snapshot of https://github.com/grpc/grpc-go.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
This commit is contained in:
29
examples/features/unix_abstract/README.md
Normal file
29
examples/features/unix_abstract/README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Unix abstract sockets
|
||||
|
||||
This examples shows how to start a gRPC server listening on a unix abstract
|
||||
socket and how to get a gRPC client to connect to it.
|
||||
|
||||
## What is a unix abstract socket
|
||||
|
||||
An abstract socket address is distinguished from a regular unix socket by the
|
||||
fact that the first byte of the address is a null byte ('\0'). The address has
|
||||
no connection with filesystem path names.
|
||||
|
||||
## Try it
|
||||
|
||||
```
|
||||
go run server/main.go
|
||||
```
|
||||
|
||||
```
|
||||
go run client/main.go
|
||||
```
|
||||
|
||||
## Explanation
|
||||
|
||||
The gRPC server in this example listens on an address starting with a null byte
|
||||
and the network is `unix`. The client uses the `unix-abstract` scheme with the
|
||||
endpoint set to the abstract unix socket address without the null byte. The
|
||||
`unix` resolver takes care of adding the null byte on the client. See
|
||||
https://github.com/grpc/grpc/blob/master/doc/naming.md for the more details.
|
||||
|
||||
75
examples/features/unix_abstract/client/main.go
Normal file
75
examples/features/unix_abstract/client/main.go
Normal file
@@ -0,0 +1,75 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright 2021 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
// Binary client is an example client which dials a server on an abstract unix
|
||||
// socket.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
ecpb "google.golang.org/grpc/examples/features/proto/echo"
|
||||
)
|
||||
|
||||
var (
|
||||
// A dial target of `unix:@abstract-unix-socket` should also work fine for
|
||||
// this example because of golang conventions (net.Dial behavior). But we do
|
||||
// not recommend this since we explicitly added the `unix-abstract` scheme
|
||||
// for cross-language compatibility.
|
||||
addr = flag.String("addr", "abstract-unix-socket", "The unix abstract socket address")
|
||||
)
|
||||
|
||||
func callUnaryEcho(c ecpb.EchoClient, message string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
r, err := c.UnaryEcho(ctx, &ecpb.EchoRequest{Message: message})
|
||||
if err != nil {
|
||||
log.Fatalf("could not greet: %v", err)
|
||||
}
|
||||
fmt.Println(r.Message)
|
||||
}
|
||||
|
||||
func makeRPCs(cc *grpc.ClientConn, n int) {
|
||||
hwc := ecpb.NewEchoClient(cc)
|
||||
for i := 0; i < n; i++ {
|
||||
callUnaryEcho(hwc, "this is examples/unix_abstract")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
sockAddr := fmt.Sprintf("unix-abstract:%v", *addr)
|
||||
cc, err := grpc.NewClient(sockAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
log.Fatalf("grpc.NewClient(%q) failed: %v", sockAddr, err)
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
fmt.Printf("--- calling echo.Echo/UnaryEcho to %s\n", sockAddr)
|
||||
makeRPCs(cc, 10)
|
||||
fmt.Println()
|
||||
}
|
||||
65
examples/features/unix_abstract/server/main.go
Normal file
65
examples/features/unix_abstract/server/main.go
Normal file
@@ -0,0 +1,65 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright 2021 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
// Binary server is an example server listening for gRPC connections on an
|
||||
// abstract unix socket.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
pb "google.golang.org/grpc/examples/features/proto/echo"
|
||||
)
|
||||
|
||||
var (
|
||||
addr = flag.String("addr", "abstract-unix-socket", "The unix abstract socket address")
|
||||
)
|
||||
|
||||
type ecServer struct {
|
||||
pb.UnimplementedEchoServer
|
||||
addr string
|
||||
}
|
||||
|
||||
func (s *ecServer) UnaryEcho(_ context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
|
||||
return &pb.EchoResponse{Message: fmt.Sprintf("%s (from %s)", req.Message, s.addr)}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
netw := "unix"
|
||||
socketAddr := fmt.Sprintf("@%v", *addr)
|
||||
lis, err := net.Listen(netw, socketAddr)
|
||||
if err != nil {
|
||||
log.Fatalf("net.Listen(%q, %q) failed: %v", netw, socketAddr, err)
|
||||
}
|
||||
s := grpc.NewServer()
|
||||
pb.RegisterEchoServer(s, &ecServer{addr: socketAddr})
|
||||
log.Printf("serving on %s\n", lis.Addr().String())
|
||||
if err := s.Serve(lis); err != nil {
|
||||
log.Fatalf("failed to serve: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user