Skip to content

Commit 590b4d6

Browse files
authored
Merge pull request #38 from getsumio/develop
Develop
2 parents 6cab3df + c793f4d commit 590b4d6

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

internal/servers/onpremise.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path"
99
"regexp"
1010
"sync"
11+
"time"
1112

1213
"github.com/getsumio/getsum/internal/config"
1314
. "github.com/getsumio/getsum/internal/config"
@@ -32,7 +33,6 @@ const uuidPattern = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][
3233
var regex *regexp.Regexp = regexp.MustCompile(uuidPattern)
3334

3435
const default_capacity = 250
35-
const threshold = 150
3636

3737
//start server in given config listen address and port or tls details
3838
//TODO add interface support
@@ -102,7 +102,6 @@ func handlePost(s *OnPremiseServer, w http.ResponseWriter, r *http.Request) {
102102
var algorithm = ValueOf(&config.Algorithm[0])
103103
config.Dir = &s.StoragePath
104104
supplier, err := factory.GetSupplierByAlgo(config, &algorithm)
105-
//something went wrong, TODO add error handler
106105
if err != nil {
107106
handleError("Can not create algorithm runner instance: "+err.Error(), w)
108107
return
@@ -117,6 +116,7 @@ func handlePost(s *OnPremiseServer, w http.ResponseWriter, r *http.Request) {
117116
return
118117
}
119118
w.Write(jsonStat)
119+
supplier.Data().StartTime = time.Now()
120120
go supplier.Run()
121121
s.suppliers[processId] = supplier
122122
logger.Info("Process started")
@@ -185,15 +185,12 @@ func handleError(message string, w http.ResponseWriter, params ...interface{}) {
185185

186186
func (s *OnPremiseServer) ensureCapacity() {
187187
if len(s.suppliers) >= default_capacity {
188-
var i int = 0
188+
now := time.Now()
189189
for k := range s.suppliers {
190-
if i >= threshold {
191-
supplier := s.suppliers[k]
192-
if supplier.Status().Type >= status.RUNNING {
193-
delete(s.suppliers, k)
194-
}
190+
supplier := s.suppliers[k]
191+
if int(now.Sub(supplier.Data().StartTime).Seconds()) > (supplier.Data().TimeOut * 2) {
192+
delete(s.suppliers, k)
195193
}
196-
i++
197194
}
198195
}
199196
}

internal/supplier/gosupplier.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func (s *GoSupplier) Run() {
6666
s.status.Type = status.STARTED
6767
t := time.After(time.Duration(s.TimeOut) * time.Second)
6868
stat := make(chan string)
69+
errChan := make(chan string)
6970
//collect related Hash instance
7071
hash, err := getHash(s.Algorithm, s.Key)
7172
if err != nil {
@@ -74,7 +75,7 @@ func (s *GoSupplier) Run() {
7475
}
7576

7677
//start calculation
77-
go calculate(hash, stat, s.File)
78+
go calculate(hash, stat, errChan, s.File)
7879
for {
7980
tEnd := time.Now()
8081
took := tEnd.Sub(tStart)
@@ -91,6 +92,10 @@ func (s *GoSupplier) Run() {
9192
s.status.Value = fmt.Sprintf("%dms", took.Milliseconds())
9293
s.status.Checksum = strings.Fields(val)[0]
9394
return
95+
case val := <-errChan:
96+
s.status.Type = status.ERROR
97+
s.status.Value = val
98+
return
9499
default:
95100
//still running update time
96101
s.status.Type = status.RUNNING
@@ -112,7 +117,6 @@ func (s *GoSupplier) Delete() {
112117
}
113118

114119
//terminate
115-
//TODO figure out how to terminate hash.sum() function
116120
func (s *GoSupplier) Terminate() error {
117121
if s.status.Type == status.RUNNING {
118122
s.status.Type = status.TERMINATED
@@ -126,10 +130,12 @@ func (s *GoSupplier) Data() *BaseSupplier {
126130
}
127131

128132
//calculates given file and returns checksum
129-
func calculate(hash hash.Hash, status chan string, file *file.File) {
133+
func calculate(hash hash.Hash, status chan string, errChan chan string, file *file.File) {
130134
f, _ := os.Open(file.Path())
131135
defer f.Close()
132136
if _, err := io.Copy(hash, f); err != nil {
137+
errChan <- err.Error()
138+
return
133139
}
134140
status <- hex.EncodeToString(hash.Sum(nil))
135141
}

internal/supplier/supplier.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package supplier
22

33
import (
4+
"time"
5+
46
"github.com/getsumio/getsum/internal/file"
57
"github.com/getsumio/getsum/internal/status"
68
)
@@ -22,4 +24,5 @@ type BaseSupplier struct {
2224
File *file.File
2325
TimeOut int
2426
Key string
27+
StartTime time.Time
2528
}

0 commit comments

Comments
 (0)