@@ -5,24 +5,36 @@ import (
55 "context"
66 "fmt"
77 "google.golang.org/api/option"
8+ "google.golang.org/api/transport"
89 "log"
910 "os"
1011 "reflect"
1112 "strconv"
1213 "time"
1314)
1415
15- func NewPubSubClientWithRetries (ctx context.Context , projectId string , keyFilename string , retries []time.Duration ) (* pubsub.Client , error ) {
16- if len (keyFilename ) > 0 && existFile (keyFilename ) {
17- log .Println ("key file exists" )
18- c , er1 := pubsub .NewClient (ctx , projectId , option .WithCredentialsFile (keyFilename ))
16+ func NewPubSubClientWithRetries (ctx context.Context , credentials []byte , retries []time.Duration , options ... string ) (* pubsub.Client , error ) {
17+ var projectId string
18+ if len (options ) > 0 && len (options [0 ]) > 0 {
19+ projectId = options [0 ]
20+ }
21+ if credentials != nil && len (credentials ) > 0 {
22+ opts := option .WithCredentialsJSON (credentials )
23+ creds , er0 := transport .Creds (ctx , opts )
24+ if er0 != nil {
25+ return nil , er0
26+ }
27+ if len (projectId ) == 0 {
28+ projectId = creds .ProjectID
29+ }
30+ c , er1 := pubsub .NewClient (ctx , projectId , opts )
1931 if er1 == nil {
2032 return c , er1
2133 }
2234 i := 0
2335 err := Retry (retries , func () (err error ) {
2436 i = i + 1
25- c2 , er2 := pubsub .NewClient (ctx , projectId , option . WithCredentialsFile ( keyFilename ) )
37+ c2 , er2 := pubsub .NewClient (ctx , projectId , opts )
2638 if er2 == nil {
2739 c = c2
2840 }
@@ -33,11 +45,11 @@ func NewPubSubClientWithRetries(ctx context.Context, projectId string, keyFilena
3345 }
3446 return c , err
3547 } else {
36- log .Println ("key file doesn't exists " )
48+ log .Println ("empty credentials " )
3749 return pubsub .NewClient (ctx , projectId )
3850 }
3951}
40- func NewPubSubClient (ctx context.Context , projectId string , keyFilename string ) (* pubsub.Client , error ) {
52+ func NewPubSubClientWithFile (ctx context.Context , projectId string , keyFilename string ) (* pubsub.Client , error ) {
4153 if len (keyFilename ) > 0 && existFile (keyFilename ) {
4254 log .Println ("key file exists" )
4355 return pubsub .NewClient (ctx , projectId , option .WithCredentialsFile (keyFilename ))
@@ -46,6 +58,28 @@ func NewPubSubClient(ctx context.Context, projectId string, keyFilename string)
4658 return pubsub .NewClient (ctx , projectId )
4759 }
4860}
61+ func NewPubSubClient (ctx context.Context , credentials []byte , options ... string ) (* pubsub.Client , error ) {
62+ opts := option .WithCredentialsJSON (credentials )
63+ var projectId string
64+ if len (options ) > 0 && len (options [0 ]) > 0 {
65+ projectId = options [0 ]
66+ } else {
67+ creds , err := transport .Creds (ctx , opts )
68+ projectId = creds .ProjectID
69+ if err != nil {
70+ panic ("Credentials Error: " + err .Error ())
71+ }
72+ if creds == nil {
73+ panic ("Error: creds is nil" )
74+ }
75+ }
76+ if credentials != nil && len (credentials ) > 0 {
77+ return pubsub .NewClient (ctx , projectId , opts )
78+ } else {
79+ log .Println ("empty credentials" )
80+ return pubsub .NewClient (ctx , projectId )
81+ }
82+ }
4983
5084func existFile (filename string ) bool {
5185 if _ , err := os .Stat (filename ); err == nil {
@@ -84,17 +118,19 @@ func DurationsFromValue(v interface{}, prefix string, max int) []time.Duration {
84118 arr := MakeArray (v , prefix , max )
85119 return MakeDurations (arr )
86120}
121+
87122type RetryConfig struct {
88- Retry1 int64 `mapstructure:"1" json:"retry1,omitempty" gorm:"column:retry1" bson:"retry1,omitempty" dynamodbav:"retry1,omitempty" firestore:"retry1,omitempty"`
89- Retry2 int64 `mapstructure:"2" json:"retry2,omitempty" gorm:"column:retry2" bson:"retry2,omitempty" dynamodbav:"retry2,omitempty" firestore:"retry2,omitempty"`
90- Retry3 int64 `mapstructure:"3" json:"retry3,omitempty" gorm:"column:retry3" bson:"retry3,omitempty" dynamodbav:"retry3,omitempty" firestore:"retry3,omitempty"`
91- Retry4 int64 `mapstructure:"4" json:"retry4,omitempty" gorm:"column:retry4" bson:"retry4,omitempty" dynamodbav:"retry4,omitempty" firestore:"retry4,omitempty"`
92- Retry5 int64 `mapstructure:"5" json:"retry5,omitempty" gorm:"column:retry5" bson:"retry5,omitempty" dynamodbav:"retry5,omitempty" firestore:"retry5,omitempty"`
93- Retry6 int64 `mapstructure:"6" json:"retry6,omitempty" gorm:"column:retry6" bson:"retry6,omitempty" dynamodbav:"retry6,omitempty" firestore:"retry6,omitempty"`
94- Retry7 int64 `mapstructure:"7" json:"retry7,omitempty" gorm:"column:retry7" bson:"retry7,omitempty" dynamodbav:"retry7,omitempty" firestore:"retry7,omitempty"`
95- Retry8 int64 `mapstructure:"8" json:"retry8,omitempty" gorm:"column:retry8" bson:"retry8,omitempty" dynamodbav:"retry8,omitempty" firestore:"retry8,omitempty"`
96- Retry9 int64 `mapstructure:"9" json:"retry9,omitempty" gorm:"column:retry9" bson:"retry9,omitempty" dynamodbav:"retry9,omitempty" firestore:"retry9,omitempty"`
123+ Retry1 int64 `mapstructure:"1" json:"retry1,omitempty" gorm:"column:retry1" bson:"retry1,omitempty" dynamodbav:"retry1,omitempty" firestore:"retry1,omitempty"`
124+ Retry2 int64 `mapstructure:"2" json:"retry2,omitempty" gorm:"column:retry2" bson:"retry2,omitempty" dynamodbav:"retry2,omitempty" firestore:"retry2,omitempty"`
125+ Retry3 int64 `mapstructure:"3" json:"retry3,omitempty" gorm:"column:retry3" bson:"retry3,omitempty" dynamodbav:"retry3,omitempty" firestore:"retry3,omitempty"`
126+ Retry4 int64 `mapstructure:"4" json:"retry4,omitempty" gorm:"column:retry4" bson:"retry4,omitempty" dynamodbav:"retry4,omitempty" firestore:"retry4,omitempty"`
127+ Retry5 int64 `mapstructure:"5" json:"retry5,omitempty" gorm:"column:retry5" bson:"retry5,omitempty" dynamodbav:"retry5,omitempty" firestore:"retry5,omitempty"`
128+ Retry6 int64 `mapstructure:"6" json:"retry6,omitempty" gorm:"column:retry6" bson:"retry6,omitempty" dynamodbav:"retry6,omitempty" firestore:"retry6,omitempty"`
129+ Retry7 int64 `mapstructure:"7" json:"retry7,omitempty" gorm:"column:retry7" bson:"retry7,omitempty" dynamodbav:"retry7,omitempty" firestore:"retry7,omitempty"`
130+ Retry8 int64 `mapstructure:"8" json:"retry8,omitempty" gorm:"column:retry8" bson:"retry8,omitempty" dynamodbav:"retry8,omitempty" firestore:"retry8,omitempty"`
131+ Retry9 int64 `mapstructure:"9" json:"retry9,omitempty" gorm:"column:retry9" bson:"retry9,omitempty" dynamodbav:"retry9,omitempty" firestore:"retry9,omitempty"`
97132}
133+
98134func Retry (sleeps []time.Duration , f func () error ) (err error ) {
99135 attempts := len (sleeps )
100136 for i := 0 ; ; i ++ {
0 commit comments