Another refactor
This commit is contained in:
parent
c5f75d6554
commit
c814742afa
1 changed files with 21 additions and 42 deletions
63
kcl/kcl.go
63
kcl/kcl.go
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -147,23 +148,18 @@ func New(
|
||||||
ioHandler: i,
|
ioHandler: i,
|
||||||
recordProcessor: recordProcessor,
|
recordProcessor: recordProcessor,
|
||||||
|
|
||||||
next: make(chan struct{}),
|
|
||||||
out: make(chan string),
|
|
||||||
outErr: make(chan error),
|
|
||||||
|
|
||||||
checkpoint: make(chan SequencePair),
|
checkpoint: make(chan SequencePair),
|
||||||
checkpointErr: make(chan error),
|
checkpointErr: make(chan error),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type KCLProcess struct {
|
type KCLProcess struct {
|
||||||
|
ckpmux sync.Mutex
|
||||||
|
readmux sync.Mutex
|
||||||
|
|
||||||
ioHandler ioHandler
|
ioHandler ioHandler
|
||||||
recordProcessor RecordProcessor
|
recordProcessor RecordProcessor
|
||||||
|
|
||||||
next chan struct{}
|
|
||||||
out chan string
|
|
||||||
outErr chan error
|
|
||||||
|
|
||||||
checkpoint chan SequencePair
|
checkpoint chan SequencePair
|
||||||
checkpointErr chan error
|
checkpointErr chan error
|
||||||
}
|
}
|
||||||
|
|
@ -192,6 +188,9 @@ func (kclp *KCLProcess) performAction(a interface{}) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kclp *KCLProcess) handleLine(line string) error {
|
func (kclp *KCLProcess) handleLine(line string) error {
|
||||||
|
kclp.readmux.Lock()
|
||||||
|
defer kclp.readmux.Unlock()
|
||||||
|
|
||||||
action, err := kclp.ioHandler.loadAction(line)
|
action, err := kclp.ioHandler.loadAction(line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -208,8 +207,7 @@ func (kclp *KCLProcess) Checkpoint(pair SequencePair, retryCount int) error {
|
||||||
sleepDuration := 5 * time.Second
|
sleepDuration := 5 * time.Second
|
||||||
|
|
||||||
for n := 0; n <= retryCount; n++ {
|
for n := 0; n <= retryCount; n++ {
|
||||||
kclp.checkpoint <- pair
|
err := kclp.processCheckpoint(pair)
|
||||||
err := <-kclp.checkpointErr
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -242,6 +240,9 @@ func (kclp *KCLProcess) Shutdown() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kclp *KCLProcess) processCheckpoint(pair SequencePair) error {
|
func (kclp *KCLProcess) processCheckpoint(pair SequencePair) error {
|
||||||
|
kclp.ckpmux.Lock()
|
||||||
|
defer kclp.ckpmux.Unlock()
|
||||||
|
|
||||||
var seq *string
|
var seq *string
|
||||||
var subSeq *int
|
var subSeq *int
|
||||||
if !pair.IsEmpty() { // an empty pair is a signal to shutdown
|
if !pair.IsEmpty() { // an empty pair is a signal to shutdown
|
||||||
|
|
@ -272,44 +273,22 @@ func (kclp *KCLProcess) processCheckpoint(pair SequencePair) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kclp *KCLProcess) startLineProcessor(
|
|
||||||
next chan struct{}, out chan string, outErr chan error,
|
|
||||||
checkpoint chan SequencePair, checkpointErr chan error,
|
|
||||||
) {
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-next:
|
|
||||||
line, err := kclp.ioHandler.readLine()
|
|
||||||
if err == nil {
|
|
||||||
if line == "" {
|
|
||||||
err = fmt.Errorf("Empty read line recieved")
|
|
||||||
}
|
|
||||||
err = kclp.handleLine(line)
|
|
||||||
}
|
|
||||||
outErr <- err
|
|
||||||
case pair := <-checkpoint:
|
|
||||||
err := kclp.processCheckpoint(pair)
|
|
||||||
checkpointErr <- err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (kclp *KCLProcess) processNextLine() error {
|
|
||||||
kclp.next <- struct{}{} // We're ready for a new line
|
|
||||||
|
|
||||||
return <-kclp.outErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (kclp *KCLProcess) Run() {
|
func (kclp *KCLProcess) Run() {
|
||||||
kclp.startLineProcessor(kclp.next, kclp.out, kclp.outErr, kclp.checkpoint, kclp.checkpointErr)
|
|
||||||
for {
|
for {
|
||||||
err := kclp.processNextLine()
|
line, err := kclp.ioHandler.readLine()
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
kclp.ioHandler.writeError("IO stream closed")
|
kclp.ioHandler.writeError("IO stream closed")
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
kclp.ioHandler.writeError(fmt.Sprintf("ERR Read line: %+#v", err))
|
||||||
|
return
|
||||||
|
} else if line == "" {
|
||||||
|
kclp.ioHandler.writeError("Empty read line recieved")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = kclp.handleLine(line)
|
||||||
|
if err != nil {
|
||||||
kclp.ioHandler.writeError(fmt.Sprintf("ERR Handle line: %+#v", err))
|
kclp.ioHandler.writeError(fmt.Sprintf("ERR Handle line: %+#v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue