-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.go
More file actions
69 lines (62 loc) · 2.04 KB
/
Copy pathmethods.go
File metadata and controls
69 lines (62 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package bitclient
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
)
// Ping checks server liveness. Returns nil on a 200 "pong" response.
func (c *Client) Ping(ctx context.Context) error {
status, body, err := c.do(ctx, http.MethodGet, "/ping")
if err != nil {
return err
}
if status != http.StatusOK || body != "pong" {
return fmt.Errorf("bitclient: ping returned status %d body %q", status, body)
}
return nil
}
// QoS queries the server for the project's current QoS (claims/second).
// Unlike the cached limiter value, this always hits the server.
func (c *Client) QoS(ctx context.Context) (float64, error) {
return c.fetchQoS(ctx)
}
// Claim claims the next todo offset, moving it into the wip map.
//
// Claim is rate-limited to the project's QoS (claims/second): it blocks until
// a token is available or ctx is cancelled. The QoS is fetched at Connect time
// and refreshed once per minute.
//
// Returns the claimed offset, or ErrNoBitsToClaim when there is nothing left.
func (c *Client) Claim(ctx context.Context) (uint64, error) {
if !c.connected || c.limiter == nil {
return 0, ErrNotConnected
}
if err := c.limiter.Wait(ctx); err != nil {
return 0, fmt.Errorf("bitclient: claim rate-limited: %w", err)
}
status, body, err := c.do(ctx, http.MethodGet, "/proj/"+c.project+"/claim")
if err != nil {
var he *HTTPError
if errors.As(err, &he) && he.Status == http.StatusNotFound {
return 0, ErrNoBitsToClaim
}
return 0, err
}
_ = status
offset, perr := strconv.ParseUint(body, 10, 64)
if perr != nil {
return 0, fmt.Errorf("bitclient: invalid claim response %q: %w", body, perr)
}
return offset, nil
}
// Move moves an offset from one map to another.
//
// `from` is a map name (MapTodo/MapWIP/MapFail/MapDone) or FromAuto to let the
// server detect the current map. `to` is the destination map name.
func (c *Client) Move(ctx context.Context, offset uint64, from, to string) error {
path := fmt.Sprintf("/proj/%s/move/%d/%s/%s", c.project, offset, from, to)
_, _, err := c.do(ctx, http.MethodPatch, path)
return err
}