mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-06-27 21:05:53 +00:00
Moved rate.Limiter into its own package
This commit is contained in:
parent
7be7fc5c3a
commit
1da392f5b3
7 changed files with 22 additions and 23 deletions
|
@ -119,7 +119,6 @@ func commandLineConsole() {
|
|||
if cl.Send(msg) {
|
||||
kickCount++
|
||||
}
|
||||
kickCount++
|
||||
}
|
||||
return fmt.Sprintf("Kicked %d clients", kickCount), nil
|
||||
})
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package server // import "bitbucket.org/stendec/frankerfacez/socketserver/server"
|
||||
package server // import "github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server/rate"
|
||||
)
|
||||
|
||||
// LastSavedMessage contains a reply to a command along with an expiration time.
|
||||
|
@ -135,7 +136,7 @@ func HTTPBackendDropBacklog(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func rateLimitFromRequest(r *http.Request) (RateLimit, error) {
|
||||
func rateLimitFromRequest(r *http.Request) (rate.Limiter, error) {
|
||||
if r.FormValue("rateCount") != "" {
|
||||
c, err := strconv.ParseInt(r.FormValue("rateCount"), 10, 32)
|
||||
if err != nil {
|
||||
|
@ -145,9 +146,9 @@ func rateLimitFromRequest(r *http.Request) (RateLimit, error) {
|
|||
if err != nil {
|
||||
return nil, errors.Wrap(err, "rateTime")
|
||||
}
|
||||
return NewRateLimit(int(c), d), nil
|
||||
return rate.NewRateLimit(int(c), d), nil
|
||||
}
|
||||
return Unlimited(), nil
|
||||
return rate.Unlimited(), nil
|
||||
}
|
||||
|
||||
// HTTPBackendCachedPublish handles the /cached_pub route.
|
||||
|
@ -213,7 +214,7 @@ func HTTPBackendCachedPublish(w http.ResponseWriter, r *http.Request) {
|
|||
close(ch)
|
||||
}()
|
||||
select {
|
||||
case time.After(3 * time.Second):
|
||||
case <-time.After(3 * time.Second):
|
||||
count = -1
|
||||
case <-ch:
|
||||
}
|
||||
|
@ -278,7 +279,7 @@ func HTTPBackendUncachedPublish(w http.ResponseWriter, r *http.Request) {
|
|||
close(ch)
|
||||
}()
|
||||
select {
|
||||
case time.After(3 * time.Second):
|
||||
case <-time.After(3 * time.Second):
|
||||
count = -1
|
||||
case <-ch:
|
||||
}
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
package server
|
||||
package rate
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A RateLimit supports a constant number of Performed() calls every
|
||||
// time a given unit of time passes.
|
||||
// A Limiter supports a constant number of Performed() calls every
|
||||
// time a certain amount of time passes.
|
||||
//
|
||||
// Calls to Performed() when no "action tokens" are available will block
|
||||
// until one is available.
|
||||
type RateLimit interface {
|
||||
type Limiter interface {
|
||||
// Run begins emitting tokens for the ratelimiter.
|
||||
// A call to Run must be followed by a call to Close.
|
||||
Run()
|
||||
|
@ -29,8 +29,8 @@ type timeRateLimit struct {
|
|||
done chan struct{}
|
||||
}
|
||||
|
||||
// Construct a new RateLimit with the given count and duration.
|
||||
func NewRateLimit(count int, period time.Duration) RateLimit {
|
||||
// Construct a new Limiter with the given count and duration.
|
||||
func NewRateLimit(count int, period time.Duration) Limiter {
|
||||
return &timeRateLimit{
|
||||
count: count,
|
||||
period: period,
|
||||
|
@ -67,8 +67,8 @@ type unlimited struct{}
|
|||
|
||||
var unlimitedInstance unlimited
|
||||
|
||||
// Unlimited returns a RateLimit that never blocks. The Run() and Close() calls are no-ops.
|
||||
func Unlimited() RateLimit {
|
||||
// Unlimited returns a Limiter that never blocks. The Run() and Close() calls are no-ops.
|
||||
func Unlimited() Limiter {
|
||||
return unlimitedInstance
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package server
|
||||
package rate
|
||||
|
||||
import (
|
||||
"testing"
|
|
@ -1,12 +1,11 @@
|
|||
package server
|
||||
|
||||
// This is the scariest code I've written yet for the server.
|
||||
// If I screwed up the locking, I won't know until it's too late.
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/FrankerFaceZ/FrankerFaceZ/socketserver/server/rate"
|
||||
)
|
||||
|
||||
type SubscriberList struct {
|
||||
|
@ -60,7 +59,7 @@ func SubscribeGlobal(client *ClientInfo) {
|
|||
GlobalSubscriptionLock.Unlock()
|
||||
}
|
||||
|
||||
func PublishToChannel(channel string, msg ClientMessage, rl RateLimit) (count int) {
|
||||
func PublishToChannel(channel string, msg ClientMessage, rl rate.Limiter) (count int) {
|
||||
var found []*ClientInfo
|
||||
|
||||
ChatSubscriptionLock.RLock()
|
||||
|
@ -82,7 +81,7 @@ func PublishToChannel(channel string, msg ClientMessage, rl RateLimit) (count in
|
|||
return
|
||||
}
|
||||
|
||||
func PublishToMultiple(channels []string, msg ClientMessage, rl RateLimit) (count int) {
|
||||
func PublishToMultiple(channels []string, msg ClientMessage, rl rate.Limiter) (count int) {
|
||||
var found []*ClientInfo
|
||||
|
||||
ChatSubscriptionLock.RLock()
|
||||
|
@ -107,7 +106,7 @@ func PublishToMultiple(channels []string, msg ClientMessage, rl RateLimit) (coun
|
|||
return count
|
||||
}
|
||||
|
||||
func PublishToAll(msg ClientMessage, rl RateLimit) (count int) {
|
||||
func PublishToAll(msg ClientMessage, rl rate.Limiter) (count int) {
|
||||
var found []*ClientInfo
|
||||
|
||||
GlobalSubscriptionLock.RLock()
|
||||
|
|
|
@ -108,7 +108,7 @@ type ClientInfo struct {
|
|||
// True if the client has already sent the 'ready' command
|
||||
ReadyComplete bool
|
||||
|
||||
// Server-initiated messages should be sent via the Send() method
|
||||
// Server-initiated messages should be sent via the Send() method.
|
||||
MessageChannel chan<- ClientMessage
|
||||
|
||||
// Closed when the client is shutting down.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue