-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
49 lines (38 loc) · 1.56 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package mux
import (
"context"
"github.com/isobit/cli"
)
type MuxCommand struct {
Database string `cli:"required,short=d,env=PGT_DATABASE,help=database connection string"`
ListenAddr string `cli:"required,short=l"`
}
func (*MuxCommand) SetupCommand(cmd *cli.Command) {
cmd.SetHelp("proxy that can multiplex connections/sessions")
cmd.SetDescription(`
NOTE: This command is experimental and likely to have some bugs.
Proxies PostgreSQL protocol TCP connections to the specified listen
address to the specified upstream database.
Connections can be multiplexed into the same upstream
connection/session by specifying the same session ID. Database names
passed by clients will be ignored (they will always proxy to the same
specified upstream database), unless they contain "@", in which case
the substring following "@" will be used as the session ID. If no
session ID is specified, it defaults to the client address, e.g.
"127.0.0.1:12345".
This is particularly useful for debugging applications mid-transaction;
to inspect the state of the database as seen by an application
transaction, simply connect to the same session using psql.
Example using an explicit session key:
$ pgt mux -d postgres://localhost:5432/postgres -l localhost:5433
# These will both connect to the same session:
$ psql postgres://localhost:5433/@foo
$ psql postgres://localhost:5433/@foo
`)
}
func NewMuxCommand() *MuxCommand {
return &MuxCommand{}
}
func (cmd *MuxCommand) Run(ctx context.Context) error {
return Listen(ctx, cmd.Database, cmd.ListenAddr)
}