~rogpeppe/juju-core/315-api-dialopts

« back to all changes in this revision

Viewing changes to state/api/apiclient.go

  • Committer: Roger Peppe
  • Date: 2013-05-25 13:11:46 UTC
  • Revision ID: roger.peppe@canonical.com-20130525131146-knkj6367hnnmapq4
api: add DialOpts to Open

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
        Delay: 500 * time.Millisecond,
46
46
}
47
47
 
48
 
func Open(info *Info) (*State, error) {
 
48
// DialOpts holds configuration parameters that control the
 
49
// Dialing behavior when connecting to a state server.
 
50
type DialOpts struct {
 
51
        // Timeout is the amount of time to wait contacting
 
52
        // a state server.
 
53
        Timeout time.Duration
 
54
 
 
55
        // RetryDelay is the amount of time to wait between
 
56
        // unsucssful connection attempts.
 
57
        RetryDelay time.Duration
 
58
}
 
59
 
 
60
// DefaultDialOpts returns a DialOpts representing the default
 
61
// parameters for contacting a state server.
 
62
func DefaultDialOpts() DialOpts {
 
63
        return DialOpts{
 
64
                Timeout:    10 * time.Minute,
 
65
                RetryDelay: 2 * time.Second,
 
66
        }
 
67
}
 
68
 
 
69
func Open(info *Info, opts DialOpts) (*State, error) {
49
70
        // TODO Select a random address from info.Addrs
50
71
        // and only fail when we've tried all the addresses.
51
72
        // TODO what does "origin" really mean, and is localhost always ok?
64
85
                ServerName: "anything",
65
86
        }
66
87
        var conn *websocket.Conn
 
88
        openAttempt := utils.AttemptStrategy{
 
89
                Total: opts.Timeout,
 
90
                Delay: opts.RetryDelay,
 
91
        }
67
92
        for a := openAttempt.Start(); a.Next(); {
68
93
                log.Infof("state/api: dialing %q", cfg.Location)
69
94
                conn, err = websocket.DialConfig(cfg)