Skip to content

Instantly share code, notes, and snippets.

Avatar

Soheil Hassas Yeganeh soheilhy

View GitHub Profile
@soheilhy
soheilhy / server.go
Created Aug 1, 2015
grpc route guide server + cmux
View server.go
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
View cmux_example.go
// Create the main listener.
lis, err := net.Listen("tcp", ":23456")
if err != nil {
log.Fatal(err)
}
// Create a cmux.
mux := cmux.New(lis)
// Match connections in order:
@soheilhy
soheilhy / queue.go
Created Jul 22, 2015
Register HTTP Handlers
View queue.go
// RegisterTaskQ registers the taskq application and all its handler in the
// hive.
func RegisterTaskQ(h beehive.Hive) {
a := h.NewApp("taskq", beehive.Persistent(3))
a.Handle(Enque{}, EnQHandler{})
a.Handle(Deque{}, DeQHandler{})
a.Handle(Ack{}, AckHandler{})
a.Handle(Timeout{}, TimeoutHandler{
ExpDur: 60 * time.Second,
})
@soheilhy
soheilhy / queue.go
Created Jul 22, 2015
AckHTTPHandler
View queue.go
// AckHTTPHandler provides the HTTP endpoint for acknowledging tasks.
type AckHTTPHandler httpHandler
func (h *AckHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
q, ok := vars["queue"]
if !ok {
http.Error(w, "unkown queue", http.StatusBadRequest)
return
}
@soheilhy
soheilhy / queue.go
Created Jul 22, 2015
DeQHTTPHandler
View queue.go
// DeQHTTPHandler provides the HTTP endpoint for dequeuing tasks.
type DeQHTTPHandler httpHandler
func (h *DeQHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
q, ok := mux.Vars(r)["queue"]
if !ok {
http.Error(w, "unkown queue", http.StatusBadRequest)
return
}
@soheilhy
soheilhy / queue.go
Last active Aug 29, 2015
EnQHTTPHandler
View queue.go
type httpHandler struct {
Hive beehive.Hive // Hive represents the hive our handler is registered on.
}
// EnQHTTPHandler provides the HTTP endpoint for enqueuing tasks.
type EnQHTTPHandler httpHandler
func (h *EnQHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
q, ok := mux.Vars(r)["queue"]
if !ok {
@soheilhy
soheilhy / main.go
Last active Aug 29, 2015
Incomplete TaskQ/main()
View main.go
func main() {
h := beehive.NewHive()
taskq.RegisterTaskQ(h)
go h.Start()
defer h.Stop()
q := taskq.Queue("MyQueue")
b := "copy f1 to f2"
enq := taskq.Enque{Task: taskq.Task{Queue: q, Body: []byte(b)}}
@soheilhy
soheilhy / main.go
Created Jul 20, 2015
main incomplete
View main.go
func main() {
h := beehive.NewHive()
taskq.RegisterTaskQ(h)
glog.Info("taskq started")
h.Start()
}
@soheilhy
soheilhy / queue.go
Last active Aug 29, 2015
Register taskq handler (full)
View queue.go
// RegisterTaskQ registers the taskq application and all its handler in the
// hive.
func RegisterTaskQ(h beehive.Hive) {
a := h.NewApp("taskq", beehive.Persistent(3))
a.Handle(Enque{}, EnQHandler{})
a.Handle(Deque{}, DeQHandler{})
a.Handle(Ack{}, AckHandler{})
a.Handle(Timeout{}, TimeoutHandler{
ExpDur: 60 * time.Second,
})
@soheilhy
soheilhy / queue.go
Last active Aug 29, 2015
Register taskq handlers (simple)
View queue.go
app := beehive.NewApp("taskq")
app.Handle(Enque{}, EnQHandler{})
app.Handle(Deque{}, DeQHandler{})
app.Handle(Ack{}, AckHandler{})
app.Handle(Timeout{}, TimeoutHandler{
ExpDur: 60 * time.Second,
})
// Detached runs a local go-routine, and NewTimer
// creates a "detached" handler that calls the function
You can’t perform that action at this time.