> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/dpr/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/dpr/latest/configuration/endpoints-watcher.md).

# Peer availability tracking

Diameter Proxy (DPR) has a small additional container named `endpoints_watcher`. It does not participate in the Diameter communication itself. Instead, it reflects the operational state of Kubernetes-based Diameter peers that connect to DPR and notifies DPR about the changes.

On its own, DPR only sees the Diameter connection towards a peer. The connection may still look alive even though the peer on the other side is already terminated. In that case, DPR keeps the connection open and keeps trying to send messages into it until the Diameter watchdog or transport timeout detects the failure. In Kubernetes, however, this information is available in advance, based on the readiness of the peer's `Endpoints`. It reflects that the peer is about to become unavailable, for example, due to an upgrade, restart, or scale-down. The `endpoints_watcher` makes this information available to DPR by notifying it whenever a peer's readiness changes.

As a result, DPR can stop sending Diameter requests towards such peers as early as possible. The established connection is not terminated, so in-flight requests can still complete. Once Kubernetes considers the peer healthy again, the `endpoints_watcher` reactivates it in DPR, and the peer resumes receiving requests.

The most common use case for this mechanism is a graceful, hitless update of the Diameter agents where DPR forwards requests.

You can enable the `endpoints_watcher` with the following configuration option:

```yaml
endpoints_watcher:
  # Enables the endpoints_watcher sidecar container.
  enabled: true
  # Container image repository.
  image: registry.cennso.com/cennso/diameter_proxy/endpoints_watcher
  # Container image tag.
  tag: v1.3.0
```

The `endpoints_watcher` runs as a sidecar container in the same pod as DPR. It watches the [Kubernetes `Endpoints`](https://kubernetes.io/docs/reference/kubernetes-api/core/endpoints-v1/) objects that have the `diameter-server` label in the same namespace where it is deployed. Periodically, it reports each peer's readiness to DPR over DPR's HTTP API. The HTTP API has two operations:

| Request | Effect on DPR |
| --- | --- |
| `POST /endpoint/<name>/down` | Marks the peer `<name>` as unavailable. DPR stops selecting it when relaying new requests. |
| `POST /endpoint/<name>/up` | Clears the "down" mark for `<name>`. DPR may select it again. |

Since events from the Kubernetes API can be missing, the `endpoints_watcher` also periodically re-reports the state of all watched endpoints to DPR. You can tune the resync interval and the retry behaviour of notifications to DPR using these configuration options:

```yaml
endpoints_watcher:
  # Period in seconds which configures how often the watcher reports the current state of all watched endpoints to DPR.
  # Set it to `0` to disable periodic resync.
  resyncSeconds: 30
  notify:
    # The number of times the watcher retries a single notification to DPR in a case of a failure. The value must be greater than or equal to `1`.
    maxAttempts: 5
    # The number of seconds the watcher waits before it retries to send a notification.
    retryDelaySeconds: 2
    # The timeout (in seconds) for sending a notification to DPR. The value must be greater than or equal to `1`.
    timeoutSeconds: 5
```

You can also configure the [Kubernetes resources](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/) for the `endpoints_watcher` container under `resources.endpoints_watcher`:

```yaml
resources:
  endpoints_watcher:
    requests:
      # Container CPU requests.
      cpu: 250m
      # Container memory requests.
      memory: 256Mi
    limits:
      # Container CPU limit.
      cpu: 250m
      # Container memory limit.
      memory: 256Mi
```
