Skip to content

Session

Daniel Carbone edited this page Nov 6, 2026 · 1 revision

Session

$consul->Session wraps Consul's Session API. Sessions are mostly used to hold locks in the KV store.

Create, renew, destroy

use DCarbone\PHPConsulAPI\Consul;
use DCarbone\PHPConsulAPI\Session\SessionEntry;

[$sid, $wm, $err] = $consul->Session->Create(sessionEntry: new SessionEntry(
    Name: 'my-lock',
    TTL: '15s',
    Behavior: Consul::SessionBehaviorRelease, // or Consul::SessionBehaviorDelete
));

// Renew before the TTL expires.  Returns the renewed entries.
[$entries, $wm, $err] = $consul->Session->Renew(id: $sid);

[$wm, $err] = $consul->Session->Destroy(id: $sid);

SessionEntry::$TTL is a string (e.g. '15s'), as Consul expects. LockDelay is a Time\Duration (see Durations and Time). By default, Create attaches the node's serfHealth check. Use CreateNoChecks to create a session with no health checks.

Inspecting sessions

[$info, $qm, $err] = $consul->Session->Info(id: $sid);   // SessionEntry[] (empty if not found)
echo $info[0]->Name . ' ' . $info[0]->TTL . ' ' . $info[0]->Behavior;

[$all, $qm, $err] = $consul->Session->List();
[$mine, $qm, $err] = $consul->Session->Node(node: 'my-node');

See Responses and Errors and Query and Write Options.

Clone this wiki locally