Skip to content

Commit

Permalink
Fix Faucet (#4624)
Browse files Browse the repository at this point in the history
* fix faucet

* preston's review
  • Loading branch information
nisdas committed Jan 23, 2020
1 parent 4602502 commit ee9b9e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions tools/faucet/main.go
Expand Up @@ -39,6 +39,7 @@ func main() {
)

reflection.Register(s)
go counterWatcher()

fmt.Printf("Serving gRPC requests on port %d\n", *port)
if err := s.Serve(lis); err != nil {
Expand Down
19 changes: 18 additions & 1 deletion tools/faucet/server.go
Expand Up @@ -22,12 +22,13 @@ import (
"google.golang.org/grpc/metadata"
)

const ipLimit = 3
const ipLimit = 5

var fundingAmount = big.NewInt(3.5 * params.Ether)
var funded = make(map[string]bool)
var ipCounter = make(map[string]int)
var fundingLock sync.Mutex
var pruneDuration = time.Hour * 4

type faucetServer struct {
r recaptcha.Recaptcha
Expand Down Expand Up @@ -178,3 +179,19 @@ func (s *faucetServer) getPeer(ctx context.Context) (string, error) {
peer := md.Get("x-forwarded-for")[0]
return peer, nil
}

// reduce the counter for each ip every few hours.
func counterWatcher() {
ticker := time.NewTicker(pruneDuration)
for {
<-ticker.C
fundingLock.Lock()
for ip, ctr := range ipCounter {
if ctr == 0 {
continue
}
ipCounter[ip] = ctr - 1
}
fundingLock.Unlock()
}
}

0 comments on commit ee9b9e6

Please sign in to comment.