|
| 1 | +""" |
| 2 | +Connection timeout using the `connection_timeout` parameter on |
| 3 | +`ServerConnectInfo`. Connects to a configurable host and port with a 3-second |
| 4 | +timeout. If the server is unreachable within the timeout, the session reports |
| 5 | +`ConnectionFailedTimeout` via `pg_session_connection_failed`. |
| 6 | +""" |
| 7 | +use "cli" |
| 8 | +use "collections" |
| 9 | +use "constrained_types" |
| 10 | +use lori = "lori" |
| 11 | +// in your code this `use` statement would be: |
| 12 | +// use "postgres" |
| 13 | +use "../../postgres" |
| 14 | + |
| 15 | +actor Main |
| 16 | + new create(env: Env) => |
| 17 | + let server_info = ServerInfo(env.vars) |
| 18 | + let auth = lori.TCPConnectAuth(env.root) |
| 19 | + |
| 20 | + let client = Client(auth, server_info, env.out) |
| 21 | + |
| 22 | +actor Client is SessionStatusNotify |
| 23 | + let _session: Session |
| 24 | + let _out: OutStream |
| 25 | + |
| 26 | + new create(auth: lori.TCPConnectAuth, info: ServerInfo, out: OutStream) => |
| 27 | + _out = out |
| 28 | + match lori.MakeConnectionTimeout(3000) |
| 29 | + | let ct: lori.ConnectionTimeout => |
| 30 | + _out.print("Connecting with 3-second timeout...") |
| 31 | + _session = Session( |
| 32 | + ServerConnectInfo(auth, info.host, info.port |
| 33 | + where connection_timeout' = ct), |
| 34 | + DatabaseConnectInfo(info.username, info.password, info.database), |
| 35 | + this) |
| 36 | + | let _: ValidationFailure => |
| 37 | + _out.print("Failed to create connection timeout.") |
| 38 | + _session = Session( |
| 39 | + ServerConnectInfo(auth, info.host, info.port), |
| 40 | + DatabaseConnectInfo(info.username, info.password, info.database), |
| 41 | + this) |
| 42 | + end |
| 43 | + |
| 44 | + be pg_session_connected(session: Session) => |
| 45 | + _out.print("Connected.") |
| 46 | + |
| 47 | + be pg_session_connection_failed(s: Session, |
| 48 | + reason: ConnectionFailureReason) |
| 49 | + => |
| 50 | + match reason |
| 51 | + | ConnectionFailedTimeout => |
| 52 | + _out.print("Connection timed out.") |
| 53 | + | ConnectionFailedDNS => |
| 54 | + _out.print("DNS resolution failed.") |
| 55 | + | ConnectionFailedTCP => |
| 56 | + _out.print("TCP connection failed.") |
| 57 | + | SSLServerRefused => |
| 58 | + _out.print("SSL refused by server.") |
| 59 | + | TLSAuthFailed => |
| 60 | + _out.print("TLS authentication failed.") |
| 61 | + | TLSHandshakeFailed => |
| 62 | + _out.print("TLS handshake failed.") |
| 63 | + end |
| 64 | + |
| 65 | + be pg_session_authenticated(session: Session) => |
| 66 | + _out.print("Authenticated.") |
| 67 | + session.close() |
| 68 | + |
| 69 | + be pg_session_authentication_failed( |
| 70 | + s: Session, |
| 71 | + reason: AuthenticationFailureReason) |
| 72 | + => |
| 73 | + _out.print("Failed to authenticate.") |
| 74 | + |
| 75 | +class val ServerInfo |
| 76 | + let host: String |
| 77 | + let port: String |
| 78 | + let username: String |
| 79 | + let password: String |
| 80 | + let database: String |
| 81 | + |
| 82 | + new val create(vars: (Array[String] val | None)) => |
| 83 | + let e = EnvVars(vars) |
| 84 | + host = try e("POSTGRES_HOST")? else "127.0.0.1" end |
| 85 | + port = try e("POSTGRES_PORT")? else "5432" end |
| 86 | + username = try e("POSTGRES_USERNAME")? else "postgres" end |
| 87 | + password = try e("POSTGRES_PASSWORD")? else "postgres" end |
| 88 | + database = try e("POSTGRES_DATABASE")? else "postgres" end |
0 commit comments