Skip to content

第9回Go勉強会の課題@luccafort #50

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

luccafort
Copy link

課題

https://mf.esa.io/posts/131091#%E8%AA%B2%E9%A1%8C

  • Greeter.SayHelloの結果をプリントする。
  • Notifier.PereodicHelloでストリームを受け取り、受け取ったメッセージをプリントする。

ハマりどころ

make setupgem install grpc-tools がSystemのほうのGemを参照していた少し困った。
解決策は書いたが環境系の問題に引っかかると面倒。
https://moneyforward.slack.com/archives/CVB3XK04U/p1587301291079800

対応としてあっているのかわからないが自動生成されたコードの require 'proto/xxxx' というファイルのパスが正しくないために client.rbLoad Error になり続けて原因究明するのに時間がかかった。
最終的に

  1. 何も require していない grpc 関係ないコードを書く → OK
  2. require を復活させて grpc 関係ないコードを書く → NG
  3. 自動生成されたファイル以外を require させて grpc 関係ないコードを書く → OK

だったので require が正常に機能していないことを確認してパスを書き換えたらうまくいった……が自動生成されたものの読み込みに問題があるのを疑うのはなかなか厳しい。

ruby ./client.rb を実行することでHelloReplyの結果を出力する実装を追加
gRPCを用いたクライアントの実装
Streamなレスポンスを受け取ったときにどうRubyで扱うのかわからなかったのでチュートリアルで実装を参考にした
https://github.com/grpc/grpc.io/blob/master/content/docs/tutorials/basic/ruby.md
http_response = http_client.say_hello(request_opts)
puts http_response

puts '====================='
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ファイルを分割しても良かったんですが元々Go側のコードも分割はしてなかったので1つのファイルで実行しています。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この標準出力は通常のレスポンスとStreamなレスポンスをわかりやすくする境界線として出力しているだけで特に意味はないです。


request_opts = Sample::HelloRequest.new(first_name: '太郎', last_name: '兼進')
stream_request = stream_client.pereodic_hello(request_opts)
stream_request.each { |req| puts req }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

今回、検証はしてないけどこの block 内で実行されているコード内で処理を抜けたい場合は条件式などで抜け出す条件を記載して return することになるのかな? 🤔
block 内で break すると意図しない挙動になるってチェリー本に書いてたのは記憶してるが詳細は忘れた。

# Generated by the protocol buffer compiler. DO NOT EDIT!
# Source: proto/helloworld.proto for package 'sample'

require 'grpc'
require 'proto/helloworld_pb'
require './proto/helloworld_pb'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは気づかない…。

# Generated by the protocol buffer compiler. DO NOT EDIT!
# Source: proto/notification.proto for package 'sample'

require 'grpc'
require 'proto/notification_pb'
require './proto/notification_pb'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは気づかない…。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant