Agreement Receiver service (#546)

* setup config and server structure for receiving bandwidth agreements

* add error channel
This commit is contained in:
Cameron 2018-11-01 12:51:19 -04:00 committed by GitHub
parent 8c740f8390
commit 856c3a779f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,37 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package agreementreceiver
import (
"context"
"go.uber.org/zap"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/provider"
)
var (
mon = monkit.Package()
)
// Config is a configuration struct that is everything you need to start an
// agreement receiver responsibility
type Config struct {
DatabaseURL string `help:"the database connection string to use" default:"$CONFDIR/agreements.db"`
}
// Run implements the provider.Responsibility interface
func (c Config) Run(ctx context.Context, server *provider.Provider) (err error) {
defer mon.Task()(&ctx)(&err)
ns, err := NewServer(c.DatabaseURL, server.Identity(), zap.L())
if err != nil {
return err
}
pb.RegisterBandwidthServer(server.GRPC(), ns)
return server.Run(ctx)
}

View File

@ -0,0 +1,66 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package agreementreceiver
import (
"fmt"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/provider"
"go.uber.org/zap"
)
// Server is an implementation of the pb.BandwidthServer interface
type Server struct {
// DB *dbx.DB
identity *provider.FullIdentity
logger *zap.Logger
}
// NewServer initializes a Server struct
func NewServer(source string, fi *provider.FullIdentity, logger *zap.Logger) (*Server, error) {
//TODO: open dbx postgres database and pass to Server
return &Server{
identity: fi,
logger: logger,
}, nil
}
// BandwidthAgreements receives and stores bandwidth agreements from storage nodes
func (s *Server) BandwidthAgreements(stream pb.Bandwidth_BandwidthAgreementsServer) (err error) {
ctx := stream.Context()
defer mon.Task()(&ctx)(&err)
ch := make(chan *pb.RenterBandwidthAllocation, 1)
errch := make(chan error, 1)
go func() {
for {
msg, err := stream.Recv()
if err != nil {
errch <- err
return
}
ch <- msg
}
}()
for {
select {
case err := <-errch:
return err
case <-ctx.Done():
return nil
case agreement := <-ch:
go func() {
fmt.Println(agreement)
//TODO: write to DB
//err = s.DB.WriteAgreement(agreement)
// if err != nil {
// return err
// }
}()
}
}
}