本文整理匯總了Golang中github.com/buaazp/uq/utils.NewError函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewError函數的具體用法?Golang NewError怎麽用?Golang NewError使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewError函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: MultiPush
// MultiPush implements MultiPush interface
func (u *UnitedQueue) MultiPush(key string, datas [][]byte) error {
key = strings.TrimPrefix(key, "/")
key = strings.TrimSuffix(key, "/")
for i, data := range datas {
if len(data) <= 0 {
cause := "message " + strconv.Itoa(i) + " has no content"
return utils.NewError(
utils.ErrBadRequest,
cause,
)
}
}
u.topicsLock.RLock()
t, ok := u.topics[key]
u.topicsLock.RUnlock()
if !ok {
return utils.NewError(
utils.ErrTopicNotExisted,
`queue multiPush`,
)
}
return t.mPush(datas)
}
示例2: create
func (u *UnitedQueue) create(key, rec string, fromEtcd bool) error {
key = strings.TrimPrefix(key, "/")
key = strings.TrimSuffix(key, "/")
var topicName, lineName string
var err error
parts := strings.Split(key, "/")
if len(parts) < 1 || len(parts) > 2 {
return utils.NewError(
utils.ErrBadKey,
`create key parts error: `+utils.ItoaQuick(len(parts)),
)
}
topicName = parts[0]
if topicName == "" {
return utils.NewError(
utils.ErrBadKey,
`create topic is nil`,
)
}
if len(parts) == 2 {
lineName = parts[1]
var recycle time.Duration
if rec != "" {
recycle, err = time.ParseDuration(rec)
if err != nil {
return utils.NewError(
utils.ErrBadRequest,
err.Error(),
)
}
}
u.topicsLock.RLock()
t, ok := u.topics[topicName]
u.topicsLock.RUnlock()
if !ok {
return utils.NewError(
utils.ErrTopicNotExisted,
`queue create`,
)
}
err = t.createLine(lineName, recycle, fromEtcd)
if err != nil {
// log.Printf("create line[%s] error: %s", lineName, err)
return err
}
} else {
err = u.createTopic(topicName, fromEtcd)
if err != nil {
// log.Printf("create topic[%s] error: %s", topicName, err)
return err
}
}
return err
}
示例3: Confirm
// Confirm implements Confirm interface
func (u *UnitedQueue) Confirm(key string) error {
key = strings.TrimPrefix(key, "/")
key = strings.TrimSuffix(key, "/")
parts := strings.Split(key, "/")
if len(parts) != 3 {
return utils.NewError(
utils.ErrBadKey,
`confirm key parts error: `+utils.ItoaQuick(len(parts)),
)
}
topicName := parts[0]
lineName := parts[1]
id, err := strconv.ParseUint(parts[2], 10, 0)
if err != nil {
return utils.NewError(
utils.ErrBadKey,
`confirm key parse id error: `+err.Error(),
)
}
u.topicsLock.RLock()
t, ok := u.topics[topicName]
u.topicsLock.RUnlock()
if !ok {
// log.Printf("topic[%s] not existed.", topicName)
return utils.NewError(
utils.ErrTopicNotExisted,
`queue confirm`,
)
}
return t.confirm(lineName, id)
}
示例4: Pop
// Pop implements Pop interface
func (u *UnitedQueue) Pop(key string) (string, []byte, error) {
key = strings.TrimPrefix(key, "/")
key = strings.TrimSuffix(key, "/")
parts := strings.Split(key, "/")
if len(parts) != 2 {
return "", nil, utils.NewError(
utils.ErrBadKey,
`pop key parts error: `+utils.ItoaQuick(len(parts)),
)
}
tName := parts[0]
lName := parts[1]
u.topicsLock.RLock()
t, ok := u.topics[tName]
u.topicsLock.RUnlock()
if !ok {
// log.Printf("topic[%s] not existed.", tName)
return "", nil, utils.NewError(
utils.ErrTopicNotExisted,
`queue pop`,
)
}
id, data, err := t.pop(lName)
if err != nil {
return "", nil, err
}
return utils.Acatui(key, "/", id), data, nil
}
示例5: createTopic
func (u *UnitedQueue) createTopic(name string, fromEtcd bool) error {
u.topicsLock.RLock()
_, ok := u.topics[name]
u.topicsLock.RUnlock()
if ok {
return utils.NewError(
utils.ErrTopicExisted,
`queue createTopic`,
)
}
t, err := u.newTopic(name)
if err != nil {
return err
}
u.topicsLock.Lock()
defer u.topicsLock.Unlock()
u.topics[name] = t
err = u.exportQueue()
if err != nil {
t.remove()
delete(u.topics, name)
return err
}
if !fromEtcd {
u.registerTopic(t.name)
}
log.Printf("topic[%s] created.", name)
return nil
}
示例6: onQmpop
func (r *RedisEntry) onQmpop(cmd *command) *reply {
key := cmd.stringAtIndex(1)
n, err := cmd.intAtIndex(2)
if err != nil {
return errorReply(utils.NewError(
utils.ErrBadRequest,
err.Error(),
))
}
ids, values, err := r.messageQueue.MultiPop(key, n)
if err != nil {
return errorReply(err)
}
np := len(ids)
vals := make([]interface{}, np*2)
for i, index := 0, 0; i < np; i++ {
vals[index] = values[i]
index++
vals[index] = ids[i]
index++
}
return multiBulksReply(vals)
}
示例7: statHandler
func (s *UnitedAdmin) statHandler(w http.ResponseWriter, req *http.Request, key string) {
if req.Method != "GET" {
http.Error(w, "405 Method Not Allowed!", http.StatusMethodNotAllowed)
return
}
qs, err := s.messageQueue.Stat(key)
if err != nil {
writeErrorHTTP(w, err)
return
}
// log.Printf("qs: %v", qs)
data, err := qs.ToJSON()
if err != nil {
writeErrorHTTP(w, utils.NewError(
utils.ErrInternalError,
err.Error(),
))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
示例8: removeTopic
func (u *UnitedQueue) removeTopic(name string, fromEtcd bool) error {
u.topicsLock.Lock()
defer u.topicsLock.Unlock()
t, ok := u.topics[name]
if !ok {
return utils.NewError(
utils.ErrTopicNotExisted,
`queue remove`,
)
}
delete(u.topics, name)
err := u.exportQueue()
if err != nil {
u.topics[name] = t
return err
}
if !fromEtcd {
u.unRegisterTopic(name)
}
return t.remove()
}
示例9: createLine
func (t *topic) createLine(name string, recycle time.Duration, fromEtcd bool) error {
t.linesLock.Lock()
defer t.linesLock.Unlock()
_, ok := t.lines[name]
if ok {
return utils.NewError(
utils.ErrLineExisted,
`topic createLine`,
)
}
l, err := t.newLine(name, recycle)
if err != nil {
return err
}
t.lines[name] = l
err = t.exportTopic()
if err != nil {
t.linesLock.Lock()
delete(t.lines, name)
t.linesLock.Unlock()
return err
}
if !fromEtcd {
t.q.registerLine(t.name, l.name, l.recycle.String())
}
log.Printf("topic[%s] line[%s:%v] created.", t.name, name, recycle)
return nil
}
示例10: removeLine
func (t *topic) removeLine(name string, fromEtcd bool) error {
t.linesLock.RLock()
defer t.linesLock.RUnlock()
l, ok := t.lines[name]
if !ok {
// log.Printf("topic[%s] line[%s] not existed.", t.name, name)
return utils.NewError(
utils.ErrLineNotExisted,
`topic statLine`,
)
}
delete(t.lines, name)
err := t.exportTopic()
if err != nil {
t.linesLock.Lock()
t.lines[name] = l
t.linesLock.Unlock()
return err
}
if !fromEtcd {
t.q.unRegisterLine(t.name, name)
}
return l.remove()
}
示例11: pop
func (l *line) pop() (uint64, []byte, error) {
l.inflightLock.Lock()
defer l.inflightLock.Unlock()
now := time.Now()
if l.recycle > 0 {
m := l.inflight.Front()
if m != nil {
msg := m.Value.(*InflightMessage)
exp := time.Unix(0, msg.Exptime)
if now.After(exp) {
// log.Printf("key[%s/%d] is expired.", l.name, msg.Tid)
msg.Exptime = now.Add(l.recycle).UnixNano()
data, err := l.t.getData(msg.Tid)
if err != nil {
return 0, nil, err
}
l.inflight.Remove(m)
l.inflight.PushBack(msg)
// log.Printf("key[%s/%s/%d] poped.", l.t.name, l.name, msg.Tid)
return msg.Tid, data, nil
}
}
}
l.headLock.Lock()
defer l.headLock.Unlock()
tid := l.head
topicTail := l.t.getTail()
if l.head >= topicTail {
// log.Printf("line[%s] is blank. head:%d - tail:%d", l.name, l.head, l.t.tail)
return 0, nil, utils.NewError(
utils.ErrNone,
`line pop`,
)
}
data, err := l.t.getData(tid)
if err != nil {
return 0, nil, err
}
l.head++
if l.recycle > 0 {
msg := new(InflightMessage)
msg.Tid = tid
msg.Exptime = now.Add(l.recycle).UnixNano()
l.inflight.PushBack(msg)
// log.Printf("key[%s/%s/%d] flighted.", l.t.name, l.name, l.head)
l.imap[tid] = true
}
return tid, data, nil
}
示例12: Empty
// Empty implements Empty interface
func (u *UnitedQueue) Empty(key string) error {
key = strings.TrimPrefix(key, "/")
key = strings.TrimSuffix(key, "/")
var topicName, lineName string
parts := strings.Split(key, "/")
if len(parts) < 1 || len(parts) > 2 {
return utils.NewError(
utils.ErrBadKey,
`empty key parts error: `+utils.ItoaQuick(len(parts)),
)
}
topicName = parts[0]
if topicName == "" {
return utils.NewError(
utils.ErrBadKey,
`empty topic is nil`,
)
}
u.topicsLock.RLock()
t, ok := u.topics[topicName]
u.topicsLock.RUnlock()
if !ok {
return utils.NewError(
utils.ErrTopicNotExisted,
`queue empty`,
)
}
if len(parts) == 2 {
lineName = parts[1]
return t.emptyLine(lineName)
// err = t.emptyLine(lineName)
// if err != nil {
// log.Printf("empty line[%s] error: %s", lineName, err)
// }
// return err
}
return t.empty()
}
示例13: delData
func (u *UnitedQueue) delData(key string) error {
err := u.storage.Del(key)
if err != nil {
// log.Printf("key[%s] del data error: %s", key, err)
return utils.NewError(
utils.ErrInternalError,
err.Error(),
)
}
return nil
}
示例14: setData
func (u *UnitedQueue) setData(key string, data []byte) error {
err := u.storage.Set(key, data)
if err != nil {
// log.Printf("key[%s] set data error: %s", key, err)
return utils.NewError(
utils.ErrInternalError,
err.Error(),
)
}
return nil
}
示例15: getData
func (u *UnitedQueue) getData(key string) ([]byte, error) {
data, err := u.storage.Get(key)
if err != nil {
// log.Printf("key[%s] get data error: %s", key, err)
return nil, utils.NewError(
utils.ErrInternalError,
err.Error(),
)
}
return data, nil
}