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
June 30, 2023
0
9k
GoとServer-Sent Events
shuntaka
June 30, 2023
Tweet
Share
More Decks by shuntaka
See All by shuntaka
リモートMCP + MCP業務取り組み例 / Remote MCP + MCP Business Initiative Examples
shuntaka
0
1.4k
話題のMCPとは何か? 基礎知識と活用事例をご紹介 / What is the Trending MCP? Introduction to Basic Knowledge and Practical Applications
shuntaka
0
840
MCPに入門する/ Introduction to MCP
shuntaka
102
66k
RustでGoogle CalendarのMCPを実装した話 / Implementing Google Calendar MCP in Rust
shuntaka
4
2.1k
サーバーレスAPI(API Gateway+Lambda)とNext.jsで 個人ブログを作ろう!
shuntaka
0
1.6k
NestJS Prisma on Fargate構成で作るWeb API開発Tips
shuntaka
1
16k
GoでDynamoDBのCLI(便利)を作った
shuntaka
0
140
20190827_Auth0_Rules_使ってみた.pdf
shuntaka
0
310
AWSのCI/CDサービスを試してみた!
shuntaka
0
270
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.4k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
It's Worth the Effort
3n
185
28k
Optimizing for Happiness
mojombo
379
70k
Stop Working from a Prison Cell
hatefulcrawdad
270
20k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
700
Designing for humans not robots
tammielis
253
25k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
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を使った並行処理
ご静聴ありがとうございました。