<commit_before>package node

import (
	"context"
	"fmt"

	"google.golang.org/grpc/codes"

	"google.golang.org/grpc"

	"github.com/n0stack/n0stack/n0proto.go/pool/v0"
	"github.com/pkg/errors"
)

// TODO: APIを叩く回数を減らす
type NodeConnections struct {
	NodeAPI ppool.NodeServiceClient
}

// func NewNodeConnections(api string) (*NodeConnections, error) {
// 	conn, err := grpc.Dial(api, grpc.WithInsecure())
// 	if err != nil {
// 		return nil, fmt.Errorf("Fail to dial to node, err:%v", err.Error())
// 	}

// 	nc := &NodeConnections{
// 		NodeAPI: ppool.NewNodeServiceClient(conn),
// 	}

// 	return nc, nil
// }

func (nc NodeConnections) IsExisting(nodeName string) (bool, error) {
	_, err := nc.NodeAPI.GetNode(context.Background(), &ppool.GetNodeRequest{Name: nodeName})
	if err != nil {
		if grpc.Code(err) == codes.NotFound {
			return false, nil
		}

		return false, errors.Wrap(err, "Failed to get node from API")
	}

	return true, nil
}

// GetConnection return a connection to Node having name of arguments.
func (nc NodeConnections) GetConnection(nodeName string) (*grpc.ClientConn, error) {
	n, err := nc.NodeAPI.GetNode(context.Background(), &ppool.GetNodeRequest{Name: nodeName})
	if err != nil {
		return nil, errors.Wrap(err, "Failed to get node from API")
	}

	if n.State == ppool.Node_NotReady {
		return nil, nil
	}

	// port を何かから取れるようにする
	conn, err := grpc.Dial(fmt.Sprintf("%s:%d", n.Address, 20181), grpc.WithInsecure())
	if err != nil {
		return nil, errors.Wrap(err, "Fail to dial to node")
	}

	return conn, nil
}
<commit_msg>update node/connection.go<commit_after>package node

import (
	"context"
	"fmt"

	"google.golang.org/grpc"

	"github.com/n0stack/n0stack/n0proto.go/pool/v0"
	"github.com/pkg/errors"
)

{'fraction_non_alphanumeric': 0.15300230946882218, 'fraction_numerical': 0.007505773672055427, 'mean_word_length': 3.9232954545454546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1544414', 'n_tokens_mistral': 693, 'n_tokens_neox': 661, 'n_words': 147}