Client manages client connections
(ctx *Context)
| 24 | |
| 25 | // Client manages client connections |
| 26 | func Client(ctx *Context) { |
| 27 | syntaxErr := "ERR Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME | PAUSE | REPLY)" |
| 28 | list := func(ctx *Context) { |
| 29 | now := time.Now() |
| 30 | var lines []string |
| 31 | clients := &ctx.Server.Clients |
| 32 | clients.Range(func(k, v interface{}) bool { |
| 33 | client := v.(*context.ClientContext) |
| 34 | if ctx.Client.Namespace != sysAdminNamespace && client.Namespace != ctx.Client.Namespace { |
| 35 | return true |
| 36 | } |
| 37 | age := now.Sub(client.Created) / time.Second |
| 38 | idle := now.Sub(client.Updated) / time.Second |
| 39 | flags := "N" |
| 40 | if client.Multi { |
| 41 | flags = "x" |
| 42 | } |
| 43 | |
| 44 | // id=2 addr=127.0.0.1:39604 fd=6 name= age=196 idle=2 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=client |
| 45 | line := fmt.Sprintf("id=%d addr=%s fd=%d name=%s age=%d idle=%d "+ |
| 46 | "flags=%s db=%d sub=%d psub=%d multi=%d qbuf=%d qbuf-free=%d obl=%d oll=%d omem=%d events=%s cmd=%s\n", |
| 47 | client.ID, client.RemoteAddr, 0, client.Name, age, idle, flags, client.DB.ID, 0, 0, len(client.Commands), |
| 48 | 0, 0, 0, 0, 0, "rw", client.LastCmd) |
| 49 | lines = append(lines, line) |
| 50 | return true |
| 51 | }) |
| 52 | resp.ReplyBulkString(ctx.Out, strings.Join(lines, "")) |
| 53 | } |
| 54 | getname := func(ctx *Context) { |
| 55 | name := ctx.Client.Name |
| 56 | if len(name) != 0 { |
| 57 | resp.ReplyBulkString(ctx.Out, name) |
| 58 | return |
| 59 | } |
| 60 | resp.ReplyNullBulkString(ctx.Out) |
| 61 | } |
| 62 | setname := func(ctx *Context) { |
| 63 | args := ctx.Args[1:] |
| 64 | if len(args) != 1 { |
| 65 | resp.ReplyError(ctx.Out, syntaxErr) |
| 66 | return |
| 67 | } |
| 68 | ctx.Client.Name = args[0] |
| 69 | resp.ReplySimpleString(ctx.Out, "OK") |
| 70 | } |
| 71 | pause := func(ctx *Context) { |
| 72 | if ctx.Client.Namespace != sysAdminNamespace { |
| 73 | resp.ReplyError(ctx.Out, "ERR client pause can be used by $sys.admin only") |
| 74 | return |
| 75 | } |
| 76 | args := ctx.Args[1:] |
| 77 | if len(args) != 1 { |
| 78 | resp.ReplyError(ctx.Out, syntaxErr) |
| 79 | return |
| 80 | } |
| 81 | msec, err := strconv.ParseInt(args[0], 10, 64) |
| 82 | if err != nil { |
| 83 | resp.ReplyError(ctx.Out, "ERR timeout is not an integer or out of range") |