Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
GoとServer-Sent Events
Search
shuntaka
PRO
June 30, 2023
0
8.9k
GoとServer-Sent Events
shuntaka
PRO
June 30, 2023
Tweet
Share
More Decks by shuntaka
See All by shuntaka
話題のMCPとは何か? 基礎知識と活用事例をご紹介 / What is the Trending MCP? Introduction to Basic Knowledge and Practical Applications
shuntaka
PRO
0
420
MCPに入門する/ Introduction to MCP
shuntaka
PRO
98
59k
RustでGoogle CalendarのMCPを実装した話 / Implementing Google Calendar MCP in Rust
shuntaka
PRO
4
1.7k
サーバーレスAPI(API Gateway+Lambda)とNext.jsで 個人ブログを作ろう!
shuntaka
PRO
0
1.5k
NestJS Prisma on Fargate構成で作るWeb API開発Tips
shuntaka
PRO
1
16k
GoでDynamoDBのCLI(便利)を作った
shuntaka
PRO
0
130
20190827_Auth0_Rules_使ってみた.pdf
shuntaka
PRO
0
300
AWSのCI/CDサービスを試してみた!
shuntaka
PRO
0
260
Goでブラウザ業務を自動化する
shuntaka
PRO
0
2.3k
Featured
See All Featured
BBQ
matthewcrist
88
9.6k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
Git: the NoSQL Database
bkeepers
PRO
430
65k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
Fontdeck: Realign not Redesign
paulrobertlloyd
83
5.5k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
GitHub's CSS Performance
jonrohan
1030
460k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
4 Signs Your Business is Dying
shpigford
183
22k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
390
Site-Speed That Sticks
csswizardry
5
500
Transcript
GoとServer-Sent Events shuntaka 2022/04/12 Let's Go Talk #9 (GoのLT会)
自己紹介 宜しくお願いします😌 埼玉からお送りしています! GitHub: https://github.com/shuntaka9576 blog: https://shuntaka.dev 仕事: AWS,IoT,サーバサイド,TypeScript,Pythonなど Go歴:
仕事ではほぼなく、ときどき趣味で書きます、、
目次 ・そもそもServer-Sent Events ・Server-Sent Eventsを調査した経緯 ・Goで実装する ・作ったツールの紹介
そもそも Server-Sent Events
Server-Sent Events(出典,詳細: MDN) リアルタイム通信をWebアプリケーションに実装するための技術 ・HTTP/1.1とHTTP/2に対応 ・単方向通信 WebSocketは双方向で、専用プロトコル
None
None
フィールドは複数ある(出典,詳細: MDN) ・event … イベントの種別 ・data … メッセージ内容(今回はdataだけ知っていればOK) ・id …
EventSourceに設定するイベント ID ・retry … 指定時間待機後再接続要求
Server-Sent Eventsを調査した 経緯
OpenAI API ・OpenAIのAPIのサービス ・ChatGPTのWeb版(chat.openai.com)の機能がAPIとして提供 されている
ChatGPTの課金体系 ・ChatGPTのサブスクとAPIは課金体系が**別** ・ChatGPT Plusはサブスク$20 ・OpenAI APIは従量課金 ※ Open AI APIのGPT-4は、waitlistに登録しないと使えない。
仮説 使い方次第ではサブスクよりAPIを使った方が安くなるケースがあ りそう。
CLIを作ろう Gopher CLI作りがち問題...
CLI設計 ・Web版のような徐々に出力される体験にしたい 🤔
参考
Goで実装する
実装方法 1. github.com/sashabaranov/go-openai 2. github.com/r3labs/sse 3. 自前
3を選択 1. github.com/sashabaranov/go-openai => 多分一番穏便(ライブラリ内でSSEは自前実装) 2. github.com/r3labs/sse => GET以外のメソッドで未対応。パッチ当てればOK 3.
自前 => 2.を参考にしつつ実装した
None
None
GitHubコード Response.Bodyをリアルタイム逐次読み出し
None
*bufio.Scanner *Response.Body (io.Reader) ストリーム読み 取りgoroutine ① ReadEvent ② Response GitHubコード
改行\n\nで逐次読み出し scanner.Split(splitFunc) メインgoroutine channel ② Response ③ ④ 標準出力 Server-Sent Event
None
作ったツールの紹介 github.com/shuntaka9576/oax
詳細はZennのブログ記事へ
oax(OpenAI eXecuter) 動作 その1 所要2分くらいあれば実演
oax(OpenAI eXecuter) 動作 その1
oax(OpenAI eXecuter) 動作 その2
使ってよかったライブラリ github.com/alecthomas/kong github.com/itchyny/timefmt-go github.com/ktr0731/go-fuzzyfinder(2つ目Gifは99.9%こちらのラ イブラリのおかげ)
まとめ SSEのリアルタイムイベントハンドリングは、Goの得意分野 ・net/httpの豊富な機能 ・io.Readerのリアルタイムストリームを読み出し ・goroutineやchannelを使った並行処理
ご静聴ありがとうございました。