New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
examples: unix abstract socket #4848
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//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" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"google.golang.org/grpc" | ||
ecpb "google.golang.org/grpc/examples/features/proto/echo" | ||
) | ||
|
||
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() { | ||
// 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 := "unix-abstract:abstract-unix-socket" | ||
cc, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock()) | ||
if err != nil { | ||
log.Fatalf("grpc.Dial(%q) failed: %v", addr, err) | ||
} | ||
defer cc.Close() | ||
|
||
fmt.Printf("--- calling echo.Echo/UnaryEcho to %s\n", addr) | ||
makeRPCs(cc, 10) | ||
fmt.Println() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//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" | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
"google.golang.org/grpc" | ||
|
||
pb "google.golang.org/grpc/examples/features/proto/echo" | ||
) | ||
|
||
type ecServer struct { | ||
pb.UnimplementedEchoServer | ||
addr string | ||
} | ||
|
||
func (s *ecServer) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) { | ||
return &pb.EchoResponse{Message: fmt.Sprintf("%s (from %s)", req.Message, s.addr)}, nil | ||
} | ||
|
||
func main() { | ||
netw, addr := "unix", "\x00abstract-unix-socket" | ||
lis, err := net.Listen(netw, addr) | ||
if err != nil { | ||
log.Fatalf("net.Listen(%q, %q) failed: %v", netw, addr, err) | ||
} | ||
s := grpc.NewServer() | ||
pb.RegisterEchoServer(s, &ecServer{addr: addr}) | ||
log.Printf("serving on %s\n", lis.Addr().String()) | ||
if err := s.Serve(lis); err != nil { | ||
log.Fatalf("failed to serve: %v", err) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, for future reference:
unix:@abstract-unix-socket
should also work fine for this because of golang conventions (net.Dial
behavior). I don't think we should recommend this since we addedunix-abstract
for cross-language compatibility.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment here for the same. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like documenting this is worse than leaving it unmentioned. It's more likely to go undiscovered without an explicit comment.