Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
improve code.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed May 22, 2017
1 parent 79d378d commit 7d70b35
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
sudo: false
language: go
go:
- 1.7
- 1.8
- 1.8.1
services:
- redis-server
before_install:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016 Teambition
Copyright (c) 2016-2017 Teambition

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Gear-Ratelimiter
=====
# Gear-Ratelimiter

Smart rate limiter middleware for Gear, base on redis or memory limiter.

[![Build Status](http://img.shields.io/travis/teambition/gear-ratelimiter.svg?style=flat-square)](https://travis-ci.org/teambition/gear-ratelimiter)
Expand All @@ -23,7 +23,7 @@ import "github.com/teambition/gear-ratelimiter"
import (
"github.com/teambition/gear-ratelimiter"
redisClient "github.com/teambition/gear-ratelimiter/redis"
redis "gopkg.in/redis.v5"
redis "gopkg.in/redis.v5"
)

limiter := ratelimiter.New(&ratelimiter.Options{
Expand Down Expand Up @@ -65,5 +65,6 @@ go run example/main.go
```

## License

Gear-Ratelimiter is licensed under the [MIT](https://github.com/teambition/gear-ratelimiter/blob/master/LICENSE) license.
Copyright © 2016 [Teambition](https://www.teambition.com).
Copyright © 2016-2017 [Teambition](https://www.teambition.com).
7 changes: 4 additions & 3 deletions ratelimiter.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package ratelimiter

import (
"fmt"
"strconv"
"time"

"github.com/teambition/gear"
baselimiter "github.com/teambition/ratelimiter-go"
)

// Version is ratelimiter's version
const Version = "1.0.1"

// Options for Limiter
type Options struct {
// Key prefix, default is "LIMIT:".
Expand Down Expand Up @@ -71,8 +73,7 @@ func (l *RateLimiter) Serve(ctx *gear.Context) error {
if res.Remaining < 0 {
after := int(res.Reset.Sub(time.Now()).Seconds())
ctx.Set("Retry-After", strconv.Itoa(after))
return ctx.Error(&gear.Error{Code: 429,
Msg: fmt.Sprintf("Rate limit exceeded, retry in %d seconds.\n", after)})
return gear.ErrTooManyRequests.WithMsgf("Rate limit exceeded, retry in %d seconds.", after)
}
return nil
}
Expand Down

0 comments on commit 7d70b35

Please sign in to comment.