cmd/faucet: protect f.reqs with Rlock to prevent data race (#20669)

* cmd/faucet: add Rlock to protect f.reqs in apiHandler

* cmd/faucet: make a locked copy of f.reqs
This commit is contained in:
Boqin Qin 2020-02-16 02:14:29 +08:00 committed by GitHub
parent 855690523a
commit 37531b1884
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -360,11 +360,14 @@ func (f *faucet) apiHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
// Send over the initial stats and the latest header // Send over the initial stats and the latest header
f.lock.RLock()
reqs := f.reqs
f.lock.RUnlock()
if err = send(conn, map[string]interface{}{ if err = send(conn, map[string]interface{}{
"funds": new(big.Int).Div(balance, ether), "funds": new(big.Int).Div(balance, ether),
"funded": nonce, "funded": nonce,
"peers": f.stack.Server().PeerCount(), "peers": f.stack.Server().PeerCount(),
"requests": f.reqs, "requests": reqs,
}, 3*time.Second); err != nil { }, 3*time.Second); err != nil {
log.Warn("Failed to send initial stats to client", "err", err) log.Warn("Failed to send initial stats to client", "err", err)
return return