Skip to content

Instantly share code, notes, and snippets.

@dulao5
Last active September 7, 2018 03:41
Show Gist options
  • Save dulao5/7ad8e4bbb6ab3c664b0ad7fc6be34f47 to your computer and use it in GitHub Desktop.
Save dulao5/7ad8e4bbb6ab3c664b0ad7fc6be34f47 to your computer and use it in GitHub Desktop.
golang-sharemem
// https://golang.org/doc/codewalk/sharemem/
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"log"
"net/http"
"time"
)
const (
numPollers = 2 // number of Poller goroutines to launch
pollInterval = 60 * time.Second // how often to poll each URL
statusInterval = 10 * time.Second // how often to log status to stdout
errTimeout = 10 * time.Second // back-off timeout on error
)
var urls = []string{
"http://www.google.com/",
"http://golang.org/",
"http://blog.golang.org/",
}
// State represents the last-known state of a URL.
type State struct {
url string
status string
}
// StateMonitor maintains a map that stores the state of the URLs being
// polled, and prints the current state every updateInterval nanoseconds.
// It returns a chan State to which resource state should be sent.
func StateMonitor(updateInterval time.Duration) chan<- State {
updates := make(chan State)
urlStatus := make(map[string]string)
ticker := time.NewTicker(updateInterval)
go func() {
for {
select {
case <-ticker.C:
logState(urlStatus)
case s := <-updates:
urlStatus[s.url] = s.status
}
}
}()
return updates
}
// logState prints a state map.
func logState(s map[string]string) {
log.Println("Current state:")
for k, v := range s {
log.Printf(" %s %s", k, v)
}
}
// Resource represents an HTTP URL to be polled by this program.
type Resource struct {
url string
errCount int
}
// Poll executes an HTTP HEAD request for url
// and returns the HTTP status string or an error string.
func (r *Resource) Poll() string {
resp, err := http.Head(r.url)
if err != nil {
log.Println("Error", r.url, err)
r.errCount++
return err.Error()
}
r.errCount = 0
return resp.Status
}
// Sleep sleeps for an appropriate interval (dependent on error state)
// before sending the Resource to done.
func (r *Resource) Sleep(done chan<- *Resource) {
time.Sleep(pollInterval + errTimeout*time.Duration(r.errCount))
done <- r
}
func Poller(in <-chan *Resource, out chan<- *Resource, status chan<- State) {
for r := range in {
s := r.Poll()
status <- State{r.url, s}
out <- r
}
}
func main() {
// Create our input and output channels.
pending, complete := make(chan *Resource), make(chan *Resource)
// Launch the StateMonitor.
status := StateMonitor(statusInterval)
// Launch some Poller goroutines.
for i := 0; i < numPollers; i++ {
go Poller(pending, complete, status)
}
// Send some Resources to the pending queue.
go func() {
for _, url := range urls {
pending <- &Resource{url: url}
}
}()
for r := range complete {
go r.Sleep(pending)
}
}
digraph g {
node [
fontsize = "16"
shape = "ellipse"
];
edge [];
subgraph cluster_main {
style=filled;
color=lightgrey;
node [style=filled,color=white];
make_chan -> go_StateMonitor -> go_Poller -> go_init_Resource -> for_range_complete -> go_sleep ;
label = "main goroutine";
}
subgraph cluster_Poller1 {
node [style=filled];
for_range_pending -> Poll -> send_to_status
Poll -> send_to_complete
label = "Poller1..2 goroutine";
color=blue
}
go_Poller -> for_range_pending
{ rank=same; go_Poller for_range_pending }
subgraph cluster_StateMonitor {
node [style=filled];
for_loop -> select -> case_ticker -> logState
select -> case_status -> update_urlStatus;
label = "StateMonitor goroutine";
color=blue
urlStatus [
label = "<f0> urlStatus map | url : status"
shape = "record"
]
}
go_StateMonitor -> for_loop
{ rank=same; go_StateMonitor for_loop }
pending [
label = "<f0> pending chan * Resource | url string | errCount int"
shape = "record"
]
complete [
label = "<f0>complete chan * Resource | url string | errCount int"
shape = "record"
]
status [
label = "<f0> status chan * State | url string | status string"
shape = "record"
]
go_init_Resource -> pending [label = "init", color=gold, style=dashed]
complete -> for_range_complete [label = "receive ", color=forestgreen, style=dashed]
go_sleep -> pending [label = "sleep and push", color=forestgreen, style=dashed]
status -> case_status [ label = "receive status", color=forestgreen, style=dashed]
update_urlStatus -> urlStatus [ label = "update", color=forestgreen, style=dashed]
logState -> urlStatus [ label = "for range", color=forestgreen, style=dashed]
pending -> for_range_pending [label = "in queue", color=forestgreen, style=dashed]
send_to_complete -> complete [label = "out queue", color=forestgreen, style=dashed]
send_to_status -> status [label = "out queue", color=forestgreen, style=dashed]
}
@dulao5
Copy link
Author

dulao5 commented Sep 7, 2018

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment