-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
220 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package pocketic | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func (pic PocketIC) do(method, url string, input, output any) error { | ||
start := time.Now() | ||
for { | ||
if pic.timeout < time.Since(start) { | ||
return fmt.Errorf("timeout exceeded") | ||
} | ||
|
||
pic.logger.Printf("[POCKETIC] %s %s %+v", method, url, input) | ||
req, err := newRequest(method, url, input) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := pic.client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
switch resp.StatusCode { | ||
case http.StatusOK, http.StatusCreated: | ||
if resp.Body == nil || output == nil { | ||
// No need to decode the response body. | ||
return nil | ||
} | ||
return json.NewDecoder(resp.Body).Decode(output) | ||
case http.StatusAccepted: | ||
var response startedOrBusyResponse | ||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { | ||
return err | ||
} | ||
if method == http.MethodGet { | ||
continue | ||
} | ||
for { | ||
if pic.timeout < time.Since(start) { | ||
return fmt.Errorf("timeout exceeded") | ||
} | ||
|
||
req, err := newRequest( | ||
http.MethodGet, | ||
fmt.Sprintf( | ||
"%s/read_graph/%s/%s", | ||
pic.server.URL(), | ||
response.StateLabel, | ||
response.OpID, | ||
), | ||
nil, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := pic.client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
switch resp.StatusCode { | ||
case http.StatusOK: | ||
if resp.Body == nil || output == nil { | ||
// No need to decode the response body. | ||
return nil | ||
} | ||
return json.NewDecoder(resp.Body).Decode(output) | ||
case http.StatusAccepted, http.StatusConflict: | ||
default: | ||
var errResp ErrorMessage | ||
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil { | ||
return err | ||
} | ||
return errResp | ||
} | ||
} | ||
case http.StatusConflict: | ||
var response startedOrBusyResponse | ||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { | ||
return err | ||
} | ||
time.Sleep(pic.delay) // Retry after a short delay. | ||
continue | ||
default: | ||
var errResp ErrorMessage | ||
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil { | ||
return err | ||
} | ||
return errResp | ||
} | ||
} | ||
} | ||
|
||
type startedOrBusyResponse struct { | ||
StateLabel string `json:"state_label"` | ||
OpID string `json:"op_id"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package pocketic | ||
|
||
//go:generate go run ../cmd/goic/main.go generate did testdata/main.did hello --output=agent_test.go --packageName=pocketic_test | ||
//go:generate go run ../cmd/goic/main.go generate did testdata/main.did hello --output=agent_test.go --packageName=pocketic_test --indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.