-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
fe70bca
commit f2a2cda
Showing
7 changed files
with
186 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
build | ||
build | ||
.vscode |
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,11 +1,11 @@ | ||
all:build | ||
|
||
build:fmt | ||
go build -o ./build/server ./example/server/ && | ||
go build -o ./build/server ./example/server/ | ||
go build -o ./build/client ./example/client/ | ||
|
||
run-server:build | ||
./build/server | ||
./build/server --local http://127.0.0.1:8080 --peer http://127.0.0.1:8081 --peer http://127.0.0.1:8082 | ||
|
||
fmt: | ||
go mod tidy && gofmt -w . |
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,27 +1,27 @@ | ||
package raftgo | ||
|
||
type AppendEntriesArgs struct { | ||
Term int | ||
LeaderID int | ||
PrevLogIndex int | ||
PrevLogTerm int | ||
Entries []LogEntry | ||
LeaderCommit int | ||
Term int `json:"term"` | ||
LeaderID int `json:"leader_id"` | ||
PrevLogIndex int `json:"prev_log_index"` | ||
PrevLogTerm int `json:"prev_log_term"` | ||
Entries []LogEntry `json:"entries"` | ||
LeaderCommit int `json:"leader_commit"` | ||
} | ||
|
||
type AppendEntriesReply struct { | ||
Term int | ||
Success bool | ||
Term int `json:"term"` | ||
Success bool `json:"success"` | ||
} | ||
|
||
type RequestVoteArgs struct { | ||
Term int | ||
CandidateID int | ||
LastLogIndex int | ||
LastLogTerm int | ||
Term int `json:"term"` | ||
CandidateID int `json:"candidate_id"` | ||
LastLogIndex int `json:"last_log_index"` | ||
LastLogTerm int `json:"last_log_term"` | ||
} | ||
|
||
type RequestVoteReply struct { | ||
Term int | ||
VoteGranted bool | ||
Term int `json:"term"` | ||
VoteGranted bool `json:"vote_granted"` | ||
} |
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,2 +1,103 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"net/http" | ||
"time" | ||
) | ||
|
||
type Option struct { | ||
Addr string | ||
} | ||
|
||
type Command struct { | ||
Type string `json:"type"` | ||
Key string `json:"key"` | ||
Value string `json:"value,omitempty"` | ||
} | ||
|
||
type Response struct { | ||
Success bool `json:"success"` | ||
Value string `json:"value,omitempty"` | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "client", | ||
Short: "A client for raft", | ||
} | ||
|
||
var setCmd = &cobra.Command{ | ||
Use: "set [key] [value]", | ||
Short: "Set the value of a string key to a string", | ||
Args: cobra.ExactArgs(2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
addr, _ := cmd.Flags().GetString("addr") | ||
key := args[0] | ||
value := args[1] | ||
set(Option{Addr: addr}, key, value) | ||
}, | ||
} | ||
|
||
var getCmd = &cobra.Command{ | ||
Use: "get [key]", | ||
Short: "Get the string value of a given string key", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
addr, _ := cmd.Flags().GetString("addr") | ||
key := args[0] | ||
get(Option{Addr: addr}, key) | ||
}, | ||
} | ||
|
||
var rmCmd = &cobra.Command{ | ||
Use: "rm [key]", | ||
Short: "Remove a given key", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
addr, _ := cmd.Flags().GetString("addr") | ||
key := args[0] | ||
rm(Option{Addr: addr}, key) | ||
}, | ||
} | ||
|
||
func main() { | ||
rootCmd.PersistentFlags().StringP("addr", "a", "", "server addr.") | ||
rootCmd.AddCommand(setCmd, getCmd, rmCmd) | ||
cobra.CheckErr(rootCmd.Execute()) | ||
} | ||
|
||
func set(op Option, key string, value string) { | ||
command := Command{Type: "set", Key: key, Value: value} | ||
send("POST", op.Addr, "/append", command) | ||
} | ||
|
||
func get(op Option, key string) { | ||
send("GET", op.Addr, "/get?key="+key, nil) | ||
} | ||
|
||
func rm(op Option, key string) { | ||
command := Command{Type: "rm", Key: key} | ||
send("POST", op.Addr, "/append", command) | ||
} | ||
|
||
func send(method string, addr string, path string, body interface{}) { | ||
start := time.Now() | ||
jsonBody, _ := json.Marshal(body) | ||
resp, err := http.Post(addr+path, "application/json", bytes.NewBuffer(jsonBody)) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
data, _ := io.ReadAll(resp.Body) | ||
var result Response | ||
json.Unmarshal(data, &result) | ||
fmt.Println("Response:", result) | ||
fmt.Println("Latency:", time.Since(start)) | ||
} |
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