2020-07-29 11:29:03 +00:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math"
|
|
|
|
|
2020-10-23 00:12:37 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2020-07-29 11:29:03 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
// SessionToken returns session token for connection
|
|
|
|
func (p *pool) Token(ctx context.Context, conn *grpc.ClientConn) (*token.SessionToken, error) {
|
2020-07-29 11:29:03 +00:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
if tkn, ok := p.tokens[conn.Target()]; ok && tkn != nil {
|
2020-07-29 11:29:03 +00:00
|
|
|
return tkn, nil
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
// prepare session token
|
2020-10-23 00:12:37 +00:00
|
|
|
tkn, err := p.prepareToken(ctx, conn)
|
2020-07-29 11:29:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
// save token for current connection
|
|
|
|
p.tokens[conn.Target()] = tkn
|
2020-07-29 11:29:03 +00:00
|
|
|
|
2020-10-13 09:32:23 +00:00
|
|
|
return tkn, nil
|
2020-07-29 11:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// creates token using
|
2020-10-23 00:12:37 +00:00
|
|
|
func (p *pool) prepareToken(ctx context.Context, conn *grpc.ClientConn) (*token.SessionToken, error) {
|
|
|
|
cli, err := client.New(p.key, client.WithGRPCConnection(conn))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tkn, err := cli.CreateSession(ctx, math.MaxUint64)
|
2020-07-29 11:29:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:12:37 +00:00
|
|
|
p.log.Info("token created for connection",
|
|
|
|
zap.String("address", conn.Target()),
|
|
|
|
zap.Stringer("owner", tkn.OwnerID()))
|
|
|
|
|
|
|
|
return tkn, err
|
2020-07-29 11:29:03 +00:00
|
|
|
}
|