mirror of
https://github.com/eosswedenorg/thalos
synced 2026-07-03 11:53:41 +02:00
api/redis/subscriber.go: Adding context as first parameter to NewSubscriber()
This commit is contained in:
parent
c1efffd4ba
commit
f9124c2864
4 changed files with 11 additions and 9 deletions
|
|
@ -27,10 +27,10 @@ func WithTimeout(value time.Duration) SubscriberOption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscriber(client *redis.Client, ns Namespace, options ...SubscriberOption) *Subscriber {
|
func NewSubscriber(ctx context.Context, client *redis.Client, ns Namespace, options ...SubscriberOption) *Subscriber {
|
||||||
sub := &Subscriber{
|
sub := &Subscriber{
|
||||||
ctx: client.Context(),
|
ctx: ctx,
|
||||||
sub: client.PSubscribe(client.Context()),
|
sub: client.PSubscribe(ctx),
|
||||||
channels: make(map[string]chan []byte),
|
channels: make(map[string]chan []byte),
|
||||||
timeout: time.Millisecond * 200,
|
timeout: time.Millisecond * 200,
|
||||||
ns: ns,
|
ns: ns,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package redis
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -16,14 +17,14 @@ func TestSubscriber_Construct(t *testing.T) {
|
||||||
client, _ := redismock.NewClientMock()
|
client, _ := redismock.NewClientMock()
|
||||||
ns := Namespace{Prefix: "prefix", ChainID: "8f2f6ec19400d372c9b3340b1438e9c805cf9e69be962fa81d055bc037ceed8d"}
|
ns := Namespace{Prefix: "prefix", ChainID: "8f2f6ec19400d372c9b3340b1438e9c805cf9e69be962fa81d055bc037ceed8d"}
|
||||||
|
|
||||||
s := NewSubscriber(client, ns)
|
s := NewSubscriber(context.Background(), client, ns)
|
||||||
|
|
||||||
assert.Equal(t, s.ctx, client.Context())
|
assert.Equal(t, s.ctx, context.Background())
|
||||||
assert.NotNil(t, s.sub)
|
assert.NotNil(t, s.sub)
|
||||||
assert.Equal(t, s.ns, ns)
|
assert.Equal(t, s.ns, ns)
|
||||||
assert.Equal(t, s.timeout, 200*time.Millisecond)
|
assert.Equal(t, s.timeout, 200*time.Millisecond)
|
||||||
|
|
||||||
s = NewSubscriber(client, ns, WithTimeout(4*time.Second))
|
s = NewSubscriber(context.Background(), client, ns, WithTimeout(4*time.Second))
|
||||||
assert.Equal(t, s.timeout, 4*time.Second)
|
assert.Equal(t, s.timeout, 4*time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,7 +37,7 @@ func TestSubscriber_Read(t *testing.T) {
|
||||||
Addr: server.Addr(),
|
Addr: server.Addr(),
|
||||||
})
|
})
|
||||||
|
|
||||||
s := NewSubscriber(client, Namespace{Prefix: "prefix", ChainID: "d41dbd2921d5a377325661427090c6c508904d60920d6b7ea771c58da5299754"})
|
s := NewSubscriber(context.Background(), client, Namespace{Prefix: "prefix", ChainID: "d41dbd2921d5a377325661427090c6c508904d60920d6b7ea771c58da5299754"})
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(time.Millisecond * 10)
|
time.Sleep(time.Millisecond * 10)
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ func main() {
|
||||||
"interval": interval,
|
"interval": interval,
|
||||||
}).Info("Starting benchmark")
|
}).Info("Starting benchmark")
|
||||||
|
|
||||||
sub := api_redis.NewSubscriber(rdb, api_redis.Namespace{
|
sub := api_redis.NewSubscriber(context.Background(), rdb, api_redis.Namespace{
|
||||||
Prefix: redis_prefix,
|
Prefix: redis_prefix,
|
||||||
ChainID: chain_id,
|
ChainID: chain_id,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
@ -17,7 +18,7 @@ func main() {
|
||||||
// Create redis client
|
// Create redis client
|
||||||
rdb := redis.NewClient(&redis.Options{})
|
rdb := redis.NewClient(&redis.Options{})
|
||||||
|
|
||||||
sub := api_redis.NewSubscriber(rdb, api_redis.Namespace{
|
sub := api_redis.NewSubscriber(context.Background(), rdb, api_redis.Namespace{
|
||||||
Prefix: "ship",
|
Prefix: "ship",
|
||||||
ChainID: "1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4", // Wax mainnet.
|
ChainID: "1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4", // Wax mainnet.
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue