-
Notifications
You must be signed in to change notification settings - Fork 989
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
836f8c6
commit 738598d
Showing
5 changed files
with
148 additions
and
18 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package puller | ||
|
||
import "log" | ||
|
||
func DownloadFunc(done chan struct{}, in chan EventWrapper) chan EventWrapper { | ||
out := make(chan EventWrapper) | ||
go func() { | ||
for event := range in { | ||
select { | ||
default: | ||
e := download(event) | ||
if e == nil { | ||
if event.DownloadRetries > 2 { | ||
// LOOK AT THIS | ||
close(done) | ||
close(out) | ||
} else { | ||
log.Println("I am retrying", event.ModelName) | ||
event.DownloadRetries += 1 | ||
in <- event | ||
} | ||
} else { | ||
out <- event | ||
} | ||
case <-done: | ||
return | ||
} | ||
} | ||
close(out) | ||
}() | ||
return out | ||
} | ||
|
||
func download(event EventWrapper) *EventWrapper { | ||
// TODO: Proper download logic | ||
// We are testing the retry logic here | ||
if event.ModelName == "my_model" || event.ModelName == "my_model2"{ | ||
log.Println("Downloading model,", event.ModelName) | ||
if event.DownloadRetries == 0 { | ||
log.Println("Need to retry download", event.ModelName) | ||
return nil | ||
} | ||
log.Println("Success downloading my model", event.ModelName) | ||
return &event | ||
} | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package puller | ||
|
||
import ( | ||
"log" | ||
) | ||
|
||
var ( | ||
p *Puller | ||
) | ||
|
||
|
||
type Puller struct { | ||
NumWorkers int | ||
Channel chan EventWrapper | ||
} | ||
|
||
func init() { | ||
p = NewPuller() | ||
} | ||
|
||
func NewPuller() *Puller { | ||
p := new(Puller) | ||
return p | ||
} | ||
|
||
func worker(id int, events<-chan EventWrapper) { | ||
log.Println("worker", id) | ||
for event := range events { | ||
log.Println("worker", id, "started job", event) | ||
// TODO: Need to signal to close done and downloadChannel | ||
// instead of closing in go-routines | ||
done := make(chan struct{}) | ||
downloadChannel := make(chan EventWrapper, 1) | ||
downloadChannel <- event | ||
requestChannel := DownloadFunc(done, downloadChannel) | ||
result := RequestFunc(done, requestChannel) | ||
log.Println("worker", id, "finished job", event, "with:", result) | ||
} | ||
} | ||
|
||
func AddModelToChannel(e EventWrapper) { | ||
p.Channel <- e | ||
} | ||
|
||
func InitiatePullers(numWorkers int) { | ||
p.NumWorkers = numWorkers | ||
p.Channel = make(chan EventWrapper) | ||
for workers := 1; workers <= p.NumWorkers; workers++ { | ||
go worker(workers, p.Channel) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package puller | ||
|
||
func RequestFunc(done chan struct{}, in chan EventWrapper) bool { | ||
for event := range in { | ||
select { | ||
default: | ||
e := request(event) | ||
if e == nil { | ||
return false | ||
} else { | ||
return true | ||
} | ||
case <-done: | ||
return false | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func request(event EventWrapper) *EventWrapper { | ||
// TODO: Write request logic for load / unload | ||
// Currently just testing my_model | ||
if event.ModelName == "my_model" { | ||
return &event | ||
} | ||
return nil | ||
} |
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