-
Notifications
You must be signed in to change notification settings - Fork 55
feat: use error channels #62
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package iface | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ipfs/interface-go-ipfs-core/path" | ||
) | ||
|
||
func ExampleNameAPI_Search() { | ||
var api CoreAPI | ||
|
||
_ = func(ctx context.Context) (result path.Path, err error) { | ||
results, errC := api.Name().Search(ctx, "foobar") | ||
for result = range results { | ||
fmt.Println(result) | ||
} | ||
return result, <-errC | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package iface | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
func ExamplePinAPI_Ls() { | ||
var api CoreAPI | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
pins, errC := api.Pin().Ls(ctx) | ||
for pin := range pins { | ||
fmt.Println(pin.Path()) | ||
} | ||
if err := <-errC; err != nil { | ||
fmt.Printf("error: %s\n", err) | ||
} | ||
} | ||
|
||
func ExamplePinAPI_Verify() { | ||
var api CoreAPI | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
status, errC := api.Pin().Verify(ctx) | ||
for pin := range status { | ||
if !pin.Ok() { | ||
for _, missing := range pin.BadNodes() { | ||
fmt.Printf("missing node %s: %s\n", missing.Path().Cid(), missing.Err()) | ||
} | ||
} | ||
} | ||
if err := <-errC; err != nil { | ||
fmt.Printf("failed to complete pin verification request: %s\n", err) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,17 +192,17 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) { | |
t.Errorf("unexpected path, %s != %s", list[0].Path().Cid().String(), p0.Cid().String()) | ||
} | ||
|
||
res, err := api.Pin().Verify(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
res, errCh := api.Pin().Verify(ctx) | ||
n := 0 | ||
for r := range res { | ||
if !r.Ok() { | ||
t.Error("expected pin to be ok") | ||
} | ||
n++ | ||
} | ||
if err := <-errCh; err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if n != 1 { | ||
t.Errorf("unexpected verify result count: %d", n) | ||
|
@@ -583,19 +583,10 @@ func assertNotPinned(t *testing.T, ctx context.Context, api iface.CoreAPI, p pat | |
} | ||
} | ||
|
||
func accPins(pins <-chan iface.Pin, err error) ([]iface.Pin, error) { | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
func accPins(pins <-chan iface.Pin, err <-chan error) ([]iface.Pin, error) { | ||
var result []iface.Pin | ||
|
||
for pin := range pins { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so, when an error happen, the result channel is not closed right ? Doesn't that means the caller would get stuck in this reading loop ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the new design, both the result and the error channels are always closed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm. I need to document that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated the issue description and the interface documentation. It looks like docker isn't actually all that consistent. |
||
if pin.Err() != nil { | ||
return nil, pin.Err() | ||
} | ||
result = append(result, pin) | ||
} | ||
|
||
return result, nil | ||
return result, <-err | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why aren't we following the pattern here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, I'm asking IPFS to find me some providers for a key. If the request is canceled, we can just say "we're done" and walk away. That is, a partial response is still a valid response.
This is different from listing files/pins because:
Ls
returned a subset of files/pins, that would be an incomplete result.On the other hand, this brings up a good point. IMO, the
Search
function should have the same interface. That isNameAPI.Search(ctx, name, opts) (<-chan path.Path, error)
. Thoughts?cc @alanshaw, I think this answers your question as well.