Last active
August 29, 2015 14:15
-
-
Save paukul/d26c482d572c86481f56 to your computer and use it in GitHub Desktop.
simple queue manager
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
D: {{device_1 apn [HSV CL] asdf} 1 f4bac0f8-9d4e-4a4b-4c94-066661982203} | |
D: {{device_1 apn [HSV CL] asdf} 1 12f9cbac-e5dd-47ea-5e67-840bb4378945} | |
R: {{device_1 apn [HSV CL] asdf} 3 6ea977d4-ec97-4653-5d3f-6aedd6407f91} | |
D: {{device_1 apn [HSV CL] asdf} 1 abdaa11e-0274-4c2b-6dcd-9d41eb303f3c} | |
R: {{device_1 apn [HSV CL] asdf} 2 7e3e2a04-6f0a-434c-6488-b6c4f21ac2d6} | |
R: {{device_1 apn [HSV CL] asdf} 2 e8d629eb-541f-4a00-50aa-4ffd473c129f} | |
R: {{device_1 apn [HSV CL] asdf} 2 1e1273fb-94bd-4d30-417d-587a0d800a6c} | |
D: {{device_1 apn [HSV CL] asdf} 1 41669839-e220-43f9-4e7b-7178ef41dc76} | |
D: {{device_1 apn [HSV CL] asdf} 1 57201b63-a648-4327-6608-f9015fc2e52a} | |
R: {{device_1 apn [HSV CL] asdf} 2 ca1ac428-86f2-413c-52cf-8059b16e6883} | |
R: {{device_1 apn [HSV CL] asdf} 2 2b7212ec-8841-44e2-4075-e75692aa7bc2} | |
D: {{device_1 apn [HSV CL] asdf} 1 63cb059d-711e-4858-60e3-1ff8a18f535f} | |
D: {{device_1 apn [HSV CL] asdf} 2 e58347be-f421-424a-5927-4ec333bb1cb4} | |
D: {{device_1 apn [HSV CL] asdf} 2 88f531e4-f8d7-4226-765b-f6dc9393da4c} | |
R: {{device_1 apn [HSV CL] asdf} 2 206f1174-4c1b-42ac-4211-19b7453bb109} | |
D: {{device_1 apn [HSV CL] asdf} 1 f490b7a4-275a-49a7-6e4e-ecd12c26a2d2} | |
R: {{device_1 apn [HSV CL] asdf} 3 9d35fbda-4965-4afe-50a1-e852101cf2b5} | |
R: {{device_1 apn [HSV CL] asdf} 3 5d4f677b-1d9b-47d8-4fe8-7b7c9e6e2b59} | |
R: {{device_1 apn [HSV CL] asdf} 3 eb887f03-10c8-4963-4119-4fa1a9e4756e} | |
D: {{device_1 apn [HSV CL] asdf} 2 2ddda8c9-1339-4386-768a-e54f166b204f} | |
D: {{device_1 apn [HSV CL] asdf} 3 0957b59a-d97f-49a5-5793-bd83e74e892c} | |
R: {{device_1 apn [HSV CL] asdf} 3 3bb190ad-f5fa-439d-5257-06ac5ddb4511} | |
R: {{device_1 apn [HSV CL] asdf} 3 14f5fe2f-d3e4-44ce-5c16-02b9f4eea663} | |
D: {{device_1 apn [HSV CL] asdf} 2 9aaa6e8d-65e6-4311-7264-f186a249c51e} | |
D: {{device_1 apn [HSV CL] asdf} 3 1ba0f496-c7f3-41e8-587b-7c52f81574b9} | |
D: {{device_1 apn [HSV CL] asdf} 2 c6936152-062e-41df-5dee-2c881394d5db} | |
R: {{device_1 apn [HSV CL] asdf} 3 3843fd03-32fe-477e-649c-577cad55fb7c} | |
D: {{device_1 apn [HSV CL] asdf} 2 dd1ed0cf-1a15-4c24-7d50-cbd4f3a3547c} | |
D: {{device_1 apn [HSV CL] asdf} 2 b53192e4-d039-4f83-4c4b-58a8ac63ac49} | |
R: {{device_1 apn [HSV CL] asdf} 3 21e8a49d-e9e4-4cf6-71d4-c25677f30664} | |
R: {{device_1 apn [HSV CL] asdf} 3 4f5c9e09-94ed-4da9-6504-b4524ba1d1b8} | |
D: {{device_1 apn [HSV CL] asdf} 3 2f694219-76a9-49e2-7824-0582bbafa42c} | |
D: {{device_1 apn [HSV CL] asdf} 2 6c732166-1405-482c-7efd-0a01c0db0f3c} | |
F: {{device_1 apn [HSV CL] asdf} 4 261acdc3-148e-4ecd-6da6-5468fb5b2e08} | |
R: {{device_1 apn [HSV CL] asdf} 3 6a22879a-64ca-40fe-6417-613e9c69458c} | |
D: {{device_1 apn [HSV CL] asdf} 2 ef87c1c2-7cf9-4c11-7456-2c0a057fc819} | |
R: {{device_1 apn [HSV CL] asdf} 3 6d671940-2ac7-457f-4fe0-df80c4270fe5} | |
R: {{device_1 apn [HSV CL] asdf} 3 afe3579a-84db-41d2-75db-aaddcb2ecc62} | |
D: {{device_1 apn [HSV CL] asdf} 2 ab5b4bc3-7721-4e9f-708d-f90fdcf44179} | |
F: {{device_1 apn [HSV CL] asdf} 4 6ea977d4-ec97-4653-5d3f-6aedd6407f91} | |
R: {{device_1 apn [HSV CL] asdf} 3 7e3e2a04-6f0a-434c-6488-b6c4f21ac2d6} | |
D: {{device_1 apn [HSV CL] asdf} 2 1e1273fb-94bd-4d30-417d-587a0d800a6c} | |
R: {{device_1 apn [HSV CL] asdf} 3 e8d629eb-541f-4a00-50aa-4ffd473c129f} | |
R: {{device_1 apn [HSV CL] asdf} 3 ca1ac428-86f2-413c-52cf-8059b16e6883} | |
R: {{device_1 apn [HSV CL] asdf} 3 2b7212ec-8841-44e2-4075-e75692aa7bc2} | |
D: {{device_1 apn [HSV CL] asdf} 2 206f1174-4c1b-42ac-4211-19b7453bb109} | |
F: {{device_1 apn [HSV CL] asdf} 4 9d35fbda-4965-4afe-50a1-e852101cf2b5} | |
F: {{device_1 apn [HSV CL] asdf} 4 5d4f677b-1d9b-47d8-4fe8-7b7c9e6e2b59} |
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
/* | |
if you find this: this is a learning project... | |
I obviously don't know much go yet and will be ashamed of this | |
code in about 2-3 days. I obvioysly forgot to delete this gist... | |
*/ | |
package web | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
"github.com/nu7hatch/gouuid" | |
) | |
type deviceRequestJob struct { | |
req deviceRequest | |
tries int | |
jid string | |
} | |
func newDeviceRequestJob(r deviceRequest) deviceRequestJob { | |
var jid string | |
u4, err := uuid.NewV4() | |
if err != nil { | |
fmt.Println("error:", err) | |
jid = fmt.Sprintf("failing-uuid-%d", rand.Int()) | |
} else { | |
jid = u4.String() | |
} | |
return deviceRequestJob{ | |
req: r, | |
tries: 0, | |
jid: jid, | |
} | |
} | |
type manager struct { | |
PoolSize int | |
queue chan deviceRequestJob | |
maxRetries int | |
} | |
func newManager(poolSize int) *manager { | |
m := &manager{ | |
queue: make(chan deviceRequestJob, 1), | |
PoolSize: poolSize, | |
maxRetries: 3, | |
} | |
go m.run() | |
return m | |
} | |
func (m *manager) Queue(r deviceRequest) { | |
m.queue <- newDeviceRequestJob(r) | |
} | |
func (m *manager) run() { | |
inbox := make([]deviceRequestJob, 0, 1000) | |
pool := make([]*worker, m.PoolSize) | |
done := make(chan *worker) | |
err := make(chan workerError) | |
for i, _ := range pool { | |
pool[i] = &worker{done: done, err: err} | |
} | |
for { | |
select { | |
case r := <-m.queue: | |
r.tries++ | |
inbox = append(inbox, r) | |
case w := <-done: | |
pool = append(pool, w) | |
case e := <-err: | |
job := e.job | |
job.tries++ | |
if job.tries > m.maxRetries { | |
fmt.Printf("F: %v\n", job) | |
continue | |
} | |
fmt.Printf("R: %v\n", job) | |
pool = append(pool, e.worker) | |
inbox = append(inbox, job) | |
} | |
if len(inbox) > 0 && len(pool) > 0 { | |
w := pool[0] | |
pool = pool[1:] | |
j := inbox[0] | |
inbox = inbox[1:] | |
go w.work(j) | |
} | |
} | |
} | |
type worker struct { | |
done chan *worker | |
err chan workerError | |
} | |
func (w *worker) work(j deviceRequestJob) { | |
time.Sleep(5 * time.Millisecond) | |
if rand.Intn(2) == 0 { | |
w.err <- w.newError("something happened", j) | |
} else { | |
fmt.Printf("D: %v\n", j) | |
w.done <- w | |
} | |
} | |
func (w *worker) newError(m string, j deviceRequestJob) workerError { | |
return workerError{ | |
msg: m, | |
worker: w, | |
job: j, | |
} | |
} | |
type workerError struct { | |
worker *worker | |
job deviceRequestJob | |
msg string | |
} | |
func (e workerError) Error() string { | |
return e.msg | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment