Skip to content

Commit

Permalink
feat(remote): Switch to --remote-accept-size-max flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Mar 29, 2019
1 parent 5cec2de commit b26cbb9
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 23 deletions.
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func NewServerRoutes(s *Server) *http.ServeMux {
dsh := NewDatasetHandlers(s.qriNode, s.cfg.API.ReadOnly)

if s.cfg.API.RemoteMode {
log.Info("This server is running in `remote` mode")
remh := NewRemoteHandlers(s.qriNode)
m.Handle("/dataset", s.middleware(remh.ReceiveHandler))
receivers, err := makeDagReceiver(s.qriNode)
Expand Down
4 changes: 2 additions & 2 deletions api/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func TestRemoteHandlers(t *testing.T) {
}

// Reject all dag.Info's
lib.Config.API.RemoteAlwaysAccept = false
lib.Config.API.RemoteAcceptSizeMax = 0
rh := NewRemoteHandlers(node)
runHandlerTestCases(t, "remote reject", rh.ReceiveHandler, testCases, true)

// Accept all dag.Info's
lib.Config.API.RemoteAlwaysAccept = true
lib.Config.API.RemoteAcceptSizeMax = -1
rh = NewRemoteHandlers(node)
runHandlerTestCases(t, "remote accept", rh.ReceiveHandler, testCases, true)
}
34 changes: 34 additions & 0 deletions base/fill/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,40 @@ func putValueToPlace(val interface{}, place reflect.Value) error {
return nil
}
return fmt.Errorf("need type int, value %s", val)
case reflect.Int64:
num, ok := val.(int)
if ok {
place.SetInt(int64(num))
return nil
}
num64, ok := val.(int64)
if ok {
place.SetInt(num64)
return nil
}
float64, ok := val.(float64)
if ok {
place.SetInt(int64(float64))
return nil
}
return fmt.Errorf("need type int64, value %s | %v | %s", val, val, reflect.TypeOf(val))
case reflect.Uint64:
num, ok := val.(uint)
if ok {
place.SetUint(uint64(num))
return nil
}
num64, ok := val.(uint64)
if ok {
place.SetUint(num64)
return nil
}
float64, ok := val.(float64)
if ok {
place.SetUint(uint64(float64))
return nil
}
return fmt.Errorf("need type uint64, value %s | %v | %s", val, val, reflect.TypeOf(val))
case reflect.Float64:
num, ok := val.(int)
if ok {
Expand Down
28 changes: 28 additions & 0 deletions base/fill/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ type Collection struct {
Dict map[string]string
List []string
Sub SubElement
Big int64
Ubig uint64
}

type SubElement struct {
Expand Down Expand Up @@ -263,6 +265,32 @@ func TestFillFloatingPoint(t *testing.T) {
}
}

func TestFillInt64(t *testing.T) {
jsonData := `{
"Big": 1234567890123456789,
"Ubig": 9934567890123456789
}`

data := make(map[string]interface{})
err := json.Unmarshal([]byte(jsonData), &data)
if err != nil {
panic(err)
}

var c Collection
err = Struct(data, &c)
if err != nil {
panic(err)
}

if c.Big != 1234567890123456768 {
t.Errorf("expected: c.Big should be 1234567890123456768, got: %d", c.Big)
}
if c.Ubig != 9934567890123456512 {
t.Errorf("expected: c.Ubig should be 9934567890123456512, got: %d", c.Ubig)
}
}

func TestFillMetaKeywords(t *testing.T) {
jsonData := `{
"Keywords": [
Expand Down
17 changes: 8 additions & 9 deletions cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ peers & swapping data.`,
cmd.Flags().BoolVarP(&o.Setup, "setup", "", false, "run setup if necessary, reading options from environment variables")
cmd.Flags().BoolVarP(&o.ReadOnly, "read-only", "", false, "run qri in read-only mode, limits the api endpoints")
cmd.Flags().BoolVarP(&o.RemoteMode, "remote-mode", "", false, "run qri in remote mode")
cmd.Flags().BoolVarP(&o.RemoteAlwaysAccept, "remote-always-accept", "", false, "when running as a remote, accept all datasets sent")
cmd.Flags().Int64VarP(&o.RemoteAcceptSizeMax, "remote-accept-size-max", "", -1, "when running as a remote, max size of dataset to accept, -1 for any size")
cmd.Flags().StringVarP(&o.Registry, "registry", "", "", "specify registry to setup with. only works when --setup is true")

return cmd
Expand All @@ -74,11 +74,11 @@ type ConnectOptions struct {
DisableWebapp bool
DisableP2P bool

Registry string
Setup bool
ReadOnly bool
RemoteMode bool
RemoteAlwaysAccept bool
Registry string
Setup bool
ReadOnly bool
RemoteMode bool
RemoteAcceptSizeMax int64

Node *p2p.QriNode
Config *config.Config
Expand Down Expand Up @@ -140,9 +140,6 @@ func (o *ConnectOptions) Run() (err error) {
if o.RemoteMode {
cfg.API.RemoteMode = true
}
if o.RemoteAlwaysAccept {
cfg.API.RemoteAlwaysAccept = true
}
if o.DisableP2P {
cfg.P2P.Enabled = false
}
Expand All @@ -156,6 +153,8 @@ func (o *ConnectOptions) Run() (err error) {
cfg.Webapp.Enabled = false
}

cfg.API.RemoteAcceptSizeMax = o.RemoteAcceptSizeMax

s := api.New(o.Node, &cfg)
err = s.Serve()
if err != nil && err.Error() == "http: Server closed" {
Expand Down
4 changes: 2 additions & 2 deletions config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type API struct {
ReadOnly bool `json:"readonly"`
// remote mode
RemoteMode bool `json:"remotemode"`
// remote always accept
RemoteAlwaysAccept bool `json:"remotealwaysaccept"`
// maximum size of dataset to accept for remote mode
RemoteAcceptSizeMax int64 `json:"remoteacceptsizemax"`
// URLRoot is the base url for this server
URLRoot string `json:"urlroot"`
// TLS enables https via letsEyncrypt
Expand Down
19 changes: 11 additions & 8 deletions lib/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,22 @@ func (r *RemoteRequests) Receive(p *ReceiveParams, reason *string) (err error) {
}

// TODO(dlong): Customization for how to decide to accept the dataset.
if !Config.API.RemoteAlwaysAccept {
if Config.API.RemoteAcceptSizeMax == 0 {
*reason = "not accepting any datasets"
return nil
}

var totalSize uint64
for _, s := range dinfo.Sizes {
totalSize += s
}
// If size is -1, accept any size of dataset. Otherwise, check if the size is allowed.
if Config.API.RemoteAcceptSizeMax != -1 {
var totalSize uint64
for _, s := range dinfo.Sizes {
totalSize += s
}

if totalSize >= allowedDagInfoSize {
*reason = "dataset size too large"
return nil
if totalSize >= uint64(Config.API.RemoteAcceptSizeMax) {
*reason = "dataset size too large"
return nil
}
}

// TODO(dlong): Generate a dsync session id, store the dag.info associated with that id,
Expand Down
4 changes: 2 additions & 2 deletions lib/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestRemote(t *testing.T) {
var rejectReason string

// Reject all dag.Info's
Config.API.RemoteAlwaysAccept = false
Config.API.RemoteAcceptSizeMax = 0
params := ReceiveParams{
Body: "{\"Sizes\":[10,20,30]}",
}
Expand All @@ -39,7 +39,7 @@ func TestRemote(t *testing.T) {
}

// Accept all dag.Info's
Config.API.RemoteAlwaysAccept = true
Config.API.RemoteAcceptSizeMax = -1
params = ReceiveParams{
Body: "{\"Sizes\":[10,20,30]}",
}
Expand Down

0 comments on commit b26cbb9

Please sign in to comment.