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

Factor out start-a-libp2p-daemon! #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,42 @@ want to reuse a running daemon.
=> <daemon>
```

Ensures that a daemon is running, and starts it if there isn't one.
Starts a new daemon running without regard to `current-libp2p-daemon`.
If a daemon is already running at `current-libp2p-daemon`,
this ignores that and creates a new one anyway.

#### start-the-libp2p-daemon!
```
(start-the-libp2p-daemon! [daemon: (daemon "p2pd")]
[options: (options [])]
[address: (address #f)]
[wait: (wait .4)])
daemon := string; the daemon executable
options := list of strings; options to pass to the daemon
address := daemon unix socket path
wait := how long to wait for daemon to initialize
=> <daemon>
```

Ensures that a daemon is running at `current-libp2p-daemon`,
and starts and sets it only if there isn't one already.

#### stop-libp2p-daemon!
```
(stop-libp2p-daemon! [daemon = (current-libp2p-daemon)])
```

Kills the libp2p daemon if it was started with `start-libp2p-daemon!`.
Kills the given libp2p daemon if it was started with `start-libp2p-daemon!`.
This does not change the value inside `current-libp2p-daemon`,
even if the `daemon` argument is not provided or is equal to that value.

#### stop-the-libp2p-daemon!
```
(stop-the-libp2p-daemon!)
```

Kills the libp2p daemon at `current-libp2p-daemon`,
and sets the `current-libp2p-daemon` to false.

#### use-libp2p-daemon!
```
Expand All @@ -85,7 +113,7 @@ Kills the libp2p daemon if it was started with `start-libp2p-daemon!`.
=> <daemon>
```

Sets the current daemon to an external process with `path` as the unix control
Sets the `current-libp2p-daemon` to an external process with `path` as the unix control
socket path.


Expand Down
2 changes: 1 addition & 1 deletion example/dht-crawl.ss
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
(lambda (file)
(start-logger!)
(debugf "Starting p2pd")
(start-libp2p-daemon! host-addresses: addresses options: ["-b" "-dhtClient" "-connManager"] wait: 10)
(start-the-libp2p-daemon! host-addresses: addresses options: ["-b" "-dhtClient" "-connManager"] wait: 10)
(debugf "Starting indefinite crawl; output to ~a" filename)
(crawl! file workers addresses))))

Expand Down
2 changes: 1 addition & 1 deletion example/libp2p-chat-with-circuit-relay.ss
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@

;;start a libp2p daemon that allows for circuit relays
(def (do-circuit-relay host-addresses)
(let* ((d (start-libp2p-daemon! host-addresses: host-addresses options: ["-relayHop"] wait: 20))
(let* ((d (start-the-libp2p-daemon! host-addresses: host-addresses options: ["-relayHop"] wait: 20))
(c (open-libp2p-client host-addresses: host-addresses wait: 20)))
(for (p (peer-info->string* (libp2p-identify c)))
(displayln "I am " p))
Expand Down
2 changes: 1 addition & 1 deletion example/pubsub-chat.ss
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
(displayln "starting up")

;start the daemon with custom arguments -pubsub and -connManager to enable pubsub
(start-libp2p-daemon! host-addresses: host-addresses options: ["-pubsub" "-connManager"] wait: 10)
(start-the-libp2p-daemon! host-addresses: host-addresses options: ["-pubsub" "-connManager"] wait: 10)

;open a client with the given host-address, it will use the daemon already running
(let* ((c (open-libp2p-client host-addresses: host-addresses wait: 10))
Expand Down
2 changes: 2 additions & 0 deletions libp2p.ss
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
(export
;; :vyzo/libp2p/daemon
current-libp2p-daemon
start-the-libp2p-daemon!
start-libp2p-daemon!
stop-libp2p-daemon!
stop-the-libp2p-daemon!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it start-a- and stop-the- ?

Copy link
Contributor Author

@AlexKnauth AlexKnauth Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If backwards compatibility is a concern,
Current behavior has:

  • start-libp2p-daemon! for "the" daemon in current-libp2p-daemon
  • stop-libp2p-daemon! for "a" daemon, where current-libp2p-daemon doesn't matter

A better design would have:

  • start-libp2p-daemon! for "a" daemon
  • stop-libp2p-daemon! for "a" daemon
  • start-the-libp2p-daemon! for "the" daemon in current-libp2p-daemon
  • stop-the-libp2p-daemon! for "the" daemon in current-libp2p-daemon

The closest thing I could think of to that while maintaining backwards compatibility has:

  • start-a-libp2p-daemon! for "a daemon"
  • stop-libp2p-daemon! for "a" daemon as dictated by existing code
  • start-libp2p-daemon! for "the" daemon as dictated by existing code
  • stop-the-libp2p-daemon! for "the" daemon in current-libp2p-daemon

If backwards compatibility is not a concern, I could make it more consistent with only "the" and no "a", but I won't do that unless @vyzo says that's what he wants.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've changed the convention to make it more consistent with only "the" and no "a", and also added some documentation.

use-libp2p-daemon!
;; :vyzo/libp2p/client
libp2p-error?
Expand Down
2 changes: 1 addition & 1 deletion libp2p/client.ss
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
(raise e)))))

(def (open-libp2p-client host-addresses: (host-addresses #f) options: (args []) address: (sock #f) wait: (timeo 12) (path #f)) ;; Extra arguments host-address and options
(let (d (start-libp2p-daemon! host-addresses: host-addresses options: args address: sock wait: timeo)) ;; Should go with host-address/tranpsort/port
(let (d (start-the-libp2p-daemon! host-addresses: host-addresses options: args address: sock wait: timeo)) ;; Should go with host-address/tranpsort/port
(make-client d (make-mutex 'libp2p-client) (make-hash-table) path #f #f)))

(def (open-stream c bufsz)
Expand Down
29 changes: 23 additions & 6 deletions libp2p/daemon.ss
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,28 @@
(def current-libp2p-daemon
(make-parameter #f))

(def (start-libp2p-daemon! host-addresses: (host-addrs #f) daemon: (bin "p2pd")
options: (args [])
address: (sock #f)
wait: (timeo 0.4))
;; Starts a new libp2p-daemon only if there is no existing current-libp2p-daemon,
;; and if so sets the current-libp2p-daemon to the new one.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that should be renamed ensure-libp2p-daemon ?
Or, if we want backward-compatibility, a force: or new: boolean flag?

(def (start-the-libp2p-daemon! host-addresses: (host-addrs #f) daemon: (bin "p2pd")
options: (args [])
address: (sock #f)
wait: (timeo 0.4))
(cond
((current-libp2p-daemon)
=> values)
(else
(let ((d (start-libp2p-daemon! host-addresses: host-addrs daemon: bin
options: args
address: sock
wait: timeo)))
(current-libp2p-daemon d)
d))))

;; Starts a libp2p-daemon without regard to current-libp2p-daemon
(def (start-libp2p-daemon! host-addresses: (host-addrs #f) daemon: (bin "p2pd")
options: (args [])
address: (sock #f)
wait: (timeo 0.4))
(let* ((path (or sock (string-append "/tmp/p2pd." (number->string (getpid)) ".sock")))
(addr (string-append "/unix" path))
(proc (if host-addrs
Expand All @@ -32,8 +46,7 @@
((process-status proc timeo #f)
=> (lambda (status)
(error "p2pd exited prematurely" status))))
(current-libp2p-daemon d)
d))))
d))

(def (stop-libp2p-daemon! (d (current-libp2p-daemon)))
(with ((daemon proc path) d)
Expand All @@ -45,5 +58,9 @@
(delete-file path))
status))))

(def (stop-the-libp2p-daemon!)
(stop-libp2p-daemon!)
(current-libp2p-daemon #f))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't stop-libp2p-daemon! always call (current-libp2p-daemon #f) ? And then this function becomes unnecessary?


(def (use-libp2p-daemon! path)
(current-libp2p-daemon (daemon #f path)))