• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

uber / cadence / 018e0d82-79ea-4513-9f5b-be2bbdc6e60f

05 Mar 2024 07:27AM UTC coverage: 62.989% (-0.03%) from 63.015%
018e0d82-79ea-4513-9f5b-be2bbdc6e60f

push

buildkite

web-flow
Implemented ratelimiting for external calls pr wfid (guarded by feature flag) (#5704)

What changed?
Implemented the actual rate limiting on the pr workflowID project

Why?
We can now roll it out and test it without needed to wait for rollouts

How did you test it?
Tested locally and with unit tests

Potential risks
Should be very low as it is guarded by a feature flag

Release notes

Documentation Changes

18 of 18 new or added lines in 2 files covered. (100.0%)

78 existing lines in 13 files now uncovered.

93111 of 147821 relevant lines covered (62.99%)

2357.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.0
/common/persistence/sql/sqlplugin/postgres/db.go
1
// Copyright (c) 2019 Uber Technologies, Inc.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a copy
4
// of this software and associated documentation files (the "Software"), to deal
5
// in the Software without restriction, including without limitation the rights
6
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
// copies of the Software, and to permit persons to whom the Software is
8
// furnished to do so, subject to the following conditions:
9
//
10
// The above copyright notice and this permission notice shall be included in
11
// all copies or substantial portions of the Software.
12
//
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
// THE SOFTWARE.
20

21
package postgres
22

23
import (
24
        "context"
25
        "database/sql"
26
        "time"
27

28
        "github.com/jmoiron/sqlx"
29
        "github.com/lib/pq"
30

31
        "github.com/uber/cadence/common/persistence/sql/sqldriver"
32
        "github.com/uber/cadence/common/persistence/sql/sqlplugin"
33
)
34

35
type (
36
        db struct {
37
                converter   DataConverter
38
                driver      sqldriver.Driver
39
                originalDBs []*sqlx.DB
40
                numDBShards int
41
        }
42
)
43

44
func (pdb *db) GetTotalNumDBShards() int {
31,595✔
45
        return pdb.numDBShards
31,595✔
46
}
31,595✔
47

48
var _ sqlplugin.DB = (*db)(nil)
49
var _ sqlplugin.Tx = (*db)(nil)
50

51
// ErrDupEntry indicates a duplicate primary key i.e. the row already exists,
52
// check http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html
53
const ErrDupEntry = "23505"
54

55
const ErrInsufficientResources = "53000"
56
const ErrTooManyConnections = "53300"
57

58
func (pdb *db) IsDupEntryError(err error) bool {
10✔
59
        sqlErr, ok := err.(*pq.Error)
10✔
60
        return ok && sqlErr.Code == ErrDupEntry
10✔
61
}
10✔
62

63
func (pdb *db) IsNotFoundError(err error) bool {
321✔
64
        return err == sql.ErrNoRows
321✔
65
}
321✔
66

67
func (pdb *db) IsTimeoutError(err error) bool {
154✔
68
        return err == context.DeadlineExceeded
154✔
69
}
154✔
70

71
func (pdb *db) IsThrottlingError(err error) bool {
154✔
72
        sqlErr, ok := err.(*pq.Error)
154✔
73
        if ok {
216✔
74
                if sqlErr.Code == ErrTooManyConnections ||
62✔
75
                        sqlErr.Code == ErrInsufficientResources {
62✔
76
                        return true
×
77
                }
×
78
        }
79
        return false
154✔
80
}
81

82
// newDB returns an instance of DB, which is a logical
83
// connection to the underlying postgres database
84
// dbShardID is needed when tx is not nil
85
func newDB(xdbs []*sqlx.DB, tx *sqlx.Tx, dbShardID int, numDBShards int) (*db, error) {
5,563✔
86
        driver, err := sqldriver.NewDriver(xdbs, tx, dbShardID)
5,563✔
87
        if err != nil {
5,563✔
88
                return nil, err
×
89
        }
×
90

91
        db := &db{
5,563✔
92
                converter:   &converter{},
5,563✔
93
                originalDBs: xdbs, // this is kept because newDB will be called again when starting a transaction
5,563✔
94
                driver:      driver,
5,563✔
95
                numDBShards: numDBShards,
5,563✔
96
        }
5,563✔
97
        return db, nil
5,563✔
98
}
99

100
// BeginTx starts a new transaction and returns a reference to the Tx object
101
func (pdb *db) BeginTx(ctx context.Context, dbShardID int) (sqlplugin.Tx, error) {
5,470✔
102
        xtx, err := pdb.driver.BeginTxx(ctx, dbShardID, nil)
5,470✔
103
        if err != nil {
5,470✔
UNCOV
104
                return nil, err
×
UNCOV
105
        }
×
106
        return newDB(pdb.originalDBs, xtx, dbShardID, pdb.numDBShards)
5,470✔
107
}
108

109
// Commit commits a previously started transaction
110
func (pdb *db) Commit() error {
5,366✔
111
        return pdb.driver.Commit()
5,366✔
112
}
5,366✔
113

114
// Rollback triggers rollback of a previously started transaction
115
func (pdb *db) Rollback() error {
105✔
116
        return pdb.driver.Rollback()
105✔
117
}
105✔
118

119
// Close closes the connection to the mysql db
120
func (pdb *db) Close() error {
94✔
121
        return pdb.driver.Close()
94✔
122
}
94✔
123

124
// PluginName returns the name of the mysql plugin
125
func (pdb *db) PluginName() string {
1,965✔
126
        return PluginName
1,965✔
127
}
1,965✔
128

129
// SupportsTTL returns weather Postgres supports TTL
130
func (pdb *db) SupportsTTL() bool {
3,040✔
131
        return false
3,040✔
132
}
3,040✔
133

134
// MaxAllowedTTL returns the max allowed ttl Postgres supports
135
func (pdb *db) MaxAllowedTTL() (*time.Duration, error) {
×
136
        return nil, sqlplugin.ErrTTLNotSupported
×
137
}
×
138

139
// SupportsTTL returns weather Postgre supports Asynchronous transaction
140
func (pdb *db) SupportsAsyncTransaction() bool {
1,540✔
141
        return false
1,540✔
142
}
1,540✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc