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
func (crud *CRUD) registerU() gomHTTP.ServerRoute { | |
// build update sql | |
fields := crud.Config.updateFields | |
if len(fields) == 0 { | |
// allow update all fields | |
fields = crud.Config.fields | |
} | |
fieldNames := make([]string, len(fields)) | |
for i, field := range fields { | |
fieldNames[i] = field.name |
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
// Update update data | |
func (crud *CRUD) Update(data interface{}) error { | |
_, err := crud.Config.DB.NamedExec(crud.Config.sqlCRUDUpdate, data) | |
if err != nil { | |
return errors.Wrap(err, "error crud update") | |
} | |
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
// PrioritizedEntity interface | |
type PrioritizedEntity interface { | |
GetPriority() int | |
AccessResource(interface{}) | |
} | |
// GreedyMutex a mutex that handles greedy entities with higher priority | |
type GreedyMutex struct { | |
sync.Mutex | |
TimeToWait time.Duration |
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
// Compete compete for resource | |
func (mux *GreedyMutex) Compete(ctx context.Context, resource interface{}) { | |
mux.Lock() | |
greedyEntities := []PrioritizedEntity{} | |
defer func() { go mux.Compete(ctx, resource) }() | |
defer mux.Unlock() | |
defer func() { | |
var winner PrioritizedEntity | |
for _, entity := range greedyEntities { | |
if winner == nil || winner.GetPriority() < entity.GetPriority() { |
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
// GateKeeper defines gate keeper for resource | |
type GateKeeper struct { | |
ctx context.Context | |
cancel context.CancelFunc | |
mux sync.Mutex | |
queue chan PrioritizedEntity | |
} | |
// New new gate keeper | |
func New(queuedElements int) *GateKeeper { |
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
// Start starts monitoring for competition entity for resource | |
func (k *GateKeeper) Start(resource interface{}, waitTime time.Duration) { | |
k.mux.Lock() | |
defer k.mux.Unlock() | |
if k.cancel != nil { | |
k.cancel() | |
} | |
k.ctx, k.cancel = context.WithCancel(context.Background()) | |
mux := &GreedyMutex{ | |
TimeToWait: waitTime, |
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
package main | |
import ( | |
"fmt" | |
"time" | |
resourceprioritize "github.com/hauxe/go-resource-prioritize" | |
) | |
// PrioritizedEntity defines prioritized entity |
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
reader, writer, err := os.Pipe() | |
if err != nil { | |
panic(err) | |
} |
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
os.Stdout = writer | |
os.Stderr = writer | |
log.SetOutput(writer) |
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
out := make(chan string) | |
go func() { | |
var buf bytes.Buffer | |
io.Copy(&buf, reader) | |
out <- buf.String() | |
}() |