chg(balancer): Move balancer-v2 and worker to separate packages.
authorsgf <sgf.dma@gmail.com>
Tue, 1 Nov 2022 15:40:23 +0000 (18:40 +0300)
committersgf <sgf.dma@gmail.com>
Tue, 1 Nov 2022 15:40:23 +0000 (18:40 +0300)
concurrency-is-not-parallelism/balancer_v2/.gitignore [new file with mode: 0644]
concurrency-is-not-parallelism/balancer_v2/balancer/balancer.go [moved from concurrency-is-not-parallelism/balancer_v2/balancer.go with 99% similarity]
concurrency-is-not-parallelism/balancer_v2/balancer/go.mod [new file with mode: 0644]
concurrency-is-not-parallelism/balancer_v2/balancer/worker/go.mod [new file with mode: 0644]
concurrency-is-not-parallelism/balancer_v2/balancer/worker/worker.go [new file with mode: 0644]
concurrency-is-not-parallelism/balancer_v2/balancer_v2.go [moved from concurrency-is-not-parallelism/balancer_v2.go with 60% similarity]
concurrency-is-not-parallelism/balancer_v2/go.mod

diff --git a/concurrency-is-not-parallelism/balancer_v2/.gitignore b/concurrency-is-not-parallelism/balancer_v2/.gitignore
new file mode 100644 (file)
index 0000000..863e124
--- /dev/null
@@ -0,0 +1 @@
+balancer_v2
diff --git a/concurrency-is-not-parallelism/balancer_v2/balancer/go.mod b/concurrency-is-not-parallelism/balancer_v2/balancer/go.mod
new file mode 100644 (file)
index 0000000..30a2fa7
--- /dev/null
@@ -0,0 +1,3 @@
+module balancer
+
+go 1.15
diff --git a/concurrency-is-not-parallelism/balancer_v2/balancer/worker/go.mod b/concurrency-is-not-parallelism/balancer_v2/balancer/worker/go.mod
new file mode 100644 (file)
index 0000000..3b84b83
--- /dev/null
@@ -0,0 +1,7 @@
+module balancer/worker
+
+go 1.15
+
+replace balancer => ../../balancer
+
+require balancer v0.0.0-00010101000000-000000000000
diff --git a/concurrency-is-not-parallelism/balancer_v2/balancer/worker/worker.go b/concurrency-is-not-parallelism/balancer_v2/balancer/worker/worker.go
new file mode 100644 (file)
index 0000000..e79bfbd
--- /dev/null
@@ -0,0 +1,47 @@
+
+package worker
+
+import (
+    "fmt"
+    "time"
+    "math/rand"
+    "balancer"
+)
+
+type Runner struct {
+    Requests chan balancer.Requester
+    id balancer.WorkerId
+}
+
+func (w Runner) String() string {
+    return fmt.Sprintf("%v", w.id)
+}
+var _ balancer.WorkRunner = (*Runner)(nil)
+
+func (w *Runner) InitRunner(wid balancer.WorkerId) {
+    w.id = wid
+}
+
+func (w *Runner) Work(id string, done chan<- *balancer.Done) {
+    t := rand.Intn(5)
+    fmt.Printf("  %v [%v]: sleeping for %v secs before begin..\n", w, id, t)
+    time.Sleep(time.Duration(t) * time.Second)
+    timeout := time.After(time.Second)
+    for {
+        select {
+        case req := <- w.Requests:
+            fmt.Printf("  %v [%v]: start computing '%v'\n", w, id, req)
+            req.Send(req.Fn())
+            done <- &balancer.Done{w.id, req}
+            fmt.Printf("  %v [%v]: finished '%v'\n", w, id, req)
+        case <-timeout:
+            fmt.Printf("  %v [%v]: i'm still here..\n", w.id, id)
+            timeout = time.After(time.Second)
+        }
+    }
+}
+
+func (w *Runner) Send(req balancer.Requester) {
+    w.Requests <- req
+}
+
@@ -5,7 +5,8 @@ import (
     "fmt"
     "time"
     "math/rand"
-    balancer "./balancer_v2"
+    "balancer"
+    "balancer/worker"
 )
 
 // FIXME: Add buffers to channels. What changes?
@@ -54,44 +55,6 @@ func requester(work chan<- balancer.Requester) {
     }
 }
 
-
-type workRun struct {
-    requests chan balancer.Requester
-    id balancer.WorkerId
-}
-
-func (w workRun) String() string {
-    return fmt.Sprintf("%v", w.id)
-}
-var _ balancer.WorkRunner = (*workRun)(nil)
-
-func (w *workRun) InitRunner(wid balancer.WorkerId) {
-    w.id = wid
-}
-
-func (w *workRun) Work(id string, done chan<- *balancer.Done) {
-    t := rand.Intn(5)
-    fmt.Printf("  %v [%v]: sleeping for %v secs before begin..\n", w, id, t)
-    time.Sleep(time.Duration(t) * time.Second)
-    timeout := time.After(time.Second)
-    for {
-        select {
-        case req := <- w.requests:
-            fmt.Printf("  %v [%v]: start computing '%v'\n", w, id, req)
-            req.Send(req.Fn())
-            done <- &balancer.Done{w.id, req}
-            fmt.Printf("  %v [%v]: finished '%v'\n", w, id, req)
-        case <-timeout:
-            fmt.Printf("  %v [%v]: i'm still here..\n", w.id, id)
-            timeout = time.After(time.Second)
-        }
-    }
-}
-
-func (w *workRun) Send(req balancer.Requester) {
-    w.requests <- req
-}
-
 var nWorker int = 4
 
 func main() {
@@ -100,7 +63,7 @@ func main() {
     workers := make([]balancer.WorkRunner, nWorker)
     for i := 0; i < nWorker; i++ {
         c := make(chan balancer.Requester)
-        workers[i] = &workRun{requests: c}
+        workers[i] = &worker.Runner{Requests: c}
     }
     b := balancer.InitBalancer(workers)
     go requester(work)
index e35bd31..9a4e6e1 100644 (file)
@@ -1,3 +1,12 @@
 module balancer_v2
 
 go 1.15
+
+replace balancer => ./balancer
+
+replace balancer/worker => ./balancer/worker
+
+require (
+       balancer v0.0.0-00010101000000-000000000000
+       balancer/worker v0.0.0-00010101000000-000000000000
+)