Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Discovery API #12

Closed
wants to merge 14 commits into from
Closed

WIP: Discovery API #12

wants to merge 14 commits into from

Conversation

lmangani
Copy link
Collaborator

@lmangani lmangani commented Nov 14, 2024

Discovery API

⚠️ This is just an experiment and will most likely end up in a different extension

The optional discovery API offers an easy hook for servers to advertise and discover each other (flock)

Endpoints

  • POST /subscribe/{secretHash} - Register a peer
  • GET /discovery/{secretHash} - Get list of active peers
  • DELETE /unsubscribe/{secretHash}/{peerId} - Remove peer

Start Server w. DUCKDB_HTTPSERVER_DISCOVERY=1

D SELECT httpserve_start('0.0.0.0',9999, '');
┌──────────────────────────────────────┐
│ httpserve_start('0.0.0.0', 9999, '') │
│               varchar                │
├──────────────────────────────────────┤
│ HTTP server started on 0.0.0.0:9999  │
└──────────────────────────────────────┘

Register a Peer under a secret hash

CURL

curl -X POST "https://localhost:9999/subscribe/secretHash" \
  -H "Content-Type: application/json" \
  -d '{ "name": "service1", "endpoint": "http://192.168.1.100:8080", "ttl": 300 }

SQL

INSTALL http_client FROM community; LOAD http_client; LOAD json;
WITH __input AS (
    SELECT
      http_post(
          'http://localhost:9999/subscribe/secretHash',
          headers => MAP {
          },
          params => MAP {
            'name': 'quackpipe1',
            'endpoint': 'https://1.1.1.1',
          }
      ) AS res
  ) SELECT res->>'reason' as res, res->>'status' as status FROM __input;

Check peers table

D SELECT name, endpoint, source_address as sourceAddress, peer_id as peerId, metadata, ttl, strftime(registered_at, '%Y-%m-%d %H:%M:%S') as registered_at FROM peers WHERE hash = 'secretHash';
┌──────────┬───────────────────────────┬───────────────┬──────────────────────────────────┬──────────┬───────┬─────────────────────┐
│   name   │         endpoint          │ sourceAddress │              peerId              │ metadata │  ttl  │    registered_at    │
│ varcharvarcharvarcharvarcharvarchar  │ int64 │       varchar       │
├──────────┼───────────────────────────┼───────────────┼──────────────────────────────────┼──────────┼───────┼─────────────────────┤
│ service1 │ http://192.168.1.100:8080xxx.xx.xx.xxx │ 0872c98634ce7e608e19aa1a1e6cf784 │ {}       │   3002024-11-14 19:44:23 │
└──────────┴───────────────────────────┴───────────────┴──────────────────────────────────┴──────────┴───────┴─────────────────────┘

Discover Peers

CURL

curl "http://localhost:9999/discovery/secretHash"

SQL

D SELECT * FROM read_ndjson_auto('http://localhost:9999/discovery/secretHash');
┌──────────┬──────────────────────┬────────────────┬──────────────────────────────┬──────────┬─────────┬─────────────────────┐
│   name   │       endpoint       │ source_address │           peer_id            │ metadata │   ttl   │    registered_at    │
│ varcharvarcharvarchar     │             uuid             │ varcharvarchartimestamp      │
├──────────┼──────────────────────┼────────────────┼──────────────────────────────┼──────────┼─────────┼─────────────────────┤
│ service1 │ http://192.168.1.1…  │ 127.0.0.1      │ 0872c986-34ce-7e60-8e19-aa…  │          │ 36002024-11-15 14:13:50 │
└──────────┴──────────────────────┴────────────────┴──────────────────────────────┴──────────┴─────────┴─────────────────────┘
D

⚠️ minor issue with peer_id being a UUID and python hating it

@lmangani lmangani marked this pull request as ready for review November 16, 2024 15:38
@lmangani lmangani changed the title WIP: Discovery API Discovery API Nov 16, 2024
@lmangani lmangani changed the title Discovery API WIP: Discovery API Dec 15, 2024
@lmangani lmangani closed this Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants