| Conditions | 10 |
| Total Lines | 70 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like raftproxy.*RaftProxy.OpenRaft often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package raftproxy |
||
| 167 | func (c *RaftProxy) OpenRaft(storage string) error { |
||
| 168 | // Setup Raft configuration. |
||
| 169 | config := raft.DefaultConfig() |
||
| 170 | config.LocalID = raft.ServerID(c.localId) |
||
| 171 | |||
| 172 | // Setup Raft communication. |
||
| 173 | addr, err := net.ResolveTCPAddr("tcp", c.raftAddr) |
||
| 174 | if err != nil { |
||
| 175 | return err |
||
| 176 | } |
||
| 177 | transport, err := raft.NewTCPTransport(c.raftAddr, addr, 3, tcpTimeout, os.Stderr) |
||
| 178 | if err != nil { |
||
| 179 | return err |
||
| 180 | } |
||
| 181 | |||
| 182 | // Create the snapshot store. This allows the Raft to truncate the log. |
||
| 183 | snapshots, err := raft.NewFileSnapshotStore(storage, retainSnapshotCount, os.Stderr) |
||
| 184 | if err != nil { |
||
| 185 | return fmt.Errorf("file snapshot store: %s", err) |
||
| 186 | } |
||
| 187 | |||
| 188 | // Create the log store and stable store. |
||
| 189 | var logStore raft.LogStore |
||
| 190 | var stableStore raft.StableStore |
||
| 191 | if storage == "" { |
||
| 192 | logStore = raft.NewInmemStore() |
||
| 193 | stableStore = raft.NewInmemStore() |
||
| 194 | } else { |
||
| 195 | boltDB, err := raftboltdb.NewBoltStore(filepath.Join(storage, "raft.db")) |
||
| 196 | if err != nil { |
||
| 197 | return fmt.Errorf("new bolt store: %s", err) |
||
| 198 | } |
||
| 199 | logStore = boltDB |
||
| 200 | stableStore = boltDB |
||
| 201 | } |
||
| 202 | |||
| 203 | // Instantiate the Raft systems. |
||
| 204 | ra, err := raft.NewRaft(config, (*fsm)(c), logStore, stableStore, snapshots, transport) |
||
| 205 | if err != nil { |
||
| 206 | return fmt.Errorf("new raft: %s", err) |
||
| 207 | } |
||
| 208 | |||
| 209 | c.raft = ra |
||
| 210 | |||
| 211 | if c.joinAddr == "" { |
||
| 212 | configuration := raft.Configuration{ |
||
| 213 | Servers: []raft.Server{ |
||
| 214 | { |
||
| 215 | ID: config.LocalID, |
||
| 216 | Address: transport.LocalAddr(), |
||
| 217 | }, |
||
| 218 | }, |
||
| 219 | } |
||
| 220 | ra.BootstrapCluster(configuration) |
||
| 221 | } else { |
||
| 222 | b, err := json.Marshal(map[string]string{"addr": c.raftAddr, "id": c.localId}) |
||
| 223 | if err != nil { |
||
| 224 | return err |
||
| 225 | } |
||
| 226 | resp, err := http.Post(fmt.Sprintf("http://%s/join", c.joinAddr), "", bytes.NewReader(b)) |
||
| 227 | if err != nil { |
||
| 228 | return err |
||
| 229 | } |
||
| 230 | defer resp.Body.Close() |
||
| 231 | |||
| 232 | return nil |
||
| 233 | |||
| 234 | } |
||
| 235 | |||
| 236 | return nil |
||
| 237 | } |
||
| 290 |