-
Couldn't load subscription status.
- Fork 11
Add support for transactions #6
Conversation
| return ParseArgs(c.Query(ctx, cmd, args...), nil) | ||
| } | ||
| | ||
| func (c *Client) MultiExec(ctx context.Context, cmds ...Command) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need documentation for this method.
| } | ||
| | ||
| r, err := c.Do(&Request{ | ||
| Addr: addr, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be rewritten
Cms: []Command{{ ... }}(no need to repeat Command{)
| | ||
| return r.Args | ||
| } | ||
| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, we need documentation for this method.
conn_test.go Outdated
| tx1 := makeConnTx(ctx, &Request{Cmd: "SET", Args: List("redis-go.test.conn", "0123456789")}) | ||
| tx2 := makeConnTx(ctx, &Request{Cmd: "GET", Args: List("redis-go.test.conn")}) | ||
| req1 := &Request{ | ||
| Cmds: []Command{Command{Cmd: "SET", Args: List("redis-go.test.conn", "0123456789")}}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we can get rid of the Command as well.
conn_test.go Outdated
| tx1 := makeConnTx(ctx, req1) | ||
| | ||
| req2 := &Request{ | ||
| Cmds: []Command{Command{Cmd: "GET", Args: List("redis-go.test.conn")}}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we can get rid of the Command as well.
request.go Outdated
| // For server request, Args is never nil, even if there are no values in | ||
| // the argument list. | ||
| Args Args | ||
| // Cmds is the list of transaction's commands. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cmds is the list of transaction's commands. => Cmds is the list of commands that are part of the transaction.
request.go Outdated
| return ParseArgs(req.Args, dsts...) | ||
| for _, cmd := range req.Cmds { | ||
| err := ParseArgs(cmd.Args, dsts...) | ||
| if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's inline the error check here if err := ParseArgs(cmd.Args, dsts...); err != nil {
server.go Outdated
| Addr: conn.RemoteAddr().String(), | ||
| Args: args, | ||
| Context: ctx, | ||
| cmd = Command{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about doing cmd := Command{ here and get rid of the variable declaration above?
server.go Outdated
| } | ||
| | ||
| cmds := []Command{} | ||
| cmds = append(cmds, cmd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we could shorten the code by rewriting it as cmds := []Command{cmd}.
server_test.go Outdated
| defer cancel() | ||
| | ||
| err := cli.MultiExec(ctx, | ||
| Command{Cmd: "MULTI"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So how about making MULTI and EXEC automatic?
c644eb2 to 336e701 Compare | if len(addr) == 0 { | ||
| addr = "localhost:6379" | ||
| } | ||
| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can optimize the memory allocations here by rewriting the code as
multi := make([]Command, 0, len(cmds)+2) multi = append(multi, Command{Cmd: "MULTI"}) multi = append(multi, cmds...) multi = append(multi, Command{Cmd: "EXEC"})| addr := c.Addr | ||
| if len(addr) == 0 { | ||
| addr = "localhost:6379" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's check that the list of command doesn't contain any MULTI, EXEC or DISCARD.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should we do in that case ? drop the query or remove duplicate keys ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to drop the query and return an error, it's a misuse of the package.
request.go Outdated
| // For server request, Args is never nil, even if there are no values in | ||
| // the argument list. | ||
| Args Args | ||
| // Cmds is a lir of commands that are part of the transaction. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a lir ?
server.go Outdated
| | ||
| func (s *Server) serveRequest(res *responseWriter, req *Request) (err error) { | ||
| args := req.Args | ||
| cmds := req.Cmds |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this intermediary variable isn't useful, we could just write for _, cmd := range req.Cmds {
server.go Outdated
| args.Close() | ||
| } | ||
| default: | ||
| err = s.serveRedis(res, req) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is weird isn't it? It's gonna call serveRedis multiple times for the same request if there are many arguments.
31e503a to 1eb106a Compare | So this is mostly working except when we parse the response. Sending : Will return But we only have |
conn_test.go Outdated
| } | ||
| } | ||
| | ||
| func isInArray(list []string, val string) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function doesn't seem used.
response.go Outdated
| if err := enc.Encode(val); err != nil { | ||
| return err | ||
| for _, arg := range res.Args { | ||
| if arg != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this ever happen? It seems like it's a bad idea to allow nil arguments.
response.go Outdated
| return enc.Close() | ||
| } | ||
| | ||
| func (res *Response) CloseAllArgs() error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we just call this Close?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I was just lazy of writing a for loop multiple times :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the method is fine, I was just arguing on the method name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad :) Can changed that for sure !
dce0e01 to 0915048 Compare args.go Outdated
| err := args.dec.Err() | ||
| | ||
| if args.done != nil { | ||
| if args.done != nil && err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@achille-roussel not sure this one is correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, we have to notify the done channel even if there were no errors, otherwise each connection will only be able to handle one request, then will timeout.
4264009 to 47bff1f Compare 47bff1f to b7df68a Compare 073d590 to d3248b5 Compare d3248b5 to 9afcdf4 Compare 9afcdf4 to 1176824 Compare | Closing in favor of #26 Thanks for your hard work on that one man! |
No description provided.