Skip to content

Commit

Permalink
refactor: removed networkChannels from client, added channel into node
Browse files Browse the repository at this point in the history
  • Loading branch information
andrix10 authored and janaakhterov committed Nov 9, 2020
1 parent c5f8be1 commit dae427a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 12 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ type Client struct {

operator *operator

networkChannels map[AccountID]*channel
networkNodeIds []node
network map[AccountID]node
networkNodeIds []node
network map[AccountID]node

mirrorNetwork mirrorNetwork

Expand Down Expand Up @@ -112,7 +111,6 @@ func newClient(network map[string]AccountID, mirrorNetwork []string) *Client {
client := Client{
maxQueryPayment: defaultMaxQueryPayment,
maxTransactionFee: defaultMaxTransactionFee,
networkChannels: make(map[AccountID]*channel),
networkNodeIds: make([]node, 0),
network: newNetwork,
mirrorNetwork: newMirrorNetwork(mirrorNetwork),
Expand Down Expand Up @@ -209,9 +207,9 @@ func ClientFromConfigFile(filename string) (*Client, error) {

// Close is used to disconnect the Client from the network
func (client *Client) Close() error {
for _, conn := range client.networkChannels {
if conn != nil {
err := conn.client.Close()
for _, conn := range client.networkNodeIds {
if conn.channel != nil {
err := conn.channel.client.Close()
if err != nil {
return err
}
Expand Down Expand Up @@ -324,8 +322,12 @@ func (client *Client) getNumberOfNodesForTransaction() int {
}

func (client *Client) getChannel(id AccountID) (*channel, error) {
if client.networkChannels[id] != nil {
return client.networkChannels[id], nil
var i int
var node node
for i, node = range client.networkNodeIds {
if node.accountID == id && node.channel != nil {
return client.networkNodeIds[i].channel, nil
}
}

conn, err := grpc.Dial(client.network[id].address, grpc.WithInsecure())
Expand All @@ -334,7 +336,7 @@ func (client *Client) getChannel(id AccountID) (*channel, error) {
}

ch := newChannel(conn)
client.networkChannels[id] = &ch
client.networkNodeIds[i].channel = &ch
return &ch, nil
}

Expand Down
2 changes: 2 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type node struct {
address string
delay int64
lastUsed *int64
channel *channel
}

type nodes struct {
Expand All @@ -23,6 +24,7 @@ func newNode(accountID AccountID, address string) node {
address: address,
delay: 250,
lastUsed: nil,
channel: nil,
}
}

Expand Down

0 comments on commit dae427a

Please sign in to comment.