Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

proposed solution to issue 1134 #1883

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion pkg/client/mediator/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package mediator
import (
"errors"
"fmt"
"time"

"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/mediator"
Expand Down Expand Up @@ -41,10 +42,41 @@ type protocolService interface {

// Config returns the router's configuration.
Config() (*mediator.Config, error)

// SetTimeout timeout value waiting for responses received from the router
SetTimeout(timeout time.Duration)
}

const (
updateTimeout = 5 * time.Second
)

// Option configures the route client and underlying service
type Option func(opts *mediatorOpts)

// mediatorOpts holds options for the router client
type mediatorOpts struct {
timeout time.Duration
}

// WithTimeout option is for definition timeout value waiting for responses received from the router
func WithTimeout(t time.Duration) Option {
return func(opts *mediatorOpts) {
opts.timeout = t
}
}

// New return new instance of route client.
func New(ctx provider) (*Client, error) {
func New(ctx provider, options ...Option) (*Client, error) {
opts := &mediatorOpts{}

defMediatorOpts(opts)

// generate router config from options
for _, option := range options {
option(opts)
}

svc, err := ctx.Service(mediator.Coordination)
if err != nil {
return nil, err
Expand All @@ -55,12 +87,19 @@ func New(ctx provider) (*Client, error) {
return nil, errors.New("cast service to route service failed")
}

routeSvc.SetTimeout(opts.timeout)

return &Client{
Event: routeSvc,
routeSvc: routeSvc,
}, nil
}

// defMediatorOpts provides default router options
func defMediatorOpts(opts *mediatorOpts) {
opts.timeout = updateTimeout
}

// Register the agent with the router(passed in connectionID). This function asks router's
// permission to publish it's endpoint and routing keys.
func (c *Client) Register(connectionID string) error {
Expand Down
16 changes: 16 additions & 0 deletions pkg/client/mediator/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ func TestNew(t *testing.T) {
require.NotNil(t, svc)
})

t.Run("test new client with all options applied", func(t *testing.T) {
i := 0
svc, err := New(&mockprovider.Provider{
ServiceValue: &mockroute.MockMediatorSvc{}},
func(opts *mediatorOpts) {
i += 1 // nolint
},
func(opts *mediatorOpts) {
i += 2
},
)
require.NoError(t, err)
require.NotNil(t, svc)
require.Equal(t, 1+2, i)
})

t.Run("test error from get service from context", func(t *testing.T) {
_, err := New(&mockprovider.Provider{ServiceErr: fmt.Errorf("service error")})
require.Error(t, err)
Expand Down
16 changes: 8 additions & 8 deletions pkg/didcomm/protocol/mediator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ const (
routeConfigDataKey = "route-config"
)

const (
updateTimeout = 5 * time.Second
)

// ErrConnectionNotFound connection not found error
var ErrConnectionNotFound = errors.New("connection not found")

Expand Down Expand Up @@ -128,6 +124,7 @@ type Service struct {
keylistUpdateMap map[string]chan *KeylistUpdateResponse
keylistUpdateMapLock sync.RWMutex
callbacks chan *callback
updateTimeout time.Duration
}

// New return route coordination service.
Expand Down Expand Up @@ -159,6 +156,11 @@ func New(prov provider) (*Service, error) {
return s, nil
}

// SetTimeout timeout value waiting for responses received from the router
func (s *Service) SetTimeout(t time.Duration) {
s.updateTimeout = t
}

func (s *Service) listenForCallbacks() {
for c := range s.callbacks {
logger.Debugf("handling user callback %+v with options %+v", c, c.options)
Expand Down Expand Up @@ -527,8 +529,7 @@ func (s *Service) doRegistration(record *connection.Record, req *Request) error
if err := s.saveRouterConfig(conf); err != nil {
return fmt.Errorf("save route config : %w", err)
}
// TODO https://github.com/hyperledger/aries-framework-go/issues/1134 configure this timeout at decorator level
case <-time.After(updateTimeout):
case <-time.After(s.updateTimeout):
return errors.New("timeout waiting for grant from the router")
}

Expand Down Expand Up @@ -617,8 +618,7 @@ func (s *Service) AddKey(recKey string) error {
if err := processKeylistUpdateResp(recKey, keyUpdateResp); err != nil {
return err
}
// TODO https://github.com/hyperledger/aries-framework-go/issues/1134 configure this timeout at decorator level
case <-time.After(updateTimeout):
case <-time.After(s.updateTimeout):
return errors.New("timeout waiting for keylist update response from the router")
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/mock/didcomm/protocol/mediator/mock_mediator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0
package mediator

import (
"time"

"github.com/google/uuid"

"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
Expand All @@ -30,6 +32,7 @@ type MockMediatorSvc struct {
ConnectionID string
GetConnectionIDErr error
AddKeyFunc func(string) error
SetTimeoutFunc func(t time.Duration)
}

// HandleInbound msg
Expand Down Expand Up @@ -117,3 +120,10 @@ func (m *MockMediatorSvc) GetConnection() (string, error) {

return m.ConnectionID, nil
}

// SetTimeout timeout value waiting for responses received from the router
func (m *MockMediatorSvc) SetTimeout(t time.Duration) {
if m.SetTimeoutFunc != nil {
m.SetTimeoutFunc(t)
}
}