github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/discovery/registry.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package discovery
    19  
    20  import (
    21  	"github.com/mysteriumnetwork/node/identity"
    22  	"github.com/mysteriumnetwork/node/market"
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  // ProposalRegistry defines methods for proposal lifecycle - registration, keeping up to date, removal
    27  type ProposalRegistry interface {
    28  	RegisterProposal(proposal market.ServiceProposal, signer identity.Signer) error
    29  	PingProposal(proposal market.ServiceProposal, signer identity.Signer) error
    30  	UnregisterProposal(proposal market.ServiceProposal, signer identity.Signer) error
    31  }
    32  
    33  type registryComposite struct {
    34  	registries []ProposalRegistry
    35  }
    36  
    37  // NewRegistry creates an instance of composite registry
    38  func NewRegistry(registries ...ProposalRegistry) *registryComposite {
    39  	return &registryComposite{registries: registries}
    40  }
    41  
    42  // AddRegistry adds registry to set of registries
    43  func (rc *registryComposite) AddRegistry(registry ProposalRegistry) {
    44  	rc.registries = append(rc.registries, registry)
    45  }
    46  
    47  // RegisterProposal registers service proposal to discovery service
    48  func (rc *registryComposite) RegisterProposal(proposal market.ServiceProposal, signer identity.Signer) error {
    49  	for _, registry := range rc.registries {
    50  		if err := registry.RegisterProposal(proposal, signer); err != nil {
    51  			return errors.Wrapf(err, "failed to register proposal: %v", proposal)
    52  
    53  		}
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  // UnregisterProposal unregisters a service proposal when client disconnects
    60  func (rc *registryComposite) UnregisterProposal(proposal market.ServiceProposal, signer identity.Signer) error {
    61  	for _, registry := range rc.registries {
    62  		if err := registry.UnregisterProposal(proposal, signer); err != nil {
    63  			return errors.Wrapf(err, "failed to unregister proposal: %v", proposal)
    64  		}
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  // PingProposal pings service proposal as being alive
    71  func (rc *registryComposite) PingProposal(proposal market.ServiceProposal, signer identity.Signer) error {
    72  	for _, registry := range rc.registries {
    73  		if err := registry.PingProposal(proposal, signer); err != nil {
    74  			return errors.Wrapf(err, "failed to ping proposal: %v", proposal)
    75  		}
    76  	}
    77  
    78  	return nil
    79  }