[#69] Add close for nns.Dial #134

Merged
alexvanin merged 1 commits from mbiryukova/frostfs-sdk-go:feature/add_nns_close into master 2023-07-31 10:26:09 +00:00
1 changed files with 23 additions and 16 deletions

View File

@ -19,12 +19,25 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
) )
// multiSchemeClient unites invoker.RPCInvoke and common interface of
// rpcclient.Client and rpcclient.WSClient.
type multiSchemeClient interface {
invoker.RPCInvoke
// Init turns client to "ready-to-work" state.
Init() error
// Close closes connections.
Close()
// GetContractStateByID returns state of the NNS contract on 1 input.
GetContractStateByID(int32) (*state.Contract, error)
}
// NNS looks up FrostFS names using Neo Name Service. // NNS looks up FrostFS names using Neo Name Service.
// //
// Instances are created with a variable declaration. Before work, the connection // Instances are created with a variable declaration. Before work, the connection
// to the NNS server MUST be established using Dial method. // to the NNS server MUST be established using Dial method.
type NNS struct { type NNS struct {
nnsContract util.Uint160 nnsContract util.Uint160
client multiSchemeClient
invoker interface { invoker interface {
Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error)
@ -37,47 +50,41 @@ type NNS struct {
// If URL address scheme is 'ws' or 'wss', then WebSocket protocol is used, // If URL address scheme is 'ws' or 'wss', then WebSocket protocol is used,
// otherwise HTTP. // otherwise HTTP.
func (n *NNS) Dial(address string) error { func (n *NNS) Dial(address string) error {
// multiSchemeClient unites invoker.RPCInvoke and common interface of
// rpcclient.Client and rpcclient.WSClient. Interface is anonymous
// according to assumption that common interface of these client types
// is not required by design and may diverge with changes.
var multiSchemeClient interface {
invoker.RPCInvoke
// Init turns client to "ready-to-work" state.
Init() error
// GetContractStateByID returns state of the NNS contract on 1 input.
GetContractStateByID(int32) (*state.Contract, error)
}
var err error var err error
uri, err := url.Parse(address) uri, err := url.Parse(address)
if err == nil && (uri.Scheme == "ws" || uri.Scheme == "wss") { if err == nil && (uri.Scheme == "ws" || uri.Scheme == "wss") {
multiSchemeClient, err = rpcclient.NewWS(context.Background(), address, rpcclient.WSOptions{}) n.client, err = rpcclient.NewWS(context.Background(), address, rpcclient.WSOptions{})
if err != nil { if err != nil {
return fmt.Errorf("create Neo WebSocket client: %w", err) return fmt.Errorf("create Neo WebSocket client: %w", err)
} }
} else { } else {
multiSchemeClient, err = rpcclient.New(context.Background(), address, rpcclient.Options{}) n.client, err = rpcclient.New(context.Background(), address, rpcclient.Options{})
if err != nil { if err != nil {
return fmt.Errorf("create Neo HTTP client: %w", err) return fmt.Errorf("create Neo HTTP client: %w", err)
} }
} }
if err = multiSchemeClient.Init(); err != nil { if err = n.client.Init(); err != nil {
return fmt.Errorf("initialize Neo client: %w", err) return fmt.Errorf("initialize Neo client: %w", err)
} }
nnsContract, err := multiSchemeClient.GetContractStateByID(1) nnsContract, err := n.client.GetContractStateByID(1)
if err != nil { if err != nil {
return fmt.Errorf("get NNS contract state: %w", err) return fmt.Errorf("get NNS contract state: %w", err)
} }
n.invoker = invoker.New(multiSchemeClient, nil) n.invoker = invoker.New(n.client, nil)
n.nnsContract = nnsContract.Hash n.nnsContract = nnsContract.Hash
return nil return nil
} }
// Close closes connections of multiSchemeClient.
func (n *NNS) Close() {
n.client.Close()
}
// ResolveContainerDomain looks up for NNS TXT records for the given container domain // ResolveContainerDomain looks up for NNS TXT records for the given container domain
// by calling `resolve` method of NNS contract. Returns the first record which represents // by calling `resolve` method of NNS contract. Returns the first record which represents
// valid container ID in a string format. Otherwise, returns an error. // valid container ID in a string format. Otherwise, returns an error.