walf443 / yisucon_practice

0 stars 0 forks source link

スクショ・各パスの役割・どういうアプリケーションか・キーになる関数 #1

Open catatsuy opened 7 years ago

catatsuy commented 7 years ago

パス一覧

    r := mux.NewRouter()
    r.HandleFunc("/initialize", initializeHandler).Methods("GET")

    l := r.PathPrefix("/login").Subrouter()
    l.Methods("POST").HandlerFunc(loginHandler)
    r.HandleFunc("/logout", logoutHandler)

    r.PathPrefix("/css/style.css").HandlerFunc(css)
    r.PathPrefix("/js/script.js").HandlerFunc(js)

    s := r.PathPrefix("/search").Subrouter()
    s.Methods("GET").HandlerFunc(searchHandler)
    t := r.PathPrefix("/hashtag/{tag}").Subrouter()
    t.Methods("GET").HandlerFunc(searchHandler)

    n := r.PathPrefix("/unfollow").Subrouter()
    n.Methods("POST").HandlerFunc(unfollowHandler)
    f := r.PathPrefix("/follow").Subrouter()
    f.Methods("POST").HandlerFunc(followHandler)

    u := r.PathPrefix("/{user}").Subrouter()
    u.Methods("GET").HandlerFunc(userHandler)

    i := r.PathPrefix("/").Subrouter()
    i.Methods("GET").HandlerFunc(topHandler)
    i.Methods("POST").HandlerFunc(tweetPostHandler)

GET /

image

投稿が一覧できる。投稿もできる。もっと読むボタンが下にあり、投稿をもっと表示できる。 各ユーザーのページにフォロー・アンフォローボタンがある。フォローしている人の投稿が一覧できているっぽい。

GET /search

検索。めっちゃ重い。

image

catatsuy commented 7 years ago

GET /{user}

image

その人の投稿が見れる

GET /hashtag/{tag}

image

ハッシュタグが見れる。URLは違うが、検索と同じsearchHandlerが呼び出されているのでこちらを最適化すれば良い。

catatsuy commented 7 years ago

POST /follow POST /unfollow

別アプリケーション isutomo を叩いている。排除したい気持ちもあるが、ボトルネックになるまでは待つべき

catatsuy commented 7 years ago

loadFriends関数

isutomoを叩いている。jsonで返ってくる。フレンドのリストが返ってくる

curl http://localhost:8081/kazuto
{"friends":["kazuto","norihiko","tomio","hiroyuki","suguru","hiromitsu","atomu","masatomo","masato","ippei","sotaro","tadateru","nobuhiko","mitsunobu","hisatsugu","yoichi","masamori","mitsuhisa","kihei","ryotaro","akito","raizo","yasunari","shunichi","matsushige","yojiro","kiyosue","hidekazu","nobuhiro","shunji","tetsuharu","kiyohiro","munemori","kankuro","mitsumori","morihiko","shotaro","kazuro","suketoshi","hozumi","dai","reizo","shinya","akisada","nagatomo","seiichi","sadayoshi","hisayoshi","tadatsugu","koji","yoichiro","tadatoshi","taku","michio","takahide","eita","kiyoaki","takanori","hiroyoshi","kunitake","munetoki","toyokazu","teruyuki","mutsuo","tatsuhito","masahiro","masamura","toshio","toshinaga","akinobu","masayoshi","katsunari","takeyoshi","nagahide","soichiro","kaku","tomoya","sanji","taisuke","shinjo","mikio","taishi","teizo","norikazu","sadao","mitsuhira","taiichi","yasuyoshi","nobukatsu","shigefumi","masaya","kakichi","masanobu","tsunejiro","hayato","atsuya","tetsumasa","tadahito","sadaaki","daichi","kensuke","masahide","tateo","kazuaki","shusaku","ikko","kosei","yasuo","nobusada","tsutomu","kyohei","kenshin","toshimi","masakage","masabumi","shingo","takanobu","tetsuro","ryunosuke","shigenaga","haruto","takuji","masahisa","kaiji","hideyuki","shiryu","shigeki","yasuro","nagamichi","hideaki","ken","fumihiro","masatane","taiji","takauji","naohiro","masao","kento","kiyohide","tetsushi","masashige","mineo","takamitsu","mitsutaka","goro","yahiko","yoshi","munetoshi","seishiro","naokatsu","heihachiro","masachika","mitsuo","katsuteru","sakichi","tatsuhiro","taiki","michiharu","kenkichi","keiichi","toyoaki","kazunori","kiyokazu","yasuharu","tsuneyoshi","yasukazu","chikashi","takeharu","taizo","tamio","naganao","masaru","shoya","heisuke","jokichi","kunimitsu","mitsuyuki","tomohisa","kiyotake","tokujiro","torahiko","hideyo","seiki","matabei","kenichi","mitsuharu","okimoto","tadahiko","hirotami","norifusa","ryoichi","tatsuro","arinobu","tomotaka","takaaki","toshiyuki","ikki","akihiro","tatsumi","fumiaki","mitsuyoshi","yasuhide","kimio","tatsuyoshi","tsunenori","kagenori","yasunobu","hisao","yasufumi","taichi","jin","toyohiko","masatoshi","reiichi","mitsuomi","masamitsu","satoru","yasujiro","michiaki","daizo","kazushi","koki","naohiko","takafumi","mamoru","toshiya","naohisa","shigekazu","matsuki","sosuke","matsuo","takuya","junichi","hirokatsu","jushiro","hisamitsu","hironobu","katsutomo","asuka","yasuyuki","kuranosuke","katsunaga","motoki","shosuke","hirofumi","muneo","takehiko","tokuo","takuma","shuta","hidetsugu","nobutoshi","juzo","yasuaki","tadachika","katsuhito","tadanori","tadahiro","junki","akio","yanosuke","toshinari","takehito","ritsuo","toshimitsu","shogo","kunihiro","kunio","ikuro","takaya","yasutoshi","masanori","shinichi","tadatsune","michiro","ryuji","fumio","katsunosuke","terumasa","motoyuki","yoritaka","toshizo","tsunehisa","shunsuke","hisato","yasushi","munenobu","shichiro","mitsuteru","shohei","toshihide","sadatoshi","michihisa","yosuke","motoshige","naritaka","masakazu","keisuke","tokuro","koya","shuichi","sogen","ichiro","ryuta","shin","yasumasa","kohei","seizo","iesada","masatsugu","haruhiro","murashige","keizo","noriyuki","morio","motoharu","eiichi","katsushi","seishi","shozo","seijiro","katsutoshi","nobuyuki","ryusaku","einosuke","katsuei","michiya","arihiro","nobutsuna","kazuhiko","takayoshi","shigemitsu","hideto","masakatsu","shigeto","tsunemi","shiko","seiya","tadami","hidetoshi","motoyasu","ryoji","kiyonari","shigeyuki","giichi","tetsuo","mitsunori","munenori","hirotomo","shuji","morihiro","seikichi","kunimasa","toshiyasu","tatsuyuki","itaru","hisataka","taichiro","tatsuhiko","toshitada","ryuzo","takumi","jukichi","sho","yasuji","masuo","takao","takahiko","shigetada","naozumi","soji","genzo","shigehiro","michitaka","naofumi","yashiro","katsusuke","haruaki","tomohide","joji","mitsugu","teruyoshi","tomohito","kanemoto","umanosuke","kazuyuki","kaichi","morikazu","ryosuke","kazutoshi","kenyu","nobutoki","kazuhiro","jiichiro","mitsusuke","naoyuki","tomomichi","katsuji","kimiya","akishige","daishin","tsuneyuki","shinzo","kisho","kenjiro","kinichi","arata","munehiro","akitoshi","eishun","kaito","takuzo","takayuki","shinpei","naotake","hisanori","ryuki","yasuhiko","tomonori","junya","fusanosuke","hiroki","shunnosuke","osamu","mototsugu","tamotsu","bunji","toshimasa","kazuhito","hisamoto","haruhisa","masataka","ryutaro","norifumi","katsuhiro","yasutake","seiichiro","tetsuya","mineichi","naomori","tokio","kinichiro","katsuo","chikayoshi","kentaro","rikiya","nobuhisa","riichi","junichiro","hiroshige","masatake","shigetoshi","teruhisa","kuniaki","takejiro","kazuma","kuniyuki","kanetsugu","shigenobu","mitsuyo","gentaro","shigehisa","keijiro","shigeyoshi","masaki","keigo","haruo","takamori","shuko","takahito","mahiro","fumihiko","yasuhiro","kintaro","tokihiko","naomichi","nobuharu","hidemitsu","toyomatsu","kazuyoshi","akitsugu","masuzo","takeshi","isao","kosuke","atsushi","tomoyoshi","kunihiko","tatsushi","tomoyasu","tomoaki","takenori","iemasa","keishi","hidenobu","shunichiro","tadatomo","takeaki","junzo","junji","takuto"]}
catatsuy commented 7 years ago

topHandler関数

GET /を返すハンドラー。ページャーもあるのでGETクエリuntilで日付を受け取れる。

SELECT * FROM tweets ORDER BY created_at DESC
# untilがある場合
SELECT * FROM tweets WHERE created_at < ? ORDER BY created_at DESC

loadFriends関数で友達のリストを取得する。上のSQLで全件取ってきているので、友達のリストをfor文で回して友達かどうか判定、友達ならappendしていく。perPage = 50に達したら打ち切るので50件以上にはならない。

catatsuy commented 7 years ago

searchHandler関数

GET /search GET /hashtagを返すハンドラー。hashtagの方ならURLから、searchならGETクエリqからクエリ名を取得してくる。

topHandler同様、untilが取れる。基本的に全件を取得してきてから

if strings.Index(t.HTML, query) != -1 {
    tweets = append(tweets, &t)
}

で見てくる。perPage = 50に達したら打ち切るので50件以上にはならない。

catatsuy commented 7 years ago

userHandler

untilも対応している

SELECT * FROM tweets WHERE user_id = ? ORDER BY created_at DESC

これをappendしているだけ。perPage = 50に達したら打ち切るので50件以上にはならない。LIMITサクッと付けたい