*: fix all comment-related golint warnings

Some of this code is going to be moved to SDK library, so it's important.

Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2021-05-13 15:22:03 +03:00 committed by Stanislav Bogatyrev
parent 3173c70eb6
commit df3c87af79
14 changed files with 88 additions and 4 deletions

4
app.go
View file

@ -27,14 +27,17 @@ type (
webDone chan struct{} webDone chan struct{}
} }
// App is an interface for the main gateway function.
App interface { App interface {
Wait() Wait()
Serve(context.Context) Serve(context.Context)
} }
// Option is an application option.
Option func(a *app) Option func(a *app)
) )
// WithLogger returns Option to set a specific logger.
func WithLogger(l *zap.Logger) Option { func WithLogger(l *zap.Logger) Option {
return func(a *app) { return func(a *app) {
if l == nil { if l == nil {
@ -44,6 +47,7 @@ func WithLogger(l *zap.Logger) Option {
} }
} }
// WithConfig returns Option to use specific Viper configuration.
func WithConfig(c *viper.Viper) Option { func WithConfig(c *viper.Viper) Option {
return func(a *app) { return func(a *app) {
if c == nil { if c == nil {

View file

@ -15,6 +15,7 @@ import (
"google.golang.org/grpc/keepalive" "google.golang.org/grpc/keepalive"
) )
// PoolBuilderOptions contains options used to build connection pool.
type PoolBuilderOptions struct { type PoolBuilderOptions struct {
Key *ecdsa.PrivateKey Key *ecdsa.PrivateKey
NodeConnectionTimeout time.Duration NodeConnectionTimeout time.Duration
@ -28,17 +29,21 @@ type PoolBuilderOptions struct {
connections []*grpc.ClientConn connections []*grpc.ClientConn
} }
// PoolBuilder is an interim structure used to collect node addresses/weights and
// build connection pool subsequently.
type PoolBuilder struct { type PoolBuilder struct {
addresses []string addresses []string
weights []float64 weights []float64
} }
// AddNode adds address/weight pair to node PoolBuilder list.
func (pb *PoolBuilder) AddNode(address string, weight float64) *PoolBuilder { func (pb *PoolBuilder) AddNode(address string, weight float64) *PoolBuilder {
pb.addresses = append(pb.addresses, address) pb.addresses = append(pb.addresses, address)
pb.weights = append(pb.weights, weight) pb.weights = append(pb.weights, weight)
return pb return pb
} }
// Build creates new pool based on current PoolBuilder state and options.
func (pb *PoolBuilder) Build(ctx context.Context, options *PoolBuilderOptions) (Pool, error) { func (pb *PoolBuilder) Build(ctx context.Context, options *PoolBuilderOptions) (Pool, error) {
if len(pb.addresses) == 0 { if len(pb.addresses) == 0 {
return nil, errors.New("no NeoFS peers configured") return nil, errors.New("no NeoFS peers configured")
@ -75,6 +80,7 @@ func (pb *PoolBuilder) Build(ctx context.Context, options *PoolBuilderOptions) (
return new(ctx, options) return new(ctx, options)
} }
// Pool is an interface providing connection artifacts on request.
type Pool interface { type Pool interface {
ConnectionArtifacts() (client.Client, *token.SessionToken, error) ConnectionArtifacts() (client.Client, *token.SessionToken, error)
} }

View file

@ -2,13 +2,17 @@ package connections
import "math/rand" import "math/rand"
// See Vose's Alias Method (https://www.keithschwarz.com/darts-dice-coins/). // Sampler implements weighted random number generation using Vose's Alias
// Method (https://www.keithschwarz.com/darts-dice-coins/).
type Sampler struct { type Sampler struct {
randomGenerator *rand.Rand randomGenerator *rand.Rand
probabilities []float64 probabilities []float64
alias []int alias []int
} }
// NewSampler creates new Sampler with a given set of probabilities using
// given source of randomness. Created Sampler will produce numbers from
// 0 to len(probabilities).
func NewSampler(probabilities []float64, source rand.Source) *Sampler { func NewSampler(probabilities []float64, source rand.Source) *Sampler {
sampler := &Sampler{} sampler := &Sampler{}
var ( var (
@ -53,6 +57,7 @@ func NewSampler(probabilities []float64, source rand.Source) *Sampler {
return sampler return sampler
} }
// Next returns the next (not so) random number from Sampler.
func (g *Sampler) Next() int { func (g *Sampler) Next() int {
n := len(g.alias) n := len(g.alias)
i := g.randomGenerator.Intn(n) i := g.randomGenerator.Intn(n)

View file

@ -165,11 +165,13 @@ func (o objectIDs) Slice() []string {
return res return res
} }
// Downloader is a download request handler.
type Downloader struct { type Downloader struct {
log *zap.Logger log *zap.Logger
plant neofs.ClientPlant plant neofs.ClientPlant
} }
// New creates an instance of Downloader using specified options.
func New(ctx context.Context, log *zap.Logger, plant neofs.ClientPlant) (*Downloader, error) { func New(ctx context.Context, log *zap.Logger, plant neofs.ClientPlant) (*Downloader, error) {
var err error var err error
d := &Downloader{log: log, plant: plant} d := &Downloader{log: log, plant: plant}
@ -187,6 +189,7 @@ func (d *Downloader) newRequest(ctx *fasthttp.RequestCtx, log *zap.Logger) *requ
} }
} }
// DownloadByAddress handles download requests using simple cid/oid format.
func (d *Downloader) DownloadByAddress(c *fasthttp.RequestCtx) { func (d *Downloader) DownloadByAddress(c *fasthttp.RequestCtx) {
var ( var (
err error err error
@ -214,6 +217,7 @@ func (d *Downloader) DownloadByAddress(c *fasthttp.RequestCtx) {
d.newRequest(c, log).receiveFile(getOpts) d.newRequest(c, log).receiveFile(getOpts)
} }
// DownloadByAttribute handles attribute-based download requests.
func (d *Downloader) DownloadByAttribute(c *fasthttp.RequestCtx) { func (d *Downloader) DownloadByAttribute(c *fasthttp.RequestCtx) {
var ( var (
err error err error

View file

@ -12,6 +12,8 @@ var (
globalContextOnce sync.Once globalContextOnce sync.Once
) )
// Context returns global context with initialized INT, TERM and HUP signal
// handlers set to notify this context.
func Context() context.Context { func Context() context.Context {
globalContextOnce.Do(func() { globalContextOnce.Do(func() {
globalContext, _ = signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) globalContext, _ = signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)

View file

@ -12,12 +12,15 @@ type (
log *zap.SugaredLogger log *zap.SugaredLogger
} }
// Logger includes grpclog.LoggerV2 interface with an additional
// Println method.
Logger interface { Logger interface {
grpclog.LoggerV2 grpclog.LoggerV2
Println(v ...interface{}) Println(v ...interface{})
} }
) )
// GRPC wraps given zap.Logger into grpclog.LoggerV2+ interface.
func GRPC(l *zap.Logger) Logger { func GRPC(l *zap.Logger) Logger {
log := l.WithOptions( log := l.WithOptions(
// skip gRPCLog + zapLogger in caller // skip gRPCLog + zapLogger in caller
@ -29,32 +32,47 @@ func GRPC(l *zap.Logger) Logger {
} }
} }
// Info implements grpclog.LoggerV2.
func (z *zapLogger) Info(args ...interface{}) { z.log.Info(args...) } func (z *zapLogger) Info(args ...interface{}) { z.log.Info(args...) }
// Infoln implements grpclog.LoggerV2.
func (z *zapLogger) Infoln(args ...interface{}) { z.log.Info(args...) } func (z *zapLogger) Infoln(args ...interface{}) { z.log.Info(args...) }
// Infof implements grpclog.LoggerV2.
func (z *zapLogger) Infof(format string, args ...interface{}) { z.log.Infof(format, args...) } func (z *zapLogger) Infof(format string, args ...interface{}) { z.log.Infof(format, args...) }
// Println allows to print a line with info severity.
func (z *zapLogger) Println(args ...interface{}) { z.log.Info(args...) } func (z *zapLogger) Println(args ...interface{}) { z.log.Info(args...) }
// Printf implements grpclog.LoggerV2.
func (z *zapLogger) Printf(format string, args ...interface{}) { z.log.Infof(format, args...) } func (z *zapLogger) Printf(format string, args ...interface{}) { z.log.Infof(format, args...) }
// Warning implements grpclog.LoggerV2.
func (z *zapLogger) Warning(args ...interface{}) { z.log.Warn(args...) } func (z *zapLogger) Warning(args ...interface{}) { z.log.Warn(args...) }
// Warningln implements grpclog.LoggerV2.
func (z *zapLogger) Warningln(args ...interface{}) { z.log.Warn(args...) } func (z *zapLogger) Warningln(args ...interface{}) { z.log.Warn(args...) }
// Warningf implements grpclog.LoggerV2.
func (z *zapLogger) Warningf(format string, args ...interface{}) { z.log.Warnf(format, args...) } func (z *zapLogger) Warningf(format string, args ...interface{}) { z.log.Warnf(format, args...) }
// Error implements grpclog.LoggerV2.
func (z *zapLogger) Error(args ...interface{}) { z.log.Error(args...) } func (z *zapLogger) Error(args ...interface{}) { z.log.Error(args...) }
// Errorln implements grpclog.LoggerV2.
func (z *zapLogger) Errorln(args ...interface{}) { z.log.Error(args...) } func (z *zapLogger) Errorln(args ...interface{}) { z.log.Error(args...) }
// Errorf implements grpclog.LoggerV2.
func (z *zapLogger) Errorf(format string, args ...interface{}) { z.log.Errorf(format, args...) } func (z *zapLogger) Errorf(format string, args ...interface{}) { z.log.Errorf(format, args...) }
// Fatal implements grpclog.LoggerV2.
func (z *zapLogger) Fatal(args ...interface{}) { z.log.Fatal(args...) } func (z *zapLogger) Fatal(args ...interface{}) { z.log.Fatal(args...) }
// Fatalln implements grpclog.LoggerV2.
func (z *zapLogger) Fatalln(args ...interface{}) { z.log.Fatal(args...) } func (z *zapLogger) Fatalln(args ...interface{}) { z.log.Fatal(args...) }
// Fatalf implements grpclog.LoggerV2.
func (z *zapLogger) Fatalf(format string, args ...interface{}) { z.log.Fatalf(format, args...) } func (z *zapLogger) Fatalf(format string, args ...interface{}) { z.log.Fatalf(format, args...) }
// V implements grpclog.LoggerV2.
func (z *zapLogger) V(int) bool { return z.Enabled(zapcore.DebugLevel) } func (z *zapLogger) V(int) bool { return z.Enabled(zapcore.DebugLevel) }

View file

@ -2,22 +2,32 @@ package logger
import "go.uber.org/zap" import "go.uber.org/zap"
// WithSamplingInitial returns Option that sets sampling initial parameter.
func WithSamplingInitial(v int) Option { return func(o *options) { o.SamplingInitial = v } } func WithSamplingInitial(v int) Option { return func(o *options) { o.SamplingInitial = v } }
// WithSamplingThereafter returns Option that sets sampling thereafter parameter.
func WithSamplingThereafter(v int) Option { return func(o *options) { o.SamplingThereafter = v } } func WithSamplingThereafter(v int) Option { return func(o *options) { o.SamplingThereafter = v } }
// WithFormat returns Option that sets format parameter.
func WithFormat(v string) Option { return func(o *options) { o.Format = v } } func WithFormat(v string) Option { return func(o *options) { o.Format = v } }
// WithLevel returns Option that sets Level parameter.
func WithLevel(v string) Option { return func(o *options) { o.Level = v } } func WithLevel(v string) Option { return func(o *options) { o.Level = v } }
// WithTraceLevel returns Option that sets trace level parameter.
func WithTraceLevel(v string) Option { return func(o *options) { o.TraceLevel = v } } func WithTraceLevel(v string) Option { return func(o *options) { o.TraceLevel = v } }
// WithoutDisclaimer returns Option that disables disclaimer.
func WithoutDisclaimer() Option { return func(o *options) { o.NoDisclaimer = true } } func WithoutDisclaimer() Option { return func(o *options) { o.NoDisclaimer = true } }
// WithoutCaller returns Option that disables caller printing.
func WithoutCaller() Option { return func(o *options) { o.NoCaller = true } } func WithoutCaller() Option { return func(o *options) { o.NoCaller = true } }
// WithAppName returns Option that sets application name.
func WithAppName(v string) Option { return func(o *options) { o.AppName = v } } func WithAppName(v string) Option { return func(o *options) { o.AppName = v } }
// WithAppVersion returns Option that sets application version.
func WithAppVersion(v string) Option { return func(o *options) { o.AppVersion = v } } func WithAppVersion(v string) Option { return func(o *options) { o.AppVersion = v } }
// WithZapOptions returns Option that sets zap logger options.
func WithZapOptions(opts ...zap.Option) Option { return func(o *options) { o.Options = opts } } func WithZapOptions(opts ...zap.Option) Option { return func(o *options) { o.Options = opts } }

View file

@ -8,6 +8,7 @@ import (
) )
type ( type (
// Option represents logger option setter.
Option func(o *options) Option func(o *options)
options struct { options struct {
@ -77,6 +78,8 @@ func defaults() *options {
} }
} }
// New returns new zap.Logger using all options specified and stdout used
// for output.
func New(opts ...Option) (*zap.Logger, error) { func New(opts ...Option) (*zap.Logger, error) {
o := defaults() o := defaults()
c := zap.NewProductionConfig() c := zap.NewProductionConfig()

View file

@ -1,8 +1,12 @@
package main package main
// Prefix is a prefix used for environment variables containing gateway
// configuration.
const Prefix = "HTTP_GW" const Prefix = "HTTP_GW"
var ( var (
Build = "now" // Build is a timestamp set during gateway build.
Build = "now"
// Version is gateway version.
Version = "dev" Version = "dev"
) )

View file

@ -13,12 +13,14 @@ import (
"github.com/nspcc-dev/neofs-http-gate/connections" "github.com/nspcc-dev/neofs-http-gate/connections"
) )
// BaseOptions represents basic NeoFS request options.
type BaseOptions struct { type BaseOptions struct {
Client client.Client Client client.Client
SessionToken *token.SessionToken SessionToken *token.SessionToken
BearerToken *token.BearerToken BearerToken *token.BearerToken
} }
// PutOptions represents NeoFS Put request options.
type PutOptions struct { type PutOptions struct {
BaseOptions BaseOptions
Attributes []*object.Attribute Attributes []*object.Attribute
@ -27,12 +29,14 @@ type PutOptions struct {
Reader io.Reader Reader io.Reader
} }
// GetOptions represents NeoFS Get request options.
type GetOptions struct { type GetOptions struct {
BaseOptions BaseOptions
ObjectAddress *object.Address ObjectAddress *object.Address
Writer io.Writer Writer io.Writer
} }
// SearchOptions represents NeoFS Search request options.
type SearchOptions struct { type SearchOptions struct {
BaseOptions BaseOptions
ContainerID *container.ID ContainerID *container.ID
@ -42,11 +46,13 @@ type SearchOptions struct {
} }
} }
// DeleteOptions represents NeoFS Delete request options.
type DeleteOptions struct { type DeleteOptions struct {
BaseOptions BaseOptions
ObjectAddress *object.Address ObjectAddress *object.Address
} }
// ObjectClient wraps basic NeoFS requests.
type ObjectClient interface { type ObjectClient interface {
Put(context.Context, *PutOptions) (*object.Address, error) Put(context.Context, *PutOptions) (*object.Address, error)
Get(context.Context, *GetOptions) (*object.Object, error) Get(context.Context, *GetOptions) (*object.Object, error)
@ -54,6 +60,8 @@ type ObjectClient interface {
Delete(context.Context, *DeleteOptions) error Delete(context.Context, *DeleteOptions) error
} }
// ClientPlant provides connections to NeoFS nodes from pool and allows to
// get local owner ID.
type ClientPlant interface { type ClientPlant interface {
ConnectionArtifacts() (client.Client, *token.SessionToken, error) ConnectionArtifacts() (client.Client, *token.SessionToken, error)
Object() ObjectClient Object() ObjectClient
@ -71,10 +79,12 @@ type neofsClientPlant struct {
pool connections.Pool pool connections.Pool
} }
// ConnectionArtifacts returns connection from pool.
func (cp *neofsClientPlant) ConnectionArtifacts() (client.Client, *token.SessionToken, error) { func (cp *neofsClientPlant) ConnectionArtifacts() (client.Client, *token.SessionToken, error) {
return cp.pool.ConnectionArtifacts() return cp.pool.ConnectionArtifacts()
} }
// Object returns ObjectClient instance from plant.
func (cp *neofsClientPlant) Object() ObjectClient { func (cp *neofsClientPlant) Object() ObjectClient {
return &neofsObjectClient{ return &neofsObjectClient{
key: cp.key, key: cp.key,
@ -82,14 +92,17 @@ func (cp *neofsClientPlant) Object() ObjectClient {
} }
} }
// OwnerID returns plant's owner ID.
func (cp *neofsClientPlant) OwnerID() *owner.ID { func (cp *neofsClientPlant) OwnerID() *owner.ID {
return cp.ownerID return cp.ownerID
} }
// NewClientPlant creates new ClientPlant from given context, pool and credentials.
func NewClientPlant(ctx context.Context, pool connections.Pool, creds Credentials) (ClientPlant, error) { func NewClientPlant(ctx context.Context, pool connections.Pool, creds Credentials) (ClientPlant, error) {
return &neofsClientPlant{key: creds.PrivateKey(), ownerID: creds.Owner(), pool: pool}, nil return &neofsClientPlant{key: creds.PrivateKey(), ownerID: creds.Owner(), pool: pool}, nil
} }
// Put does NeoFS Put request, returning new object address if successful.
func (oc *neofsObjectClient) Put(ctx context.Context, options *PutOptions) (*object.Address, error) { func (oc *neofsObjectClient) Put(ctx context.Context, options *PutOptions) (*object.Address, error) {
var ( var (
err error err error
@ -117,6 +130,7 @@ func (oc *neofsObjectClient) Put(ctx context.Context, options *PutOptions) (*obj
return address, nil return address, nil
} }
// Get does NeoFS Get request, returning an object received if successful.
func (oc *neofsObjectClient) Get(ctx context.Context, options *GetOptions) (*object.Object, error) { func (oc *neofsObjectClient) Get(ctx context.Context, options *GetOptions) (*object.Object, error) {
var ( var (
err error err error
@ -134,6 +148,7 @@ func (oc *neofsObjectClient) Get(ctx context.Context, options *GetOptions) (*obj
return obj, err return obj, err
} }
// Search does NeoFS Search request, returning object IDs if successful.
func (oc *neofsObjectClient) Search(ctx context.Context, options *SearchOptions) ([]*object.ID, error) { func (oc *neofsObjectClient) Search(ctx context.Context, options *SearchOptions) ([]*object.ID, error) {
sfs := object.NewSearchFilters() sfs := object.NewSearchFilters()
sfs.AddRootFilter() sfs.AddRootFilter()
@ -149,6 +164,7 @@ func (oc *neofsObjectClient) Search(ctx context.Context, options *SearchOptions)
) )
} }
// Delete deletes NeoFS object.
func (oc *neofsObjectClient) Delete(ctx context.Context, options *DeleteOptions) error { func (oc *neofsObjectClient) Delete(ctx context.Context, options *DeleteOptions) error {
ops := new(client.DeleteObjectParams).WithAddress(options.ObjectAddress) ops := new(client.DeleteObjectParams).WithAddress(options.ObjectAddress)
err := options.Client.DeleteObject( err := options.Client.DeleteObject(

View file

@ -24,8 +24,8 @@ type (
} }
) )
// New creates an instance of Credentials through string representation of secret. // NewCredentials creates an instance of Credentials through string
// It allows passing WIF, path, hex-encoded and others. // representation of secret. It allows passing WIF, path, hex-encoded and others.
func NewCredentials(secret string) (Credentials, error) { func NewCredentials(secret string) (Credentials, error) {
key, err := crypto.LoadPrivateKey(secret) key, err := crypto.LoadPrivateKey(secret)
if err != nil { if err != nil {

View file

@ -26,6 +26,7 @@ const (
// return // return
// } // }
// BearerTokenFromHeader extracts bearer token from Authorization request header.
func BearerTokenFromHeader(h *fasthttp.RequestHeader) []byte { func BearerTokenFromHeader(h *fasthttp.RequestHeader) []byte {
auth := h.Peek(fasthttp.HeaderAuthorization) auth := h.Peek(fasthttp.HeaderAuthorization)
if auth == nil || !bytes.HasPrefix(auth, []byte(bearerTokenHdr)) { if auth == nil || !bytes.HasPrefix(auth, []byte(bearerTokenHdr)) {
@ -37,6 +38,7 @@ func BearerTokenFromHeader(h *fasthttp.RequestHeader) []byte {
return auth return auth
} }
// BearerTokenFromCookie extracts bearer token from cookies.
func BearerTokenFromCookie(h *fasthttp.RequestHeader) []byte { func BearerTokenFromCookie(h *fasthttp.RequestHeader) []byte {
auth := h.Cookie(bearerTokenHdr) auth := h.Cookie(bearerTokenHdr)
if len(auth) == 0 { if len(auth) == 0 {
@ -46,6 +48,8 @@ func BearerTokenFromCookie(h *fasthttp.RequestHeader) []byte {
return auth return auth
} }
// StoreBearerToken extracts bearer token from header or cookie and stores
// it in the request context.
func StoreBearerToken(ctx *fasthttp.RequestCtx) error { func StoreBearerToken(ctx *fasthttp.RequestCtx) error {
tkn, err := fetchBearerToken(ctx) tkn, err := fetchBearerToken(ctx)
if err != nil { if err != nil {
@ -56,6 +60,8 @@ func StoreBearerToken(ctx *fasthttp.RequestCtx) error {
return nil return nil
} }
// LoadBearerToken returns bearer token stored in context given (if it's
// present there).
func LoadBearerToken(ctx context.Context) (*token.BearerToken, error) { func LoadBearerToken(ctx context.Context) (*token.BearerToken, error) {
if tkn, ok := ctx.Value(bearerTokenKey).(*token.BearerToken); ok && tkn != nil { if tkn, ok := ctx.Value(bearerTokenKey).(*token.BearerToken); ok && tkn != nil {
return tkn, nil return tkn, nil

View file

@ -7,6 +7,8 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
// MultipartFile provides standard ReadCloser interface and also allows one to
// get file name, it's used for multipart uploads.
type MultipartFile interface { type MultipartFile interface {
io.ReadCloser io.ReadCloser
FileName() string FileName() string

View file

@ -29,16 +29,20 @@ var putOptionsPool = sync.Pool{
}, },
} }
// Uploader is an upload request handler.
type Uploader struct { type Uploader struct {
log *zap.Logger log *zap.Logger
plant neofs.ClientPlant plant neofs.ClientPlant
enableDefaultTimestamp bool enableDefaultTimestamp bool
} }
// New creates a new Uploader using specified logger, connection pool and
// other options.
func New(log *zap.Logger, plant neofs.ClientPlant, enableDefaultTimestamp bool) *Uploader { func New(log *zap.Logger, plant neofs.ClientPlant, enableDefaultTimestamp bool) *Uploader {
return &Uploader{log, plant, enableDefaultTimestamp} return &Uploader{log, plant, enableDefaultTimestamp}
} }
// Upload handles multipart upload request.
func (u *Uploader) Upload(c *fasthttp.RequestCtx) { func (u *Uploader) Upload(c *fasthttp.RequestCtx) {
var ( var (
err error err error