Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fallback executor support #171

Merged
merged 3 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions examples/adder/remote/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,20 @@ func main() {
panic(err)
}

req.Options["encoding"] = cmds.Text

// create http rpc client
client := http.NewClient(":6798")

// send request to server
res, err := client.Send(req)
if err != nil {
panic(err)
}

req.Options["encoding"] = cmds.Text

// create an emitter
re, err := cli.NewResponseEmitter(os.Stdout, os.Stderr, req)
if err != nil {
panic(err)
}

// copy received result into cli emitter
if pr, ok := req.Command.PostRun[cmds.CLI]; ok {
err = pr(res, re)
} else {
err = cmds.Copy(re, res)
}
// send request to server
err = client.Execute(req, re, nil)
if err != nil {
re.CloseWithError(err)
panic(err)
}

os.Exit(re.Status())
}
30 changes: 22 additions & 8 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ var OptionSkipMap = map[string]bool{
"api": true,
}

// Client is the commands HTTP client interface.
type Client interface {
Send(req *cmds.Request) (cmds.Response, error)
}

type client struct {
serverAddress string
httpClient *http.Client
ua string
apiPrefix string
fallback cmds.Executor
}

type ClientOpt func(*client)
Expand All @@ -48,7 +44,16 @@ func ClientWithAPIPrefix(apiPrefix string) ClientOpt {
}
}

func NewClient(address string, opts ...ClientOpt) Client {
// ClientWithFallback adds a fallback executor to the client.
//
// Note: This may run the PreRun function twice.
func ClientWithFallback(exe cmds.Executor) ClientOpt {
return func(c *client) {
c.fallback = exe
}
}

func NewClient(address string, opts ...ClientOpt) cmds.Executor {
if !strings.HasPrefix(address, "http://") {
address = "http://" + address
}
Expand All @@ -69,16 +74,25 @@ func NewClient(address string, opts ...ClientOpt) Client {
func (c *client) Execute(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
cmd := req.Command

err := cmd.CheckArguments(req)
if err != nil {
return err
}

if cmd.PreRun != nil {
err := cmd.PreRun(req, env)
if err != nil {
return err
}
}

res, err := c.Send(req)
res, err := c.send(req)
if err != nil {
if isConnRefused(err) {
if c.fallback != nil {
// XXX: this runs the PreRun twice
return c.fallback.Execute(req, re, env)
}
err = fmt.Errorf("cannot connect to the api. Is the daemon running? To run as a standalone CLI command remove the api file in `$IPFS_PATH/api`")
}
return err
Expand Down Expand Up @@ -146,7 +160,7 @@ func (c *client) toHTTPRequest(req *cmds.Request) (*http.Request, error) {
return httpReq, nil
}

func (c *client) Send(req *cmds.Request) (cmds.Response, error) {
func (c *client) send(req *cmds.Request) (cmds.Response, error) {
if req.Context == nil {
log.Warningf("no context set in request")
req.Context = context.Background()
Expand Down
4 changes: 2 additions & 2 deletions http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestClientUserAgent(t *testing.T) {

c := NewClient(tc.host, ClientWithUserAgent(tc.ua)).(*client)
c.httpClient = testClient
c.Send(r)
c.send(r)

if !called {
t.Error("handler has not been called")
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestClientAPIPrefix(t *testing.T) {

c := NewClient(tc.host, ClientWithAPIPrefix(tc.prefix)).(*client)
c.httpClient = testClient
c.Send(r)
c.send(r)

if !called {
t.Error("handler has not been called")
Expand Down
2 changes: 1 addition & 1 deletion http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestHTTP(t *testing.T) {
req.Files = tc.file
}

res, err := c.Send(req)
res, err := c.(*client).send(req)
if tc.sendErr != nil {
if err == nil {
t.Fatalf("expected error %q but got nil", tc.sendErr)
Expand Down