Created
January 16, 2018 15:02
-
-
Save peterhellberg/83b6a1325f0966824d7486d7ce242a5e to your computer and use it in GitHub Desktop.
A pretty minimal work queue
This file contains 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 ( | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
func main() { | |
logger := log.New(os.Stdout, "", 0) | |
s := NewServer( | |
Log(logger), | |
Queue(make(chan func(), 100)), | |
) | |
go s.Work() | |
hs := &http.Server{Addr: "0.0.0.0:2018", Handler: s} | |
logger.Printf("Listening on http://%s\n", hs.Addr) | |
hs.ListenAndServe() | |
} | |
type Logger interface { | |
Printf(format string, v ...interface{}) | |
} | |
type Server struct { | |
logger Logger | |
handler http.Handler | |
queue chan func() | |
} | |
type Option func(*Server) | |
func Log(l Logger) Option { | |
return func(s *Server) { | |
s.logger = l | |
} | |
} | |
func Queue(q chan func()) Option { | |
return func(s *Server) { | |
s.queue = q | |
} | |
} | |
func NewServer(options ...Option) *Server { | |
s := &Server{ | |
logger: log.New(os.Stdout, "", 0), | |
queue: make(chan func(), 10), | |
} | |
for _, f := range options { | |
f(s) | |
} | |
s.register(http.NewServeMux()) | |
return s | |
} | |
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
s.logger.Printf("%s %q count#requests=1\n", r.Method, r.URL.Path) | |
s.handler.ServeHTTP(w, r) | |
} | |
func (s *Server) Work() { | |
for job := range s.queue { | |
job() | |
} | |
} | |
func (s *Server) index(w http.ResponseWriter, r *http.Request) { | |
jobID := time.Now().Unix() | |
s.queue <- func() { | |
s.log("Job %d: Started", jobID) | |
s.log("Job %d: Sleeping", jobID) | |
time.Sleep(2 * time.Second) | |
s.log("Job %d: Finished", jobID) | |
} | |
s.log("Job %d: Enqueued", jobID) | |
w.WriteHeader(http.StatusNoContent) | |
} | |
func (s *Server) log(format string, v ...interface{}) { | |
s.logger.Printf(format+"\n", v...) | |
} | |
func (s *Server) register(mux *http.ServeMux) { | |
mux.HandleFunc("/", s.index) | |
s.handler = mux | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use some globally unique id generator (xid, ulid, etc.)