Skip to content

Commit

Permalink
refactor: move timeout context used for transfer to handler
Browse files Browse the repository at this point in the history
  • Loading branch information
iczc committed Sep 16, 2021
1 parent ff28ddd commit 2b2beba
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
6 changes: 1 addition & 5 deletions internal/server/limiter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package server

import (
"context"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -50,10 +49,7 @@ func (l *Limiter) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.Ha
l.cache.SetWithTTL(clintIP, true, l.ttl)
l.mutex.Unlock()

ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
defer cancel()

next.ServeHTTP(w, r.WithContext(ctx))
next.ServeHTTP(w, r)
if w.(negroni.ResponseWriter).Status() != http.StatusOK {
l.cache.Remove(address)
l.cache.Remove(clintIP)
Expand Down
4 changes: 3 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ func (s *Server) handleClaim() http.HandlerFunc {
return
}

txHash, err := s.Transfer(r.Context(), address, s.cfg.payout)
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
defer cancel()
txHash, err := s.Transfer(ctx, address, s.cfg.payout)
s.sem.Release(1)
if err != nil {
log.WithError(err).Error("Failed to send transaction")
Expand Down

0 comments on commit 2b2beba

Please sign in to comment.