// Set a new silence. func (s *Silences) Set(sil *types.Silence) (uint64, error) { s.mtx.Lock() defer s.mtx.Unlock() var ( uid uint64 err error ) err = s.db.Update(func(tx *bolt.Tx) error { b := tx.Bucket(bktSilences) // Silences are immutable and we always create a new one. uid, err = b.NextSequence() if err != nil { return err } sil.ID = uid k := make([]byte, 8) binary.BigEndian.PutUint64(k, uid) msb, err := json.Marshal(sil.Silence) if err != nil { return err } return b.Put(k, msb) }) if err != nil { return 0, err } s.cache[uid] = sil return uid, nil }
func (api *API) addSilence(w http.ResponseWriter, r *http.Request) { var sil types.Silence if err := receive(r, &sil); err != nil { respondError(w, apiError{ typ: errorBadData, err: err, }, nil) return } if sil.CreatedAt.IsZero() { sil.CreatedAt = time.Now() } if err := sil.Validate(); err != nil { respondError(w, apiError{ typ: errorBadData, err: err, }, nil) return } sid, err := api.silences.Set(&sil) if err != nil { respondError(w, apiError{ typ: errorInternal, err: err, }, nil) return } respond(w, struct { SilenceID uint64 `json:"silenceId"` }{ SilenceID: sid, }) }
// Set impelements the Silences interface. func (s *MemSilences) Set(sil *types.Silence) (uint64, error) { s.mtx.Lock() defer s.mtx.Unlock() if sil.ID == 0 { sil.ID = uint64(len(s.silences) + 1) } else { if _, ok := s.silences[sil.ID]; !ok { return 0, ErrNotFound } } s.silences[sil.ID] = &sil.Silence return sil.ID, nil }