Created
November 7, 2020 16:06
-
-
Save foreseaz/670d43cc41bd6742d8f51945b3f6ab2d to your computer and use it in GitHub Desktop.
Golang Subscriber Pattern
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 ( | |
"time" | |
) | |
type Event struct { | |
data int | |
} | |
// Fib generator | |
func Fib(n int) chan int { | |
out := make(chan int) | |
go func() { | |
defer close(out) | |
for i, j := 0, 1; i < n; i, j = i+j, i { | |
out <- i | |
} | |
}() | |
return out | |
} | |
func main() { | |
subscriber1 := NewObserver(1, time.Now()) | |
subscriber2 := NewObserver(2, time.Now()) | |
subject := NewSubject() | |
subject.Attach(subscriber1) | |
subject.Attach(subscriber2) | |
go func() { | |
select { | |
case <- time.After(1 * time.Millisecond): | |
subject.Detach(subscriber2) | |
} | |
}() | |
for x := range Fib(1000000) { | |
subject.Notify(Event{data: x}) | |
} | |
} |
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" | |
"time" | |
) | |
type Observer interface { | |
NotifyCallback(evt Event) | |
} | |
// concrete observer for fib sequenece | |
type FibObserver struct { | |
id int64 // current observer id | |
time time.Time // join time? | |
} | |
// Observer | |
func NewObserver(id int64, time time.Time) *FibObserver { | |
return &FibObserver{ | |
id, | |
time, | |
} | |
} | |
func (o *FibObserver) NotifyCallback(evt Event) { | |
fmt.Println(o.id, "received event with data:", evt.data, "with time:", time.Since(o.time)) | |
} | |
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 "sync" | |
type Subject interface { | |
Attach(o Observer) | |
Detach(o Observer) | |
Notify(evt Event) | |
} | |
// concrete subject for this data source | |
// needs to maintain an array of observers | |
type FibSubject struct { | |
observers sync.Map | |
} | |
// Subject | |
func NewSubject() *FibSubject { | |
return &FibSubject{ | |
observers: sync.Map{}, | |
} | |
} | |
func (s *FibSubject) Attach(o Observer) { | |
s.observers.Store(o, struct{}{}) | |
} | |
func (s *FibSubject) Detach(o Observer) { | |
s.observers.Delete(o) | |
} | |
func (s *FibSubject) Notify(evt Event) { | |
s.observers.Range(func(key interface{}, value interface{}) bool { | |
if key == nil || value == nil { | |
return false | |
} | |
key.(Observer).NotifyCallback(evt) | |
return true | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because the Notify is async, it cannot ensure the subscriber's order to run callback function.