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とは何か? 基礎知識と活用事例をご紹介 / What is the Trending MCP? Introduction to Basic Knowledge and Practical Applications
shuntaka
0
750
MCPに入門する/ Introduction to MCP
shuntaka
101
65k
RustでGoogle CalendarのMCPを実装した話 / Implementing Google Calendar MCP in Rust
shuntaka
4
2k
サーバーレスAPI(API Gateway+Lambda)とNext.jsで 個人ブログを作ろう!
shuntaka
0
1.5k
NestJS Prisma on Fargate構成で作るWeb API開発Tips
shuntaka
1
16k
GoでDynamoDBのCLI(便利)を作った
shuntaka
0
140
20190827_Auth0_Rules_使ってみた.pdf
shuntaka
0
300
AWSのCI/CDサービスを試してみた!
shuntaka
0
260
Goでブラウザ業務を自動化する
shuntaka
0
2.3k
Featured
See All Featured
Gamification - CAS2011
davidbonilla
81
5.3k
The Power of CSS Pseudo Elements
geoffreycrofte
76
5.8k
How to Ace a Technical Interview
jacobian
276
23k
Optimizing for Happiness
mojombo
378
70k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Building Flexible Design Systems
yeseniaperezcruz
329
39k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
470
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Code Review Best Practice
trishagee
68
18k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
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を使った並行処理
ご静聴ありがとうございました。