2014-04-30 09:47:17 -04:00
|
|
|
# Tcl client library - used by the Redis test
|
|
|
|
# Copyright (C) 2009-2014 Salvatore Sanfilippo
|
2009-11-03 05:28:37 -05:00
|
|
|
# Released under the BSD license like Redis itself
|
|
|
|
#
|
|
|
|
# Example usage:
|
|
|
|
#
|
|
|
|
# set r [redis 127.0.0.1 6379]
|
|
|
|
# $r lpush mylist foo
|
|
|
|
# $r lpush mylist bar
|
|
|
|
# $r lrange mylist 0 -1
|
|
|
|
# $r close
|
2010-04-07 07:55:06 -04:00
|
|
|
#
|
|
|
|
# Non blocking usage example:
|
|
|
|
#
|
|
|
|
# proc handlePong {r type reply} {
|
|
|
|
# puts "PONG $type '$reply'"
|
|
|
|
# if {$reply ne "PONG"} {
|
|
|
|
# $r ping [list handlePong]
|
|
|
|
# }
|
|
|
|
# }
|
2014-07-31 14:39:49 -04:00
|
|
|
#
|
2010-04-07 07:55:06 -04:00
|
|
|
# set r [redis]
|
|
|
|
# $r blocking 0
|
|
|
|
# $r get fo [list handlePong]
|
|
|
|
#
|
|
|
|
# vwait forever
|
2009-11-03 05:28:37 -05:00
|
|
|
|
2010-04-19 05:05:08 -04:00
|
|
|
package require Tcl 8.5
|
2009-11-03 05:28:37 -05:00
|
|
|
package provide redis 0.1
|
|
|
|
|
|
|
|
namespace eval redis {}
|
|
|
|
set ::redis::id 0
|
|
|
|
array set ::redis::fd {}
|
2014-06-18 09:52:14 -04:00
|
|
|
array set ::redis::addr {}
|
2010-04-07 07:55:06 -04:00
|
|
|
array set ::redis::blocking {}
|
2010-06-15 15:16:27 -04:00
|
|
|
array set ::redis::deferred {}
|
2021-07-04 12:43:58 -04:00
|
|
|
array set ::redis::readraw {}
|
2022-02-06 17:10:05 -05:00
|
|
|
array set ::redis::attributes {} ;# Holds the RESP3 attributes from the last call
|
2014-06-18 09:52:14 -04:00
|
|
|
array set ::redis::reconnect {}
|
2021-03-30 16:11:32 -04:00
|
|
|
array set ::redis::tls {}
|
2010-04-07 07:55:06 -04:00
|
|
|
array set ::redis::callback {}
|
|
|
|
array set ::redis::state {} ;# State in non-blocking reply reading
|
2010-04-08 09:56:21 -04:00
|
|
|
array set ::redis::statestack {} ;# Stack of states, for nested mbulks
|
2009-11-03 05:28:37 -05:00
|
|
|
|
2021-07-04 12:43:58 -04:00
|
|
|
proc redis {{server 127.0.0.1} {port 6379} {defer 0} {tls 0} {tlsoptions {}} {readraw 0}} {
|
2019-09-12 03:56:54 -04:00
|
|
|
if {$tls} {
|
|
|
|
package require tls
|
|
|
|
::tls::init \
|
|
|
|
-cafile "$::tlsdir/ca.crt" \
|
2020-12-11 11:31:40 -05:00
|
|
|
-certfile "$::tlsdir/client.crt" \
|
|
|
|
-keyfile "$::tlsdir/client.key" \
|
2019-09-12 04:10:22 -04:00
|
|
|
{*}$tlsoptions
|
2019-09-12 03:56:54 -04:00
|
|
|
set fd [::tls::socket $server $port]
|
|
|
|
} else {
|
|
|
|
set fd [socket $server $port]
|
|
|
|
}
|
2009-11-03 05:28:37 -05:00
|
|
|
fconfigure $fd -translation binary
|
|
|
|
set id [incr ::redis::id]
|
|
|
|
set ::redis::fd($id) $fd
|
2014-06-18 09:52:14 -04:00
|
|
|
set ::redis::addr($id) [list $server $port]
|
2010-04-07 07:55:06 -04:00
|
|
|
set ::redis::blocking($id) 1
|
2010-06-15 15:16:27 -04:00
|
|
|
set ::redis::deferred($id) $defer
|
2021-07-04 12:43:58 -04:00
|
|
|
set ::redis::readraw($id) $readraw
|
2014-06-18 09:52:14 -04:00
|
|
|
set ::redis::reconnect($id) 0
|
2021-03-30 16:11:32 -04:00
|
|
|
set ::redis::tls($id) $tls
|
2010-04-07 07:55:06 -04:00
|
|
|
::redis::redis_reset_state $id
|
2009-11-03 05:28:37 -05:00
|
|
|
interp alias {} ::redis::redisHandle$id {} ::redis::__dispatch__ $id
|
|
|
|
}
|
|
|
|
|
2014-06-18 09:52:14 -04:00
|
|
|
# This is a wrapper to the actual dispatching procedure that handles
|
|
|
|
# reconnection if needed.
|
2009-11-03 05:28:37 -05:00
|
|
|
proc ::redis::__dispatch__ {id method args} {
|
2014-06-18 09:52:14 -04:00
|
|
|
set errorcode [catch {::redis::__dispatch__raw__ $id $method $args} retval]
|
|
|
|
if {$errorcode && $::redis::reconnect($id) && $::redis::fd($id) eq {}} {
|
|
|
|
# Try again if the connection was lost.
|
|
|
|
# FIXME: we don't re-select the previously selected DB, nor we check
|
|
|
|
# if we are inside a transaction that needs to be re-issued from
|
|
|
|
# scratch.
|
|
|
|
set errorcode [catch {::redis::__dispatch__raw__ $id $method $args} retval]
|
|
|
|
}
|
|
|
|
return -code $errorcode $retval
|
|
|
|
}
|
|
|
|
|
|
|
|
proc ::redis::__dispatch__raw__ {id method argv} {
|
2009-11-03 05:28:37 -05:00
|
|
|
set fd $::redis::fd($id)
|
2014-06-18 09:52:14 -04:00
|
|
|
|
|
|
|
# Reconnect the link if needed.
|
2021-09-13 11:16:47 -04:00
|
|
|
if {$fd eq {} && $method ne {close}} {
|
2014-06-18 09:52:14 -04:00
|
|
|
lassign $::redis::addr($id) host port
|
2021-03-30 16:11:32 -04:00
|
|
|
if {$::redis::tls($id)} {
|
2019-09-12 03:56:54 -04:00
|
|
|
set ::redis::fd($id) [::tls::socket $host $port]
|
|
|
|
} else {
|
|
|
|
set ::redis::fd($id) [socket $host $port]
|
|
|
|
}
|
2014-06-18 09:52:14 -04:00
|
|
|
fconfigure $::redis::fd($id) -translation binary
|
|
|
|
set fd $::redis::fd($id)
|
|
|
|
}
|
|
|
|
|
2010-04-07 07:55:06 -04:00
|
|
|
set blocking $::redis::blocking($id)
|
2010-06-15 15:16:27 -04:00
|
|
|
set deferred $::redis::deferred($id)
|
2010-04-07 07:55:06 -04:00
|
|
|
if {$blocking == 0} {
|
2014-06-18 09:52:14 -04:00
|
|
|
if {[llength $argv] == 0} {
|
2010-04-07 07:55:06 -04:00
|
|
|
error "Please provide a callback in non-blocking mode"
|
|
|
|
}
|
2014-06-18 09:52:14 -04:00
|
|
|
set callback [lindex $argv end]
|
|
|
|
set argv [lrange $argv 0 end-1]
|
2010-04-07 07:55:06 -04:00
|
|
|
}
|
2009-11-03 05:28:37 -05:00
|
|
|
if {[info command ::redis::__method__$method] eq {}} {
|
2022-02-06 17:10:05 -05:00
|
|
|
catch {unset ::redis::attributes($id)}
|
2014-06-18 09:52:14 -04:00
|
|
|
set cmd "*[expr {[llength $argv]+1}]\r\n"
|
2010-10-15 09:50:29 -04:00
|
|
|
append cmd "$[string length $method]\r\n$method\r\n"
|
2014-06-18 09:52:14 -04:00
|
|
|
foreach a $argv {
|
2010-10-15 09:50:29 -04:00
|
|
|
append cmd "$[string length $a]\r\n$a\r\n"
|
2009-11-03 05:28:37 -05:00
|
|
|
}
|
2010-10-15 09:50:29 -04:00
|
|
|
::redis::redis_write $fd $cmd
|
2014-06-18 09:52:14 -04:00
|
|
|
if {[catch {flush $fd}]} {
|
2020-11-22 14:21:42 -05:00
|
|
|
catch {close $fd}
|
2014-06-18 09:52:14 -04:00
|
|
|
set ::redis::fd($id) {}
|
|
|
|
return -code error "I/O error reading reply"
|
|
|
|
}
|
2010-10-15 09:50:29 -04:00
|
|
|
|
2010-06-15 15:16:27 -04:00
|
|
|
if {!$deferred} {
|
|
|
|
if {$blocking} {
|
2014-06-18 09:52:14 -04:00
|
|
|
::redis::redis_read_reply $id $fd
|
2010-06-15 15:16:27 -04:00
|
|
|
} else {
|
|
|
|
# Every well formed reply read will pop an element from this
|
|
|
|
# list and use it as a callback. So pipelining is supported
|
|
|
|
# in non blocking mode.
|
|
|
|
lappend ::redis::callback($id) $callback
|
|
|
|
fileevent $fd readable [list ::redis::redis_readable $fd $id]
|
|
|
|
}
|
2010-04-07 07:55:06 -04:00
|
|
|
}
|
2009-11-03 05:28:37 -05:00
|
|
|
} else {
|
2014-06-18 09:52:14 -04:00
|
|
|
uplevel 1 [list ::redis::__method__$method $id $fd] $argv
|
2009-11-03 05:28:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-07 07:55:06 -04:00
|
|
|
proc ::redis::__method__blocking {id fd val} {
|
|
|
|
set ::redis::blocking($id) $val
|
|
|
|
fconfigure $fd -blocking $val
|
|
|
|
}
|
|
|
|
|
2014-06-18 09:52:14 -04:00
|
|
|
proc ::redis::__method__reconnect {id fd val} {
|
|
|
|
set ::redis::reconnect($id) $val
|
|
|
|
}
|
|
|
|
|
2010-06-15 15:16:27 -04:00
|
|
|
proc ::redis::__method__read {id fd} {
|
2014-06-18 09:52:14 -04:00
|
|
|
::redis::redis_read_reply $id $fd
|
2010-06-15 15:16:27 -04:00
|
|
|
}
|
|
|
|
|
2021-11-03 14:47:18 -04:00
|
|
|
proc ::redis::__method__rawread {id fd len} {
|
|
|
|
return [read $fd $len]
|
|
|
|
}
|
|
|
|
|
2010-10-13 05:25:40 -04:00
|
|
|
proc ::redis::__method__write {id fd buf} {
|
|
|
|
::redis::redis_write $fd $buf
|
|
|
|
}
|
|
|
|
|
|
|
|
proc ::redis::__method__flush {id fd} {
|
|
|
|
flush $fd
|
|
|
|
}
|
|
|
|
|
2009-11-03 05:28:37 -05:00
|
|
|
proc ::redis::__method__close {id fd} {
|
|
|
|
catch {close $fd}
|
|
|
|
catch {unset ::redis::fd($id)}
|
2014-06-18 09:52:14 -04:00
|
|
|
catch {unset ::redis::addr($id)}
|
2010-04-07 07:55:06 -04:00
|
|
|
catch {unset ::redis::blocking($id)}
|
2014-06-18 09:07:08 -04:00
|
|
|
catch {unset ::redis::deferred($id)}
|
2021-07-04 12:43:58 -04:00
|
|
|
catch {unset ::redis::readraw($id)}
|
2022-02-06 17:10:05 -05:00
|
|
|
catch {unset ::redis::attributes($id)}
|
2014-06-18 09:52:14 -04:00
|
|
|
catch {unset ::redis::reconnect($id)}
|
2021-03-30 16:11:32 -04:00
|
|
|
catch {unset ::redis::tls($id)}
|
2010-04-07 07:55:06 -04:00
|
|
|
catch {unset ::redis::state($id)}
|
2010-04-08 09:56:21 -04:00
|
|
|
catch {unset ::redis::statestack($id)}
|
2010-04-07 07:55:06 -04:00
|
|
|
catch {unset ::redis::callback($id)}
|
2009-11-03 05:28:37 -05:00
|
|
|
catch {interp alias {} ::redis::redisHandle$id {}}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc ::redis::__method__channel {id fd} {
|
|
|
|
return $fd
|
|
|
|
}
|
|
|
|
|
2013-05-30 12:54:28 -04:00
|
|
|
proc ::redis::__method__deferred {id fd val} {
|
|
|
|
set ::redis::deferred($id) $val
|
|
|
|
}
|
|
|
|
|
2021-07-04 12:43:58 -04:00
|
|
|
proc ::redis::__method__readraw {id fd val} {
|
|
|
|
set ::redis::readraw($id) $val
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:27:24 -04:00
|
|
|
proc ::redis::__method__readingraw {id fd} {
|
|
|
|
return $::redis::readraw($id)
|
|
|
|
}
|
|
|
|
|
2022-02-06 17:10:05 -05:00
|
|
|
proc ::redis::__method__attributes {id fd} {
|
|
|
|
set _ $::redis::attributes($id)
|
|
|
|
}
|
|
|
|
|
2009-11-03 05:28:37 -05:00
|
|
|
proc ::redis::redis_write {fd buf} {
|
|
|
|
puts -nonewline $fd $buf
|
|
|
|
}
|
|
|
|
|
|
|
|
proc ::redis::redis_writenl {fd buf} {
|
|
|
|
redis_write $fd $buf
|
|
|
|
redis_write $fd "\r\n"
|
|
|
|
flush $fd
|
|
|
|
}
|
|
|
|
|
|
|
|
proc ::redis::redis_readnl {fd len} {
|
|
|
|
set buf [read $fd $len]
|
|
|
|
read $fd 2 ; # discard CR LF
|
|
|
|
return $buf
|
|
|
|
}
|
|
|
|
|
|
|
|
proc ::redis::redis_bulk_read {fd} {
|
|
|
|
set count [redis_read_line $fd]
|
|
|
|
if {$count == -1} return {}
|
|
|
|
set buf [redis_readnl $fd $count]
|
|
|
|
return $buf
|
|
|
|
}
|
|
|
|
|
2014-06-18 09:52:14 -04:00
|
|
|
proc ::redis::redis_multi_bulk_read {id fd} {
|
2009-11-03 05:28:37 -05:00
|
|
|
set count [redis_read_line $fd]
|
|
|
|
if {$count == -1} return {}
|
|
|
|
set l {}
|
2012-04-06 17:52:28 -04:00
|
|
|
set err {}
|
2009-11-03 05:28:37 -05:00
|
|
|
for {set i 0} {$i < $count} {incr i} {
|
2012-04-06 17:52:28 -04:00
|
|
|
if {[catch {
|
2014-06-18 09:52:14 -04:00
|
|
|
lappend l [redis_read_reply $id $fd]
|
2012-04-06 17:52:28 -04:00
|
|
|
} e] && $err eq {}} {
|
|
|
|
set err $e
|
|
|
|
}
|
2009-11-03 05:28:37 -05:00
|
|
|
}
|
2012-04-06 17:52:28 -04:00
|
|
|
if {$err ne {}} {return -code error $err}
|
2009-11-03 05:28:37 -05:00
|
|
|
return $l
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:54:47 -05:00
|
|
|
proc ::redis::redis_read_map {id fd} {
|
|
|
|
set count [redis_read_line $fd]
|
|
|
|
if {$count == -1} return {}
|
2021-01-05 01:29:20 -05:00
|
|
|
set d {}
|
2020-11-09 15:54:47 -05:00
|
|
|
set err {}
|
|
|
|
for {set i 0} {$i < $count} {incr i} {
|
|
|
|
if {[catch {
|
2021-01-05 01:29:20 -05:00
|
|
|
set k [redis_read_reply $id $fd] ; # key
|
|
|
|
set v [redis_read_reply $id $fd] ; # value
|
|
|
|
dict set d $k $v
|
2020-11-09 15:54:47 -05:00
|
|
|
} e] && $err eq {}} {
|
|
|
|
set err $e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if {$err ne {}} {return -code error $err}
|
2021-01-05 01:29:20 -05:00
|
|
|
return $d
|
2020-11-09 15:54:47 -05:00
|
|
|
}
|
|
|
|
|
2009-11-03 05:28:37 -05:00
|
|
|
proc ::redis::redis_read_line fd {
|
|
|
|
string trim [gets $fd]
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:54:47 -05:00
|
|
|
proc ::redis::redis_read_null fd {
|
|
|
|
gets $fd
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
2021-07-14 12:14:31 -04:00
|
|
|
proc ::redis::redis_read_bool fd {
|
|
|
|
set v [redis_read_line $fd]
|
|
|
|
if {$v == "t"} {return 1}
|
|
|
|
if {$v == "f"} {return 0}
|
|
|
|
return -code error "Bad protocol, '$v' as bool type"
|
|
|
|
}
|
|
|
|
|
2021-08-03 04:37:19 -04:00
|
|
|
proc ::redis::redis_read_verbatim_str fd {
|
|
|
|
set v [redis_bulk_read $fd]
|
|
|
|
# strip the first 4 chars ("txt:")
|
|
|
|
return [string range $v 4 end]
|
|
|
|
}
|
|
|
|
|
2014-06-18 09:52:14 -04:00
|
|
|
proc ::redis::redis_read_reply {id fd} {
|
2021-07-04 12:43:58 -04:00
|
|
|
if {$::redis::readraw($id)} {
|
|
|
|
return [redis_read_line $fd]
|
|
|
|
}
|
|
|
|
|
2021-07-14 12:14:31 -04:00
|
|
|
while {1} {
|
|
|
|
set type [read $fd 1]
|
|
|
|
switch -exact -- $type {
|
|
|
|
_ {return [redis_read_null $fd]}
|
|
|
|
: -
|
|
|
|
( -
|
|
|
|
+ {return [redis_read_line $fd]}
|
|
|
|
, {return [expr {double([redis_read_line $fd])}]}
|
|
|
|
# {return [redis_read_bool $fd]}
|
2021-08-03 04:37:19 -04:00
|
|
|
= {return [redis_read_verbatim_str $fd]}
|
2021-07-14 12:14:31 -04:00
|
|
|
- {return -code error [redis_read_line $fd]}
|
|
|
|
$ {return [redis_bulk_read $fd]}
|
|
|
|
> -
|
|
|
|
~ -
|
|
|
|
* {return [redis_multi_bulk_read $id $fd]}
|
|
|
|
% {return [redis_read_map $id $fd]}
|
|
|
|
| {
|
2022-02-06 17:10:05 -05:00
|
|
|
set attrib [redis_read_map $id $fd]
|
|
|
|
set ::redis::attributes($id) $attrib
|
2021-07-14 12:14:31 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
if {$type eq {}} {
|
|
|
|
catch {close $fd}
|
|
|
|
set ::redis::fd($id) {}
|
|
|
|
return -code error "I/O error reading reply"
|
|
|
|
}
|
|
|
|
return -code error "Bad protocol, '$type' as reply type byte"
|
2014-06-18 09:52:14 -04:00
|
|
|
}
|
2014-05-13 18:14:35 -04:00
|
|
|
}
|
2009-11-03 05:28:37 -05:00
|
|
|
}
|
|
|
|
}
|
2010-04-07 07:55:06 -04:00
|
|
|
|
|
|
|
proc ::redis::redis_reset_state id {
|
|
|
|
set ::redis::state($id) [dict create buf {} mbulk -1 bulk -1 reply {}]
|
2010-04-08 09:56:21 -04:00
|
|
|
set ::redis::statestack($id) {}
|
2010-04-07 07:55:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
proc ::redis::redis_call_callback {id type reply} {
|
|
|
|
set cb [lindex $::redis::callback($id) 0]
|
|
|
|
set ::redis::callback($id) [lrange $::redis::callback($id) 1 end]
|
|
|
|
uplevel #0 $cb [list ::redis::redisHandle$id $type $reply]
|
|
|
|
::redis::redis_reset_state $id
|
|
|
|
}
|
|
|
|
|
|
|
|
# Read a reply in non-blocking mode.
|
|
|
|
proc ::redis::redis_readable {fd id} {
|
|
|
|
if {[eof $fd]} {
|
|
|
|
redis_call_callback $id eof {}
|
|
|
|
::redis::__method__close $id $fd
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if {[dict get $::redis::state($id) bulk] == -1} {
|
|
|
|
set line [gets $fd]
|
|
|
|
if {$line eq {}} return ;# No complete line available, return
|
|
|
|
switch -exact -- [string index $line 0] {
|
|
|
|
: -
|
|
|
|
+ {redis_call_callback $id reply [string range $line 1 end-1]}
|
|
|
|
- {redis_call_callback $id err [string range $line 1 end-1]}
|
Unified Lua and modules reply parsing and added RESP3 support to RM_Call (#9202)
## Current state
1. Lua has its own parser that handles parsing `reds.call` replies and translates them
to Lua objects that can be used by the user Lua code. The parser partially handles
resp3 (missing big number, verbatim, attribute, ...)
2. Modules have their own parser that handles parsing `RM_Call` replies and translates
them to RedisModuleCallReply objects. The parser does not support resp3.
In addition, in the future, we want to add Redis Function (#8693) that will probably
support more languages. At some point maintaining so many parsers will stop
scaling (bug fixes and protocol changes will need to be applied on all of them).
We will probably end up with different parsers that support different parts of the
resp protocol (like we already have today with Lua and modules)
## PR Changes
This PR attempt to unified the reply parsing of Lua and modules (and in the future
Redis Function) by introducing a new parser unit (`resp_parser.c`). The new parser
handles parsing the reply and calls different callbacks to allow the users (another
unit that uses the parser, i.e, Lua, modules, or Redis Function) to analyze the reply.
### Lua API Additions
The code that handles reply parsing on `scripting.c` was removed. Instead, it uses
the resp_parser to parse and create a Lua object out of the reply. As mentioned
above the Lua parser did not handle parsing big numbers, verbatim, and attribute.
The new parser can handle those and so Lua also gets it for free.
Those are translated to Lua objects in the following way:
1. Big Number - Lua table `{'big_number':'<str representation for big number>'}`
2. Verbatim - Lua table `{'verbatim_string':{'format':'<verbatim format>', 'string':'<verbatim string value>'}}`
3. Attribute - currently ignored and not expose to the Lua parser, another issue will be open to decide how to expose it.
Tests were added to check resp3 reply parsing on Lua
### Modules API Additions
The reply parsing code on `module.c` was also removed and the new resp_parser is used instead.
In addition, the RedisModuleCallReply was also extracted to a separate unit located on `call_reply.c`
(in the future, this unit will also be used by Redis Function). A nice side effect of unified parsing is
that modules now also support resp3. Resp3 can be enabled by giving `3` as a parameter to the
fmt argument of `RM_Call`. It is also possible to give `0`, which will indicate an auto mode. i.e, Redis
will automatically chose the reply protocol base on the current client set on the RedisModuleCtx
(this mode will mostly be used when the module want to pass the reply to the client as is).
In addition, the following RedisModuleAPI were added to allow analyzing resp3 replies:
* New RedisModuleCallReply types:
* `REDISMODULE_REPLY_MAP`
* `REDISMODULE_REPLY_SET`
* `REDISMODULE_REPLY_BOOL`
* `REDISMODULE_REPLY_DOUBLE`
* `REDISMODULE_REPLY_BIG_NUMBER`
* `REDISMODULE_REPLY_VERBATIM_STRING`
* `REDISMODULE_REPLY_ATTRIBUTE`
* New RedisModuleAPI:
* `RedisModule_CallReplyDouble` - getting double value from resp3 double reply
* `RedisModule_CallReplyBool` - getting boolean value from resp3 boolean reply
* `RedisModule_CallReplyBigNumber` - getting big number value from resp3 big number reply
* `RedisModule_CallReplyVerbatim` - getting format and value from resp3 verbatim reply
* `RedisModule_CallReplySetElement` - getting element from resp3 set reply
* `RedisModule_CallReplyMapElement` - getting key and value from resp3 map reply
* `RedisModule_CallReplyAttribute` - getting a reply attribute
* `RedisModule_CallReplyAttributeElement` - getting key and value from resp3 attribute reply
* New context flags:
* `REDISMODULE_CTX_FLAGS_RESP3` - indicate that the client is using resp3
Tests were added to check the new RedisModuleAPI
### Modules API Changes
* RM_ReplyWithCallReply might return REDISMODULE_ERR if the given CallReply is in resp3
but the client expects resp2. This is not a breaking change because in order to get a resp3
CallReply one needs to specifically specify `3` as a parameter to the fmt argument of
`RM_Call` (as mentioned above).
Tests were added to check this change
### More small Additions
* Added `debug set-disable-deny-scripts` that allows to turn on and off the commands no-script
flag protection. This is used by the Lua resp3 tests so it will be possible to run `debug protocol`
and check the resp3 parsing code.
Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Yossi Gottlieb <yossigo@gmail.com>
2021-08-04 09:28:07 -04:00
|
|
|
( {redis_call_callback $id reply [string range $line 1 end-1]}
|
2010-04-07 07:55:06 -04:00
|
|
|
$ {
|
|
|
|
dict set ::redis::state($id) bulk \
|
|
|
|
[expr [string range $line 1 end-1]+2]
|
|
|
|
if {[dict get $::redis::state($id) bulk] == 1} {
|
|
|
|
# We got a $-1, hack the state to play well with this.
|
|
|
|
dict set ::redis::state($id) bulk 2
|
|
|
|
dict set ::redis::state($id) buf "\r\n"
|
|
|
|
::redis::redis_readable $fd $id
|
|
|
|
}
|
|
|
|
}
|
2010-04-08 09:56:21 -04:00
|
|
|
* {
|
|
|
|
dict set ::redis::state($id) mbulk [string range $line 1 end-1]
|
|
|
|
# Handle *-1
|
|
|
|
if {[dict get $::redis::state($id) mbulk] == -1} {
|
|
|
|
redis_call_callback $id reply {}
|
|
|
|
}
|
|
|
|
}
|
2010-04-07 07:55:06 -04:00
|
|
|
default {
|
|
|
|
redis_call_callback $id err \
|
|
|
|
"Bad protocol, $type as reply type byte"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
set totlen [dict get $::redis::state($id) bulk]
|
|
|
|
set buflen [string length [dict get $::redis::state($id) buf]]
|
|
|
|
set toread [expr {$totlen-$buflen}]
|
|
|
|
set data [read $fd $toread]
|
|
|
|
set nread [string length $data]
|
|
|
|
dict append ::redis::state($id) buf $data
|
|
|
|
# Check if we read a complete bulk reply
|
|
|
|
if {[string length [dict get $::redis::state($id) buf]] ==
|
|
|
|
[dict get $::redis::state($id) bulk]} {
|
|
|
|
if {[dict get $::redis::state($id) mbulk] == -1} {
|
|
|
|
redis_call_callback $id reply \
|
|
|
|
[string range [dict get $::redis::state($id) buf] 0 end-2]
|
|
|
|
} else {
|
|
|
|
dict with ::redis::state($id) {
|
|
|
|
lappend reply [string range $buf 0 end-2]
|
|
|
|
incr mbulk -1
|
|
|
|
set bulk -1
|
|
|
|
}
|
|
|
|
if {[dict get $::redis::state($id) mbulk] == 0} {
|
|
|
|
redis_call_callback $id reply \
|
|
|
|
[dict get $::redis::state($id) reply]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|