Permalink
Browse files

Create banlist

  • Loading branch information...
nomeaning777 committed Oct 20, 2018
1 parent d26c4e6 commit d61f6103921b0e4397c42ee55f7701001539a622
Showing with 27 additions and 1 deletion.
  1. +27 −1 src/isucon8/isucoin/controller/handler.go
@@ -7,6 +7,7 @@ import (
"log"
"net/http"
"strconv"
"sync"
"time"

"isucon8/isucoin/model"
@@ -85,8 +86,23 @@ func (h *Handler) Signup(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
}
}

var banList map[string]int
var banMutex *sync.Mutex

func (h *Handler) Signin(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if banList == nil {
banList = make(map[string]int)
banMutex = &sync.Mutex{}
}
bankID := r.FormValue("bank_id")
banned := func () bool {
banMutex.Lock()
defer banMutex.Unlock()
return banList[bankID] >= 5
}()
if banned {
h.handleError(w, errors.New("error: too many failures"), 403)
}
password := r.FormValue("password")
if bankID == "" || password == "" {
h.handleError(w, errors.New("all parameters are required"), 400)
@@ -96,6 +112,11 @@ func (h *Handler) Signin(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
switch {
case err == model.ErrUserNotFound:
// TODO: 失敗が多いときに403を返すBanの仕様に対応
func () {
banMutex.Lock()
defer banMutex.Unlock()
banList[bankID]++
}()
h.handleError(w, err, 404)
case err != nil:
h.handleError(w, err, 500)
@@ -110,6 +131,11 @@ func (h *Handler) Signin(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
h.handleError(w, err, 500)
return
}
func () {
banMutex.Lock()
defer banMutex.Unlock()
banList[bankID] = 0
}()
h.handleSuccess(w, user)
}
}
@@ -170,6 +196,7 @@ func (h *Handler) Info(w http.ResponseWriter, r *http.Request, _ httprouter.Para
res["traded_orders"] = orders
}


bySecTime := BaseTime.Add(-300 * time.Second)
if lt.After(bySecTime) {
bySecTime = time.Date(lt.Year(), lt.Month(), lt.Day(), lt.Hour(), lt.Minute(), lt.Second(), 0, lt.Location())
@@ -221,7 +248,6 @@ func (h *Handler) Info(w http.ResponseWriter, r *http.Request, _ httprouter.Para
}
// TODO: trueにするとシェアボタンが有効になるが、アクセスが増えてヤバイので一旦falseにしておく
res["enable_share"] = false

h.handleSuccess(w, res)
}

0 comments on commit d61f610

Please sign in to comment.