Skip to content

Commit

Permalink
Rebased onto master to v3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 committed Nov 13, 2024
1 parent dfa2f8b commit 3985f4d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
9 changes: 5 additions & 4 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ func Restart(ctx context.Context) error {
}

// StartWith a local cluster with specific addresses
func StartWith(localPeers []gubernator.PeerInfo, opts ...option) error {
func StartWith(localPeers []gubernator.PeerInfo, opts ...Option) error {
for _, peer := range localPeers {
ctx, cancel := context.WithTimeout(context.Background(), clock.Second*10)
d, err := gubernator.SpawnDaemon(ctx, gubernator.DaemonConfig{
cfg := gubernator.DaemonConfig{
Logger: logrus.WithField("instance", peer.HTTPAddress),
InstanceID: peer.HTTPAddress,
HTTPListenAddress: peer.HTTPAddress,
Expand All @@ -189,6 +189,7 @@ func StartWith(localPeers []gubernator.PeerInfo, opts ...option) error {
BatchTimeout: clock.Second * 5,
},
}

for _, opt := range opts {
opt.Apply(&cfg)
}
Expand Down Expand Up @@ -219,7 +220,7 @@ func Stop(ctx context.Context) {
daemons = nil
}

type option interface {
type Option interface {
Apply(cfg *gubernator.DaemonConfig)
}

Expand All @@ -232,6 +233,6 @@ func (o *eventChannelOption) Apply(cfg *gubernator.DaemonConfig) {
}

// WithEventChannel sets EventChannel to Gubernator config.
func WithEventChannel(eventChannel chan<- gubernator.HitEvent) option {
func WithEventChannel(eventChannel chan<- gubernator.HitEvent) Option {
return &eventChannelOption{eventChannel: eventChannel}
}
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ type Config struct {
}

type HitEvent struct {
Request *RateLimitReq
Response *RateLimitResp
Request *RateLimitRequest
Response *RateLimitResponse
}

func (c *Config) SetDefaults() error {
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestDefaultListenAddress(t *testing.T) {
# a comment`
daemonConfig, err := SetupDaemonConfig(logrus.StandardLogger(), strings.NewReader(s))
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("%s:80", LocalHost()), daemonConfig.HTTPListenAddress)
require.Equal(t, fmt.Sprintf("%s:1050", LocalHost()), daemonConfig.HTTPListenAddress)
require.NotEmpty(t, daemonConfig.InstanceID)
}

Expand Down
3 changes: 2 additions & 1 deletion daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ func (d *Daemon) Start(ctx context.Context) error {
return NewPeerClient(WithPeerInfo(info))
},
CacheFactory: cacheFactory,
DataCenter: d.conf.DataCenter,
EventChannel: d.conf.EventChannel,
InstanceID: d.conf.InstanceID,
DataCenter: d.conf.DataCenter,
CacheSize: d.conf.CacheSize,
Behaviors: d.conf.Behaviors,
Workers: d.conf.Workers,
Expand Down
7 changes: 3 additions & 4 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,11 @@ func peer(ip string, self string, ipv6 bool) PeerInfo {
if ipv6 {
ip = "[" + ip + "]"
}
grpc := ip + ":1051"
addr := ip + ":1050"
return PeerInfo{
DataCenter: "",
HTTPAddress: ip + ":1050",
GRPCAddress: grpc,
IsOwner: grpc == self,
HTTPAddress: addr,
IsOwner: addr == self,
}

}
Expand Down
32 changes: 18 additions & 14 deletions functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2150,38 +2150,42 @@ func TestEventChannel(t *testing.T) {
}()

// Spawn specialized Gubernator cluster with EventChannel enabled.
cluster.Stop()
cluster.Stop(context.Background())
defer func() {
err := startGubernator()
require.NoError(t, err)
}()
peers := []guber.PeerInfo{
{GRPCAddress: "127.0.0.1:10000", HTTPAddress: "127.0.0.1:10001", DataCenter: cluster.DataCenterNone},
{GRPCAddress: "127.0.0.1:10002", HTTPAddress: "127.0.0.1:10003", DataCenter: cluster.DataCenterNone},
{GRPCAddress: "127.0.0.1:10004", HTTPAddress: "127.0.0.1:10005", DataCenter: cluster.DataCenterNone},
{HTTPAddress: "127.0.0.1:10001", DataCenter: cluster.DataCenterNone},
{HTTPAddress: "127.0.0.1:10002", DataCenter: cluster.DataCenterNone},
{HTTPAddress: "127.0.0.1:10003", DataCenter: cluster.DataCenterNone},
}
err := cluster.StartWith(peers, cluster.WithEventChannel(eventChannel))
require.NoError(t, err)
defer cluster.Stop()
defer cluster.Stop(context.Background())

addr := cluster.GetRandomPeerInfo(cluster.DataCenterNone).HTTPAddress
client, err := guber.NewClient(guber.WithNoTLS(addr))
require.Nil(t, err)

client, err := guber.DialV1Server(cluster.GetRandomPeer(cluster.DataCenterNone).GRPCAddress, nil)
require.NoError(t, err)
sendHit := func(key string, behavior guber.Behavior) {
ctx, cancel := context.WithTimeout(context.Background(), clock.Second*10)
defer cancel()
_, err = client.GetRateLimits(ctx, &guber.GetRateLimitsReq{
Requests: []*guber.RateLimitReq{

var resp guber.CheckRateLimitsResponse
err = client.CheckRateLimits(ctx, &guber.CheckRateLimitsRequest{
Requests: []*guber.RateLimitRequest{
{
Name: "test",
UniqueKey: key,
Algorithm: guber.Algorithm_TOKEN_BUCKET,
Behavior: behavior,
Duration: guber.Minute * 3,
Hits: 2,
Behavior: behavior,
Name: "test",
Limit: 1000,
UniqueKey: key,
Hits: 2,
},
},
})
}, &resp)
require.NoError(t, err)
select {
case <-sem:
Expand Down

0 comments on commit 3985f4d

Please sign in to comment.