1
0
mirror of https://github.com/golang/go synced 2024-11-25 11:57:58 -07:00

clean up server code in tutorial

R=rsc
DELTA=15  (1 added, 0 deleted, 14 changed)
OCL=23889
CL=23889
This commit is contained in:
Rob Pike 2009-01-30 10:18:58 -08:00
parent 391425ae55
commit 96777ea2a7
4 changed files with 15 additions and 14 deletions

View File

@ -755,7 +755,7 @@ With channels, it's possible to serve multiple independent client goroutines wit
writing an actual multiplexer. The trick is to send the server a channel in the message, writing an actual multiplexer. The trick is to send the server a channel in the message,
which it will then use to reply to the original sender. which it will then use to reply to the original sender.
A realistic client-server program is a lot of code, so here is a very simple substitute A realistic client-server program is a lot of code, so here is a very simple substitute
to illustrate the idea. It starts by defining a "Request" type, which embeds a channel to illustrate the idea. It starts by defining a "request" type, which embeds a channel
that will be used for the reply. that will be used for the reply.
--PROG progs/server.go /type.request/ /^}/ --PROG progs/server.go /type.request/ /^}/

View File

@ -68,7 +68,8 @@ testit print_string "" "77 Sunset Strip"
testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29" testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29"
testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29" testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29"
# server hangs; don't run it # server hangs; don't run it, just compile it
6g server.go
testit server1 "" "" testit server1 "" ""
rm -f 6.out *.6 rm -f 6.out *.6

View File

@ -11,21 +11,21 @@ type request struct {
type binOp (a, b int) int; type binOp (a, b int) int;
func run(op *BinOp, request *Request) { func run(op *binOp, req *request) {
result := op(request.a, request.b); result := op(req.a, req.b);
request.replyc <- result; req.replyc <- result;
} }
func server(op *BinOp, service chan *Request) { func server(op *binOp, service chan *request) {
for { for {
request := <-service; req := <-service;
go run(op, request); // don't wait for it go run(op, req); // don't wait for it
} }
} }
func startServer(op *BinOp) chan *Request { func startServer(op *binOp) chan *request {
req := make(chan *Request); req := make(chan *request);
go Server(op, req); go server(op, req);
return req; return req;
} }

View File

@ -11,9 +11,9 @@ type request struct {
type binOp (a, b int) int; type binOp (a, b int) int;
func run(op *binOp, request *request) { func run(op *binOp, req *request) {
result := op(request.a, request.b); result := op(req.a, req.b);
request.replyc <- result; req.replyc <- result;
} }
func server(op *binOp, service chan *request, quit chan bool) { func server(op *binOp, service chan *request, quit chan bool) {