This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns thornydev.go-lightly.boring.generator-tq | |
(:require [thornydev.go-lightly :refer [go go& stop]]) | |
(:import (java.util.concurrent LinkedTransferQueue))) | |
(defn- channel [] | |
(LinkedTransferQueue.)) | |
(defn- boring [msg] | |
(let [ch (channel)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns thornydev.go-lightly.boring.generator-lamina | |
(:require [thornydev.go-lightly.util :refer [go&]] | |
[lamina.core :refer [channel enqueue close | |
closed? read-channel]])) | |
;; this version is REPL-friendly since the deamon go routine | |
;; will exit when the channel is closed | |
(defn- boring [msg] | |
(let [ch (channel)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns thornydev.go-lightly.boring.generator-lamina | |
(:require | |
[thornydev.go-lightly.util :refer [go& with-channel-open]] | |
[lamina.core :refer [channel enqueue close | |
closed? read-channel]])) | |
(defn- boring [msg] | |
(let [ch (channel)] | |
(go& (loop [i 0] | |
(when-not (closed? ch) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
"os" | |
) | |
// Channels-driven concurrency with Go |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns thornydev.go-lightly.boring.select-timeout | |
(:refer-clojure :exclude [peek take]) | |
(:require [thornydev.go-lightly :refer | |
[go stop put take select select-timeout | |
channel timeout-channel with-timeout]])) | |
(defn- boring [msg] | |
(let [ch (channel)] | |
(go (loop [i 0] | |
(put ch (str msg " " i)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn- choose [ready-chans] | |
(.take (nth ready-chans (rand-int (count ready-chans))))) | |
(defn- peek-channels [channels] | |
(let [ready (doall (keep #(when-not (nil? (.peek %)) %) channels))] | |
(if (seq ready) | |
(nth ready (rand-int (count ready))) ;; pick at random if >1 ready | |
(Thread/sleep 0 500)))) | |
(defn- probe-til-ready [channels] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
var ( | |
Web1 = fakeSearch("web1") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(testing "timeout channel with other channels" | |
(let [ch1 (channel) | |
ch2 (channel) | |
tch (timeout-channel 250) | |
fnext-msg (partial select ch1 ch2 tch)] | |
(go (test-routine ch1 1)) | |
(go (test-routine ch1 2)) | |
(loop [msg (fnext-msg)] | |
(when-not (= msg :go-lightly/timeout) | |
(is (some #{1 2} [msg])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns thornydev.go-lightly.boring.select-lamina | |
(:require [thornydev.go-lightly :as go] | |
[thornydev.go-lightly.util :refer [with-channel-open]] | |
[lamina.core :refer [close] :as lam]) | |
(:import (java.util.concurrent TimeoutException))) | |
(defn- boring [msg] | |
(let [ch (lam/channel)] | |
(go/go& (loop [i 0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns thornydev.go-lightly.primes.conc-prime-sieve | |
(:refer-clojure :exclude [take peek]) | |
(:require [thornydev.go-lightly :refer :all])) | |
;; A Clojure implementation of the Concurrent Primary Sieve | |
;; example in Go, using the go-lightly library | |
;; Note: could not use a Lamina channel for this particular | |
;; implementation, bcs it uses ConcurrentLinkedQueue, | |
;; which is unbounded and non-blocking |