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
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Embracing the Ebb and Flow
colly
86
4.7k
Building Applications with DynamoDB
mza
95
6.5k
Navigating Team Friction
lara
187
15k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
138
34k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
The Pragmatic Product Professional
lauravandoore
35
6.7k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
107
19k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.8k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
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を使った並行処理
ご静聴ありがとうございました。