Skip to content

Commit

Permalink
refactor(rpc): improve consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Jan 4, 2021
1 parent ec66c50 commit de6ce25
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions internal/rpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"github.com/gotd/td/clock"
)

// Config of rpc engine.
type Config struct {
// Options of rpc engine.
type Options struct {
RetryInterval time.Duration
MaxRetries int
Logger *zap.Logger
Clock clock.Clock
}

func (cfg *Config) setDefaults() {
func (cfg *Options) setDefaults() {
if cfg.RetryInterval == 0 {
cfg.RetryInterval = time.Second * 10
}
Expand Down
5 changes: 3 additions & 2 deletions internal/rpc/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"golang.org/x/xerrors"

"github.com/gotd/td/bin"
"github.com/gotd/td/clock"
)

// Engine handles RPC requests.
Expand All @@ -21,7 +22,7 @@ type Engine struct {
rpc map[int64]func(*bin.Buffer, error) error
ack map[int64]func()

clock Clock
clock clock.Clock
log *zap.Logger
retryInterval time.Duration
maxRetries int
Expand All @@ -37,7 +38,7 @@ type Send func(ctx context.Context, msgID int64, seqNo int32, in bin.Encoder) er
func NopSend(ctx context.Context, msgID int64, seqNo int32, in bin.Encoder) error { return nil }

// New creates new rpc Engine.
func New(send Send, cfg Config) *Engine {
func New(send Send, cfg Options) *Engine {
cfg.setDefaults()

cfg.Logger.Info("Initialized",
Expand Down
12 changes: 6 additions & 6 deletions internal/rpc/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestRPCError(t *testing.T) {
return nil
}

runTest(t, Config{
runTest(t, Options{
RetryInterval: time.Second * 3,
MaxRetries: 2,
Clock: clock,
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestRPCResult(t *testing.T) {
return nil
}

runTest(t, Config{
runTest(t, Options{
RetryInterval: time.Second * 4,
MaxRetries: 2,
Clock: clock,
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestRPCAckThenResult(t *testing.T) {
return nil
}

runTest(t, Config{
runTest(t, Options{
RetryInterval: time.Second * 4,
MaxRetries: 2,
Clock: clock,
Expand Down Expand Up @@ -285,7 +285,7 @@ func TestRPCWithRetryResult(t *testing.T) {
return nil
}

runTest(t, Config{
runTest(t, Options{
RetryInterval: time.Second * 4,
MaxRetries: 5,
Clock: clock,
Expand Down Expand Up @@ -357,7 +357,7 @@ func TestEngineGracefulShutdown(t *testing.T) {
return nil
}

runTest(t, Config{
runTest(t, Options{
RetryInterval: time.Second * 5,
MaxRetries: 5,
Logger: log.Named("rpc"),
Expand All @@ -366,7 +366,7 @@ func TestEngineGracefulShutdown(t *testing.T) {

func runTest(
t *testing.T,
cfg Config,
cfg Options,
server func(t *testing.T, e *Engine, incoming <-chan request) error,
client func(t *testing.T, e *Engine) error,
) {
Expand Down
2 changes: 1 addition & 1 deletion mtproto/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func NewConn(addr string, opt Options) *Conn {
authKey: opt.Key,
salt: opt.Salt,
}
conn.rpc = rpc.New(conn.write, rpc.Config{
conn.rpc = rpc.New(conn.write, rpc.Options{
Logger: opt.Logger.Named("rpc"),
RetryInterval: opt.RetryInterval,
MaxRetries: opt.MaxRetries,
Expand Down
2 changes: 1 addition & 1 deletion mtproto/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func newTestClient(h testHandler) *Conn {
return engine.NotifyResult(msgID, &b)
}
return nil
}, rpc.Config{})
}, rpc.Options{})

client := &Conn{
rpc: engine,
Expand Down
2 changes: 1 addition & 1 deletion mtproto/handle_message_fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (Zero) Read(p []byte) (n int, err error) { return len(p), nil }
func FuzzHandleMessage(data []byte) int {
c := &Conn{
rand: Zero{},
rpc: rpc.New(rpc.NopSend, rpc.Config{}),
rpc: rpc.New(rpc.NopSend, rpc.Options{}),
log: zap.NewNop(),
messageID: proto.NewMessageIDGen(time.Now, 1),
}
Expand Down
2 changes: 1 addition & 1 deletion mtproto/handle_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestClientHandleMessageCorpus(t *testing.T) {
c := &Conn{
rand: Zero{},
log: zap.NewNop(),
rpc: rpc.New(rpc.NopSend, rpc.Config{}),
rpc: rpc.New(rpc.NopSend, rpc.Options{}),
}

corpus, err := ioutil.ReadDir(filepath.Join("..", "_fuzz", "handle_message", "corpus"))
Expand Down
2 changes: 1 addition & 1 deletion telegram/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func newTestClient(h testHandler) *Client {
return engine.NotifyResult(msgID, &b)
}
return nil
}, rpc.Config{})
}, rpc.Options{})

client := &Client{
log: zap.NewNop(),
Expand Down

0 comments on commit de6ce25

Please sign in to comment.