main.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Binary client is an example client.
  19. package main
  20. import (
  21. "context"
  22. "flag"
  23. "fmt"
  24. "io"
  25. "log"
  26. "time"
  27. "google.golang.org/grpc"
  28. pb "google.golang.org/grpc/examples/features/proto/echo"
  29. "google.golang.org/grpc/metadata"
  30. )
  31. var addr = flag.String("addr", "localhost:50051", "the address to connect to")
  32. const (
  33. timestampFormat = time.StampNano // "Jan _2 15:04:05.000"
  34. streamingCount = 10
  35. )
  36. func unaryCallWithMetadata(c pb.EchoClient, message string) {
  37. fmt.Printf("--- unary ---\n")
  38. // Create metadata and context.
  39. md := metadata.Pairs("timestamp", time.Now().Format(timestampFormat))
  40. ctx := metadata.NewOutgoingContext(context.Background(), md)
  41. // Make RPC using the context with the metadata.
  42. var header, trailer metadata.MD
  43. r, err := c.UnaryEcho(ctx, &pb.EchoRequest{Message: message}, grpc.Header(&header), grpc.Trailer(&trailer))
  44. if err != nil {
  45. log.Fatalf("failed to call UnaryEcho: %v", err)
  46. }
  47. if t, ok := header["timestamp"]; ok {
  48. fmt.Printf("timestamp from header:\n")
  49. for i, e := range t {
  50. fmt.Printf(" %d. %s\n", i, e)
  51. }
  52. } else {
  53. log.Fatal("timestamp expected but doesn't exist in header")
  54. }
  55. if l, ok := header["location"]; ok {
  56. fmt.Printf("location from header:\n")
  57. for i, e := range l {
  58. fmt.Printf(" %d. %s\n", i, e)
  59. }
  60. } else {
  61. log.Fatal("location expected but doesn't exist in header")
  62. }
  63. fmt.Printf("response:\n")
  64. fmt.Printf(" - %s\n", r.Message)
  65. if t, ok := trailer["timestamp"]; ok {
  66. fmt.Printf("timestamp from trailer:\n")
  67. for i, e := range t {
  68. fmt.Printf(" %d. %s\n", i, e)
  69. }
  70. } else {
  71. log.Fatal("timestamp expected but doesn't exist in trailer")
  72. }
  73. }
  74. func serverStreamingWithMetadata(c pb.EchoClient, message string) {
  75. fmt.Printf("--- server streaming ---\n")
  76. // Create metadata and context.
  77. md := metadata.Pairs("timestamp", time.Now().Format(timestampFormat))
  78. ctx := metadata.NewOutgoingContext(context.Background(), md)
  79. // Make RPC using the context with the metadata.
  80. stream, err := c.ServerStreamingEcho(ctx, &pb.EchoRequest{Message: message})
  81. if err != nil {
  82. log.Fatalf("failed to call ServerStreamingEcho: %v", err)
  83. }
  84. // Read the header when the header arrives.
  85. header, err := stream.Header()
  86. if err != nil {
  87. log.Fatalf("failed to get header from stream: %v", err)
  88. }
  89. // Read metadata from server's header.
  90. if t, ok := header["timestamp"]; ok {
  91. fmt.Printf("timestamp from header:\n")
  92. for i, e := range t {
  93. fmt.Printf(" %d. %s\n", i, e)
  94. }
  95. } else {
  96. log.Fatal("timestamp expected but doesn't exist in header")
  97. }
  98. if l, ok := header["location"]; ok {
  99. fmt.Printf("location from header:\n")
  100. for i, e := range l {
  101. fmt.Printf(" %d. %s\n", i, e)
  102. }
  103. } else {
  104. log.Fatal("location expected but doesn't exist in header")
  105. }
  106. // Read all the responses.
  107. var rpcStatus error
  108. fmt.Printf("response:\n")
  109. for {
  110. r, err := stream.Recv()
  111. if err != nil {
  112. rpcStatus = err
  113. break
  114. }
  115. fmt.Printf(" - %s\n", r.Message)
  116. }
  117. if rpcStatus != io.EOF {
  118. log.Fatalf("failed to finish server streaming: %v", rpcStatus)
  119. }
  120. // Read the trailer after the RPC is finished.
  121. trailer := stream.Trailer()
  122. // Read metadata from server's trailer.
  123. if t, ok := trailer["timestamp"]; ok {
  124. fmt.Printf("timestamp from trailer:\n")
  125. for i, e := range t {
  126. fmt.Printf(" %d. %s\n", i, e)
  127. }
  128. } else {
  129. log.Fatal("timestamp expected but doesn't exist in trailer")
  130. }
  131. }
  132. func clientStreamWithMetadata(c pb.EchoClient, message string) {
  133. fmt.Printf("--- client streaming ---\n")
  134. // Create metadata and context.
  135. md := metadata.Pairs("timestamp", time.Now().Format(timestampFormat))
  136. ctx := metadata.NewOutgoingContext(context.Background(), md)
  137. // Make RPC using the context with the metadata.
  138. stream, err := c.ClientStreamingEcho(ctx)
  139. if err != nil {
  140. log.Fatalf("failed to call ClientStreamingEcho: %v\n", err)
  141. }
  142. // Read the header when the header arrives.
  143. header, err := stream.Header()
  144. if err != nil {
  145. log.Fatalf("failed to get header from stream: %v", err)
  146. }
  147. // Read metadata from server's header.
  148. if t, ok := header["timestamp"]; ok {
  149. fmt.Printf("timestamp from header:\n")
  150. for i, e := range t {
  151. fmt.Printf(" %d. %s\n", i, e)
  152. }
  153. } else {
  154. log.Fatal("timestamp expected but doesn't exist in header")
  155. }
  156. if l, ok := header["location"]; ok {
  157. fmt.Printf("location from header:\n")
  158. for i, e := range l {
  159. fmt.Printf(" %d. %s\n", i, e)
  160. }
  161. } else {
  162. log.Fatal("location expected but doesn't exist in header")
  163. }
  164. // Send all requests to the server.
  165. for i := 0; i < streamingCount; i++ {
  166. if err := stream.Send(&pb.EchoRequest{Message: message}); err != nil {
  167. log.Fatalf("failed to send streaming: %v\n", err)
  168. }
  169. }
  170. // Read the response.
  171. r, err := stream.CloseAndRecv()
  172. if err != nil {
  173. log.Fatalf("failed to CloseAndRecv: %v\n", err)
  174. }
  175. fmt.Printf("response:\n")
  176. fmt.Printf(" - %s\n\n", r.Message)
  177. // Read the trailer after the RPC is finished.
  178. trailer := stream.Trailer()
  179. // Read metadata from server's trailer.
  180. if t, ok := trailer["timestamp"]; ok {
  181. fmt.Printf("timestamp from trailer:\n")
  182. for i, e := range t {
  183. fmt.Printf(" %d. %s\n", i, e)
  184. }
  185. } else {
  186. log.Fatal("timestamp expected but doesn't exist in trailer")
  187. }
  188. }
  189. func bidirectionalWithMetadata(c pb.EchoClient, message string) {
  190. fmt.Printf("--- bidirectional ---\n")
  191. // Create metadata and context.
  192. md := metadata.Pairs("timestamp", time.Now().Format(timestampFormat))
  193. ctx := metadata.NewOutgoingContext(context.Background(), md)
  194. // Make RPC using the context with the metadata.
  195. stream, err := c.BidirectionalStreamingEcho(ctx)
  196. if err != nil {
  197. log.Fatalf("failed to call BidirectionalStreamingEcho: %v\n", err)
  198. }
  199. go func() {
  200. // Read the header when the header arrives.
  201. header, err := stream.Header()
  202. if err != nil {
  203. log.Fatalf("failed to get header from stream: %v", err)
  204. }
  205. // Read metadata from server's header.
  206. if t, ok := header["timestamp"]; ok {
  207. fmt.Printf("timestamp from header:\n")
  208. for i, e := range t {
  209. fmt.Printf(" %d. %s\n", i, e)
  210. }
  211. } else {
  212. log.Fatal("timestamp expected but doesn't exist in header")
  213. }
  214. if l, ok := header["location"]; ok {
  215. fmt.Printf("location from header:\n")
  216. for i, e := range l {
  217. fmt.Printf(" %d. %s\n", i, e)
  218. }
  219. } else {
  220. log.Fatal("location expected but doesn't exist in header")
  221. }
  222. // Send all requests to the server.
  223. for i := 0; i < streamingCount; i++ {
  224. if err := stream.Send(&pb.EchoRequest{Message: message}); err != nil {
  225. log.Fatalf("failed to send streaming: %v\n", err)
  226. }
  227. }
  228. stream.CloseSend()
  229. }()
  230. // Read all the responses.
  231. var rpcStatus error
  232. fmt.Printf("response:\n")
  233. for {
  234. r, err := stream.Recv()
  235. if err != nil {
  236. rpcStatus = err
  237. break
  238. }
  239. fmt.Printf(" - %s\n", r.Message)
  240. }
  241. if rpcStatus != io.EOF {
  242. log.Fatalf("failed to finish server streaming: %v", rpcStatus)
  243. }
  244. // Read the trailer after the RPC is finished.
  245. trailer := stream.Trailer()
  246. // Read metadata from server's trailer.
  247. if t, ok := trailer["timestamp"]; ok {
  248. fmt.Printf("timestamp from trailer:\n")
  249. for i, e := range t {
  250. fmt.Printf(" %d. %s\n", i, e)
  251. }
  252. } else {
  253. log.Fatal("timestamp expected but doesn't exist in trailer")
  254. }
  255. }
  256. const message = "this is examples/metadata"
  257. func main() {
  258. flag.Parse()
  259. // Set up a connection to the server.
  260. conn, err := grpc.Dial(*addr, grpc.WithInsecure())
  261. if err != nil {
  262. log.Fatalf("did not connect: %v", err)
  263. }
  264. defer conn.Close()
  265. c := pb.NewEchoClient(conn)
  266. unaryCallWithMetadata(c, message)
  267. time.Sleep(1 * time.Second)
  268. serverStreamingWithMetadata(c, message)
  269. time.Sleep(1 * time.Second)
  270. clientStreamWithMetadata(c, message)
  271. time.Sleep(1 * time.Second)
  272. bidirectionalWithMetadata(c, message)
  273. }