Skip to content

Commit

Permalink
refactor: replace semaphore with trylock
Browse files Browse the repository at this point in the history
  • Loading branch information
iczc committed Oct 16, 2021
1 parent cbc9a0a commit ec79304
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module github.com/chainflag/eth-faucet
go 1.16

require (
github.com/LK4D4/trylock v0.0.0-20191027065348-ff7e133a5c54
github.com/ReneKroon/ttlcache/v2 v2.8.1
github.com/agiledragon/gomonkey/v2 v2.2.0
github.com/ethereum/go-ethereum v1.10.9
github.com/sirupsen/logrus v1.8.1
github.com/urfave/negroni v1.0.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbt
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/LK4D4/trylock v0.0.0-20191027065348-ff7e133a5c54 h1:sg9CWNOhr58hMGmJ0q7x7jQ/B1RK/GyHNmeaYCJos9M=
github.com/LK4D4/trylock v0.0.0-20191027065348-ff7e133a5c54/go.mod h1:uHbOgfPowb74TKlV4AR5Az2haG6evxzM8Lmj1Xil25E=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/ReneKroon/ttlcache/v2 v2.8.1 h1:0Exdyt5+vEsdRoFO1T7qDIYM3gq/ETbeYV+vjgcPxZk=
github.com/ReneKroon/ttlcache/v2 v2.8.1/go.mod h1:mBxvsNY+BT8qLLd6CuAJubbKo6r0jh3nb5et22bbfGY=
Expand Down
17 changes: 8 additions & 9 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strconv"
"time"

"github.com/LK4D4/trylock"
log "github.com/sirupsen/logrus"
"github.com/urfave/negroni"
"golang.org/x/sync/semaphore"

"github.com/chainflag/eth-faucet/internal/chain"
"github.com/chainflag/eth-faucet/web"
Expand All @@ -20,17 +20,16 @@ const AddressKey string = "address"

type Server struct {
chain.TxBuilder
mutex trylock.Mutex
cfg *Config
queue chan string
sem *semaphore.Weighted
}

func NewServer(builder chain.TxBuilder, cfg *Config) *Server {
return &Server{
TxBuilder: builder,
cfg: cfg,
queue: make(chan string, cfg.queueCap),
sem: semaphore.NewWeighted(1),
}
}

Expand Down Expand Up @@ -63,8 +62,8 @@ func (s *Server) consumeQueue() {
return
}

s.sem.Acquire(context.Background(), 1)
defer s.sem.Release(1)
s.mutex.Lock()
defer s.mutex.Unlock()
for len(s.queue) != 0 {
address := <-s.queue
txHash, err := s.Transfer(context.Background(), address, s.cfg.payout)
Expand All @@ -87,8 +86,8 @@ func (s *Server) handleClaim() http.HandlerFunc {
}

address := r.PostFormValue(AddressKey)
// The semaphore can be acquired only if the work queue is empty
if len(s.queue) != 0 || !s.sem.TryAcquire(1) {
// Try to lock mutex if the work queue is empty
if len(s.queue) != 0 || !s.mutex.TryLock() {
select {
case s.queue <- address:
log.WithFields(log.Fields{
Expand All @@ -103,10 +102,10 @@ func (s *Server) handleClaim() http.HandlerFunc {
return
}

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

0 comments on commit ec79304

Please sign in to comment.