Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Some 'sensible' changes to the syncasync example

  • Loading branch information...
commit b03f821432e7e34ed02c222ccb965bfa13186f0a 1 parent 99cba0b
Michael Bernstein authored January 05, 2013

Showing 1 changed file with 11 additions and 12 deletions. Show diff stats Hide diff stats

  1. 23  examples/syncasync/main.go
23  examples/syncasync/main.go
@@ -18,6 +18,8 @@ func main() {
18 18
 	node.NewAsyncEvent(Watcher)
19 19
 
20 20
 	node.NewAsyncEvent(ASyncLogger)
  21
+	node.NewAsyncEvent(ASyncLogger)
  22
+	node.NewAsyncEvent(ASyncLogger)
21 23
 
22 24
 	node.NewSyncEvent(SyncLogger)
23 25
 	node.NewSyncEvent(SyncLogger2)
@@ -29,21 +31,17 @@ func main() {
29 31
 }
30 32
 
31 33
 func Starter(c syl.Channels) {
32  
-	cd := 0
33  
-	for {
  34
+	for cd := 0; cd < 100; cd++ {
34 35
 		c.Data <- []byte{byte(cd)}
35  
-		cd += 1
36  
-		if cd > 100 {
37  
-			c.Control <- syl.NodeExit()
38  
-		}
39 36
 	}
  37
+	c.Control <- syl.NodeExit()
40 38
 }
41 39
 
42 40
 func ASyncLogger(c syl.Channels) {
43 41
 	for {
44 42
 		select {
45 43
 		case data := <-c.Data:
46  
-			<-time.After(time.Duration(rand.Int31n(100)) * time.Millisecond)
  44
+			<-time.After(time.Duration(rand.Int31n(10)) * time.Millisecond)
47 45
 			log.Print("a", data)
48 46
 		}
49 47
 	}
@@ -52,7 +50,7 @@ func ASyncLogger(c syl.Channels) {
52 50
 func SyncLogger(c syl.Channels) {
53 51
 	select {
54 52
 	case data := <-c.Data:
55  
-		<-time.After(time.Duration(rand.Int31n(100)) * time.Millisecond)
  53
+		<-time.After(time.Duration(rand.Int31n(10)) * time.Millisecond)
56 54
 		log.Print("           s", data)
57 55
 		c.Control <- syl.NodeNext()
58 56
 	}
@@ -61,7 +59,7 @@ func SyncLogger(c syl.Channels) {
61 59
 func SyncLogger2(c syl.Channels) {
62 60
 	select {
63 61
 	case data := <-c.Data:
64  
-		<-time.After(time.Duration(rand.Int31n(100)) * time.Millisecond)
  62
+		<-time.After(time.Duration(rand.Int31n(10)) * time.Millisecond)
65 63
 		log.Print("           s2", data)
66 64
 		c.Control <- syl.NodeNext()
67 65
 	}
@@ -70,7 +68,7 @@ func SyncLogger2(c syl.Channels) {
70 68
 func SyncLogger3(c syl.Channels) {
71 69
 	select {
72 70
 	case data := <-c.Data:
73  
-		<-time.After(time.Duration(rand.Int31n(100)) * time.Millisecond)
  71
+		<-time.After(time.Duration(rand.Int31n(10)) * time.Millisecond)
74 72
 		log.Print("           s3", data)
75 73
 		c.Control <- syl.NodeSyncEventRestart()
76 74
 	}
@@ -78,14 +76,15 @@ func SyncLogger3(c syl.Channels) {
78 76
 
79 77
 func Watcher(c syl.Channels) {
80 78
 	for {
81  
-		<-time.After(1 * time.Millisecond)
82 79
 		select {
83 80
 		case control := <-c.Control:
84 81
 			if bytes.Equal(control, syl.NodeExit()) {
85  
-				log.Print("Received EXIT, exiting")
  82
+				log.Print("Received EXIT, exiting in 100ms")
  83
+				<-time.After(500 * time.Millisecond)
86 84
 				os.Exit(0)
87 85
 			} else {
88 86
 				c.Control <- control
  87
+				<-time.After(1 * time.Millisecond)
89 88
 			}
90 89
 		}
91 90
 	}

0 notes on commit b03f821

Please sign in to comment.
Something went wrong with that request. Please try again.