Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: cipd/client/cipd/remote.go

Issue 2317633002: Add counter-write and counter-read commands. (Closed)
Patch Set: CL comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cipd/client/cipd/client.go ('k') | cipd/client/cipd/remote_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The LUCI Authors. All rights reserved. 1 // Copyright 2014 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package cipd 5 package cipd
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "encoding/json" 9 "encoding/json"
10 "errors" 10 "errors"
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 for i, instance := range reply.Instances { 633 for i, instance := range reply.Instances {
634 pins[i] = common.Pin{instance.PackageName, instance.Inst anceID} 634 pins[i] = common.Pin{instance.PackageName, instance.Inst anceID}
635 } 635 }
636 return pins, nil 636 return pins, nil
637 case "ERROR": 637 case "ERROR":
638 return nil, errors.New(reply.ErrorMessage) 638 return nil, errors.New(reply.ErrorMessage)
639 } 639 }
640 return nil, fmt.Errorf("unexpected searchInstances status: %s", reply.St atus) 640 return nil, fmt.Errorf("unexpected searchInstances status: %s", reply.St atus)
641 } 641 }
642 642
643 func (r *remoteImpl) incrementCounter(ctx context.Context, pin common.Pin, count er string, delta int) error {
644 endpoint, err := counterEndpoint(pin, counter)
645 if err != nil {
646 return err
647 }
648
649 var request struct {
650 Delta int `json:"delta"`
651 }
652 request.Delta = delta
653
654 var reply struct {
655 Status string `json:"status"`
656 ErrorMessage string `json:"error_message"`
657 }
658 err = r.makeRequest(ctx, endpoint, "POST", &request, &reply)
659 if err != nil {
660 return err
661 }
662 switch reply.Status {
663 case "SUCCESS":
664 return nil
665 case "ERROR":
666 return errors.New(reply.ErrorMessage)
667 }
668 return fmt.Errorf("unexpected incrementCounter status: %s", reply.Status )
669 }
670
671 func (r *remoteImpl) readCounter(ctx context.Context, pin common.Pin, counter st ring) (Counter, error) {
672 endpoint, err := counterEndpoint(pin, counter)
673 if err != nil {
674 return Counter{}, err
675 }
676
677 var reply struct {
678 Status string `json:"status"`
679 ErrorMessage string `json:"error_message"`
680
681 Value string `json:"value"`
682 CreatedTS string `json:"created_ts"`
683 UpdatedTS string `json:"updated_ts"`
684 }
685 err = r.makeRequest(ctx, endpoint, "GET", nil, &reply)
686 if err != nil {
687 return Counter{}, err
688 }
689 switch reply.Status {
690 case "SUCCESS":
691 ret := Counter{Name: counter}
692 if ret.Value, err = strconv.ParseInt(reply.Value, 10, 64); err ! = nil {
693 return Counter{}, fmt.Errorf("unexpected counter value % q in the server response", reply.Value)
694 }
695 if reply.CreatedTS != "" {
696 ts, err := convertTimestamp(reply.CreatedTS)
697 if err != nil {
698 return Counter{}, err
699 }
700 ret.CreatedTS = UnixTime(ts)
701 }
702 if reply.UpdatedTS != "" {
703 ts, err := convertTimestamp(reply.UpdatedTS)
704 if err != nil {
705 return Counter{}, err
706 }
707 ret.UpdatedTS = UnixTime(ts)
708 }
709 return ret, nil
710 case "ERROR":
711 return Counter{}, errors.New(reply.ErrorMessage)
712 }
713 return Counter{}, fmt.Errorf("unexpected readCounter status: %s", reply. Status)
714 }
715
643 //////////////////////////////////////////////////////////////////////////////// 716 ////////////////////////////////////////////////////////////////////////////////
644 717
645 func packageEndpoint(packageName string) (string, error) { 718 func packageEndpoint(packageName string) (string, error) {
646 if err := common.ValidatePackageName(packageName); err != nil { 719 if err := common.ValidatePackageName(packageName); err != nil {
647 return "", err 720 return "", err
648 } 721 }
649 params := url.Values{} 722 params := url.Values{}
650 params.Add("package_name", packageName) 723 params.Add("package_name", packageName)
651 return "repo/v1/package?" + params.Encode(), nil 724 return "repo/v1/package?" + params.Encode(), nil
652 } 725 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
730 } 803 }
731 params := url.Values{} 804 params := url.Values{}
732 params.Add("package_name", pin.PackageName) 805 params.Add("package_name", pin.PackageName)
733 params.Add("instance_id", pin.InstanceID) 806 params.Add("instance_id", pin.InstanceID)
734 for _, tag := range tags { 807 for _, tag := range tags {
735 params.Add("tag", tag) 808 params.Add("tag", tag)
736 } 809 }
737 return "repo/v1/tags?" + params.Encode(), nil 810 return "repo/v1/tags?" + params.Encode(), nil
738 } 811 }
739 812
813 func counterEndpoint(pin common.Pin, counterName string) (string, error) {
814 params := url.Values{}
815 params.Add("package_name", pin.PackageName)
816 params.Add("instance_id", pin.InstanceID)
817 params.Add("counter_name", counterName)
818 return "repo/v1/counter?" + params.Encode(), nil
819 }
820
740 // convertTimestamp coverts string with int64 timestamp in microseconds since 821 // convertTimestamp coverts string with int64 timestamp in microseconds since
741 // to time.Time 822 // to time.Time
742 func convertTimestamp(ts string) (time.Time, error) { 823 func convertTimestamp(ts string) (time.Time, error) {
743 i, err := strconv.ParseInt(ts, 10, 64) 824 i, err := strconv.ParseInt(ts, 10, 64)
744 if err != nil { 825 if err != nil {
745 return time.Time{}, fmt.Errorf("unexpected timestamp value %q in the server response", ts) 826 return time.Time{}, fmt.Errorf("unexpected timestamp value %q in the server response", ts)
746 } 827 }
747 return time.Unix(0, i*1000), nil 828 return time.Unix(0, i*1000), nil
748 } 829 }
OLDNEW
« no previous file with comments | « cipd/client/cipd/client.go ('k') | cipd/client/cipd/remote_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698