This file contains hidden or 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) Delete(data interface{}) (int64, error) { | |
rv := reflect.ValueOf(data) | |
rv = reflect.Indirect(rv) | |
pk := rv.Field(crud.Config.pk.index) | |
if !pk.CanInterface() { | |
return 0, errors.Errorf("table %s with primary key has wrong interface type", | |
crud.Config.TableName, crud.Config.pk.index) | |
} | |
result, err := crud.Config.DB.Exec(crud.Config.sqlCRUDDelete, pk.Interface()) | |
if err != nil { |
This file contains hidden or 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) registerD() gomHTTP.ServerRoute { | |
// build create sql | |
crud.Config.sqlCRUDDelete = fmt.Sprintf(sqlCRUDDelete, crud.Config.TableName, | |
crud.Config.pk.name) | |
return gomHTTP.ServerRoute{ | |
Name: "crud_delete_" + crud.Config.TableName, | |
Method: http.MethodDelete, | |
Path: fmt.Sprintf("/%s", crud.Config.TableName), | |
Validators: []gomHTTP.ParamValidator{validatePrimaryKey(crud.Config.pk.index)}, | |
Handler: crud.handleDelete, |
This file contains hidden or 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 ( | |
"bytes" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"sync" | |
) |
This file contains hidden or 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() | |
}() |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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() { |