Why namespace inside a go block changes? - clojure

(ns myapp.core
(:require
[clojure.core.async :refer [>! <! >!! <!! chan go-loop]])
(:gen-class))
(def this-ns *ns*)
(defn get-ns [c0]
(go-loop []
(let [x (<! c0)]
(println (str "this ns -> " this-ns))
(println (str "go block ns -> " *ns*)))
(recur)))
(defn -main [& args]
(let [c0 (chan)]
(get-ns c0)
(>!! c0 1)))
Output:
this ns -> myapp.core
go block ns -> user
Can anyone explain why the namespace is different inside the go block?

I don' think this has to do with core.async. It's just how *ns* works - it contains the current repls current namespace
clojure.core/*ns*
A clojure.lang.Namespace object representing the current namespace.
in other words, *ns* is dependent on the namespace you are calling from
Example:
; <current-ns>=> <code>
user=> (load-file "yourcode.clj")
#'myapp.core/-main
user=> (myapp.core/-main)
this ns -> myapp.core
go block ns -> user
true
user=> (in-ns 'myapp.core) ; <-- switching namespace
#object[clojure.lang.Namespace 0x679e26fe "myapp.core"]
myapp.core=> (-main)
this ns -> myapp.core
go block ns -> myapp.core
true

Related

How to test a function created with intern in clojure?

I use "intern" function for generate runtime funtions, but I can't test generted function.
In core.clj I defined functions that generate runtime functions like this.
(ns cftf.core)
(defn- generation-function [function-name ratio]
(let [function-symbol (symbol function-name)]
(intern 'cftf.core function-symbol (fn [input] (cftf.core/convert ratio input)))))
...
(defn generate-function-by-units [units]
(->> units
(units->function-name-and-ratio)
(map #(generation-function (first %) (second %)))))
I wrote the following test code.
(ns cftf.core-test
(:require [clojure.test :refer :all]
[cftf.core :as c]))
(defn amount-of-alcohol-per-one-bottle [bottle-size abv]
(* bottle-size (/ abv 100)))
(def alcohol-units
{:soju (amount-of-alcohol-per-one-bottle 360 16.5)
:beer (amount-of-alcohol-per-one-bottle 500 5)
:whisky (amount-of-alcohol-per-one-bottle 700 40)
})
(c/generate-function-by-units alcohol-units)
(deftest a-test
(testing "soju->beer test"
(is (= (int (c/soju->beer 1)) 2))))
After calling "generate-function-by-units" in the repl, you can use the "soju->beer" function.
But in the test I get the error "No such var: c/soju->beer".
I've tried using a fixture or calling " generate-function-by-units" the top in test.clj, but I'm still getting the error.

Why do I have memory leak for the following code with channel sub/unsub?

I am using [org.clojure/clojure "1.10.1"],[org.clojure/core.async "1.2.603"] and the latest Amazon Corretto 11 JVM if there was anything to do with them.
The following code is a simplified version of the code used in production and it does cause memory leak. I have no idea why that happened but suspect it might due to sub/unsub of channels. Can anyone help point out where my code may go wrong or how I can fix the memory leak?
(ns test-gc.core
(:require [clojure.core.async :as a :refer [chan put! close! <! go >! go-loop timeout]])
(:import [java.util UUID]))
(def global-msg-ch (chan (a/sliding-buffer 200)))
(def global-msg-pub (a/pub global-msg-ch :id))
(defn io-promise []
(let [id (UUID/randomUUID)
ch (chan)]
(a/sub global-msg-pub id ch)
[id (go
(let [x (<! ch)]
(a/unsub global-msg-pub id ch)
(:data x)))]))
(defn -main []
(go-loop []
(<! (timeout 1))
(let [[pid pch] (io-promise)
cmd {:id pid
:data (rand-int 1E5)}]
(>! global-msg-ch cmd)
(println (<! pch)))
(recur))
(while true
(Thread/yield)))
A quick heap dump gives the following statistics for example:
Class by number of instances
java.util.LinkedList 5,157,128 (14.4%)
java.util.concurrent.atomic.AtomicReference 3,698,382 (10.3%)
clojure.lang.Atom 3,094,279 (8.6%)
...
Class by size of instances
java.lang.Object[] 210,061,752 B (13.8%)
java.util.LinkedList 206,285,120 B (13.6%)
clojure.lang.Atom 148,525,392 B (9.8%)
clojure.core.async.impl.channels.ManyToManyChannel 132,022,336 B (8.7%)
...
I finally figured out why. By looking at the source code, we get the following segment:
(defn pub
"Creates and returns a pub(lication) of the supplied channel, ..."
...
(let [mults (atom {}) ;;topic->mult
ensure-mult (fn [topic]
(or (get #mults topic)
(get (swap! mults
#(if (% topic) % (assoc % topic (mult (chan (buf-fn topic))))))
topic)))
p (reify
Mux
(muxch* [_] ch)
Pub
(sub* [p topic ch close?]
(let [m (ensure-mult topic)]
(tap m ch close?)))
(unsub* [p topic ch]
(when-let [m (get #mults topic)]
(untap m ch)))
(unsub-all* [_] (reset! mults {}))
(unsub-all* [_ topic] (swap! mults dissoc topic)))]
...
p)))
We can see mults stores all topic hence shall increase monotonically if we do not clear it. We may add something like (a/unsub-all* global-msg-pub pid) to fix that.

Setting a debug function from the command line in Clojure

I have a namespace like this:
(ns foo.core)
(def ^:dynamic *debug-fn*
"A function taking arguments [bar baz]"
nil)
(defn bar-info
[bar _]
(println bar))
(defn baz-info
[_ baz]
(println baz))
(defn do-stuff
[bar baz]
(when *debug-fn* (*debug-fn* bar baz)))
(defn -main
[& {:keys [debug-fn]}]
(binding [*debug-fn* (symbol debug-fn)] ;; THIS WON'T WORK!
(do-stuff 27 42)))
What I would like to do is allow a debug function to be specified from the command line like this: lein run bar-info or lein run baz-info.
I'm not sure how to take the string specified as a command-line argument and turn it into the namespace-qualified function to bind. Do I need a macro to do this?
Use ns-resolve, you will need to specify namespace where your function is defined though.
user=> (defn f [n] (* n n n))
#'user/f
user=> ((ns-resolve *ns* (symbol "f")) 10)
1000
Use alter-var-root:
user=> (doc alter-var-root)
-------------------------
clojure.core/alter-var-root
([v f & args])
Atomically alters the root binding of var v by applying f to its
current value plus any args
nil
user=> (alter-var-root #'*debug-fn* (fn [v] (fn [x] (println x) x)))
#<user$eval171$fn__172$fn__173 user$eval171$fn__172$fn__173#7c93d88e>
user=> (*debug-fn* 1)
1
1
Though I've accepted Guillermo's answer above, I figured that it might also be useful to add the solution I ended up going with:
(def debug-fns
{:bar-info (fn [bar _] (println bar))
:baz-info (fn [_ baz] (println baz))
(def active-debug-fns (atom []))
(defn activate-debug-fn!
[fn-key]
(let [f (debug-fns fn-key)]
(if f
(swap! active-debug-fns conj f)
(warn (str "Debug function " fn-key " not found! Available functions are: "
(join " " (map name (keys debug-fns))))))))
(defn debug-fn-keys
[args]
(if (args "--debug")
(split (or (args "--debug") "") #",")
[]))
(defn do-stuff
[bar baz]
(doseq [f #active-debug-fns]
(f bar baz)))
(defn -main
[& args]
(let [args (apply hash-map args)]
(doseq [f (debug-fn-keys args)]
(activate-debug-fn! (keyword k)))
(do-stuff 27 42)))
So now you can say something like lein run --debug bar-info to get info on bars, or lein run --debug bar,baz to get info on both bars and bazes.
Any suggestions to make this more idiomatic will be happily accepted and edited in. :)

Clojure: Defining a symbol in another namespace

Context
This is the contents of init.clj
(ns init)
(defn get-hotswap []
(filter #(= (ns-name %) 'hotswap) (all-ns)))
(let [x (get-hotswap)]
(let [old-ns *ns*]
(if (empty? x)
(do
(create-ns 'hotswap)
(in-ns 'hotswap)
(def global-kv-store (clojure.core/atom {}))
(in-ns (ns-name old-ns)))
(println "Found Hotswap"))))
Now. hotswap/global-kv-store does not exist, but init/global-kv-store does exist.
Question
How do I fix this? I want to be able to
create a new namespace hotswap
and then define a new variable global-kv-store in it
Thanks!
You can try this:
(if-not (find-ns 'hotswap)
(intern (create-ns 'hotswap) 'global-kv-store (atom {})))

clojure: adding a debug trace to every function in a namespace?

just started using log4j in one of my home-projects and I was just about to break out the mouse and cut-and-paste (trace (str "entering: " function-name)) into every function in a large module. then the voice of reason caught up and said "there has simply got to be a better way"... I can think of making a macro that wraps a whole block of functions and adds the traces to them or something like that? Any advice from the wise Stack-overflowing-clojurians?
No need for a macro:
(defn trace-ns
"ns should be a namespace object or a symbol."
[ns]
(doseq [s (keys (ns-interns ns))
:let [v (ns-resolve ns s)]
:when (and (ifn? #v) (-> v meta :macro not))]
(intern ns
(with-meta s {:traced true :untraced #v})
(let [f #v] (fn [& args]
(clojure.contrib.trace/trace (str "entering: " s))
(apply f args))))))
(defn untrace-ns [ns]
(doseq [s (keys (ns-interns ns))
:let [v (ns-resolve ns s)]
:when (:traced (meta v))]
(alter-meta! (intern ns s (:untraced (meta v)))
#(dissoc % :traced :untraced))))
...or something similar. The most likely extra requirement would be to use filter so as not to call trace on things which aren't ifn?s. Update: edited in a solution to that (also handling macros). Update 2: fixed some major bugs. Update 4: added untrace functionality.
Update 3: Here's an example from my REPL:
user> (ns foo)
nil
foo> (defn foo [x] x)
#'foo/foo
foo> (defmacro bar [x] x)
#'foo/bar
foo> (ns user)
nil
user> (trace-ns 'foo)
nil
user> (foo/foo :foo)
TRACE: "entering: foo"
:foo
user> (foo/bar :foo)
:foo
user> (untrace-ns 'foo)
nil
user> (foo/foo :foo)
:foo